Sub topic 8
Styling by number of children
This chapter assumes you know :has() with the child combinator (>), :nth-child(), and :not(). A quantity query is a selector that matches an element only when it has at least, at most, or exactly some number of children. Before :has(), these needed tricks with :nth-last-child() aimed at the children. With :has() you can target the parent directly.
The core idea
:nth-child(4) matches an element only if it is the fourth child of its parent. So if a list has a fourth child, the list contains something that matches :nth-child(4). That is the question :has() asks:
ul:has(> li:nth-child(4)) {
columns: 2;
}The list gets two columns only when it has four or more li children. The > restricts the test to direct children, so list items nested deeper do not count.
At least, fewer than, and exactly
Add :not() to invert the test. Combining selectors gives the other quantities. These examples use n for the count of direct children.
| Goal | Selector |
|---|---|
| n ≥ 4 | `ul:has(> :nth-child(4))` |
| n < 4 | `ul:not(:has(> :nth-child(4)))` |
| n = 1 | `ul:has(> :only-child)` |
| n = 3 | `ul:has(> :nth-child(3)):not(:has(> :nth-child(4)))` |
The "exactly 3" rule reads as: has a third child, and does not have a fourth. Here is a complete example you can run:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Quantity queries</title>
<style>
ul { padding-inline-start: 1.5rem; }
ul:has(> :nth-child(4)) {
columns: 2;
}
ul:has(> :only-child) {
list-style: none;
font-weight: bold;
}
ul:has(> :nth-child(3)):not(:has(> :nth-child(4))) {
background: lightyellow;
}
</style>
</head>
<body>
<ul><li>Only</li></ul>
<ul><li>One</li><li>Two</li><li>Three</li></ul>
<ul><li>One</li><li>Two</li><li>Three</li><li>Four</li><li>Five</li></ul>
</body>
</html>The first list is bold with no bullet. The second has a yellow background. The third is split into two columns.
Styling the children too
You can combine the parent test with a descendant selector. This makes items fill the available width when there are only a few of them:
.toolbar:has(> button:nth-child(4)) > button {
font-size: 0.85rem;
}The :has() part decides which .toolbar matches. The > button after it styles the buttons inside it. This is how you restyle children based on how many siblings they have.
Common errors
- Counting every element.
:nth-child(4)counts all element siblings, not onlyli. Inul:has(> :nth-child(4)), a fourth child of any type counts. Writing> li:nth-child(4)requires that the fourth child is anli, so a mixed list ofliandscriptelements may not match as you expect. For counting only certain elements,:nth-child(4 of li)works in current browsers, but check support for your audience. - Forgetting
>.ul:has(:nth-child(4))matches if any descendant is a fourth child, including inside nested lists. A short outer list with a long nested list would match. - Counting text. Text nodes and comments are not elements, so they are never counted.
- Dynamic changes. Quantity queries re-evaluate when the DOM changes. Adding a fourth item at runtime switches the layout with no script.
Exercise
Write a selector that matches a div.gallery only when it has exactly two direct children, and explain why .gallery:has(> :nth-child(2)) alone is not enough.
Show answer
Use .gallery:has(> :nth-child(2)):not(:has(> :nth-child(3))). The first part requires at least two children and the second rules out a third. .gallery:has(> :nth-child(2)) alone matches any gallery with two or more children, because a gallery with five children also has a second child.