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
This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.