Day 67: counting children

There are a lot of interesting things you can do with the :has() pseudo-class. I’ve already covered some of them on day 26.

If you want to style an element based on the number of direct children it has, you can do that with just CSS.
Let’s say you want to style a list in a certain way when it contains at least three items. You use the :has() pseudo-class with the condition that it has a direct child item that is a third child.

Note: Firefox doesn’t support :has() yet.

ul:has(>:nth-child(3)) {
  border: 10px solid red;
}
  • A
  • B
  • A
  • B
  • C
  • A
  • B
  • C
  • D

If you want to limit the rule to only apply styles if the list contains exactly three items, you extend the condition and only select the <ul> if it contains a direct child item that is the third and last item.

ul:has(>:nth-child(3):last-child) {
  border: 10px solid red;
}
  • A
  • B
  • A
  • B
  • C
  • A
  • B
  • C
  • D

My blog doesn’t support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović

There are a lot of interesting things you can do with the :has() pseudo-class. I’ve already covered some of them on day 26.

If you want to style an element based on the number of direct children it has, you can do that with just CSS.
Let's say you want to style a list in a certain way when it contains at least three items. You use the :has() pseudo-class with the condition that it has a direct child item that is a third child.

Note: Firefox doesn't support :has() yet.

ul:has(>:nth-child(3)) {
  border: 10px solid red;
}
  • A
  • B
  • A
  • B
  • C
  • A
  • B
  • C
  • D

If you want to limit the rule to only apply styles if the list contains exactly three items, you extend the condition and only select the <ul> if it contains a direct child item that is the third and last item.

ul:has(>:nth-child(3):last-child) {
  border: 10px solid red;
}
  • A
  • B
  • A
  • B
  • C
  • A
  • B
  • C
  • D

My blog doesn't support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2022-12-27T00:00:00+00:00) Day 67: counting children. Retrieved from https://www.scien.cx/2022/12/27/day-67-counting-children-2/

MLA
" » Day 67: counting children." Manuel Matuzović | Sciencx - Tuesday December 27, 2022, https://www.scien.cx/2022/12/27/day-67-counting-children-2/
HARVARD
Manuel Matuzović | Sciencx Tuesday December 27, 2022 » Day 67: counting children., viewed ,<https://www.scien.cx/2022/12/27/day-67-counting-children-2/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 67: counting children. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/27/day-67-counting-children-2/
CHICAGO
" » Day 67: counting children." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2022/12/27/day-67-counting-children-2/
IEEE
" » Day 67: counting children." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2022/12/27/day-67-counting-children-2/. [Accessed: ]
rf:citation
» Day 67: counting children | Manuel Matuzović | Sciencx | https://www.scien.cx/2022/12/27/day-67-counting-children-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.