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.

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

<p>My blog doesn’t support comments yet, but you can reply via <a href=’mailto:blog@matuzo.at?subject=Comment%20on%20%E2%80%9CDay%2067%3A%20counting%20children%E2%80%9D’>e-mail</a>.</p>


This content originally appeared on Manuel Matuzović - Web development 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.

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
<p>My blog doesn't support comments yet, but you can reply via <a href='mailto:blog@matuzo.at?subject=Comment%20on%20%E2%80%9CDay%2067%3A%20counting%20children%E2%80%9D'>e-mail</a>.</p>


This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

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

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

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.