Color scheme

Filtered :nth-child()

You already know that :nth-child() selects elements by their position among siblings. You can pick every other row, every third item, or a specific index using the An+B formula. But classic :nth-child() counts all siblings, regardless of type or class. If you hide a row, add a heading between list items, or mix different element types together, the count includes everything, and your carefully planned pattern breaks.

CSS Selectors Level 4 added the of S syntax, which lets you pre-filter which elements get counted before the An+B formula runs. This means you can say things like "the second item with class .featured" or "every other visible row" and the count only includes elements that match the filter. The syntax is supported in all modern browsers (Chrome 111+, Firefox 113+, Safari 9+).

The Problem with Classic Counting

To understand why filtered :nth-child() matters, consider the most common use case: zebra-striped table rows. The traditional approach looks like this:


        tr:nth-child(even) {
            background-color: #f1f5f9;
        }
    

This works perfectly for a static table. But the moment you hide a row (with JavaScript filtering, a [hidden] attribute, or display: none), the pattern falls apart. If row 2 is hidden, the browser still counts it. Row 3, which is now visually the second row, keeps its odd styling. You end up with two adjacent rows that share the same background color, and the stripes are broken.

Before the of S syntax existed, the only fix was JavaScript: loop through the visible rows, manually toggle classes, and re-apply them every time the filter changed. Now CSS can handle it natively.

The of S Syntax

The full syntax is :nth-child(An+B of S), where S is a selector list. The browser first collects all siblings that match S, then applies the An+B formula to that filtered set. Elements that do not match S are invisible to the count.


        /* Stripe only visible rows (skip hidden ones) */
        tr:nth-child(even of :not([hidden])) {
            background-color: #f1f5f9;
        }

        /* Select the 1st element with class .featured */
        :nth-child(1 of .featured) {
            border: 2px solid gold;
        }

        /* Every other item that matches .active */
        :nth-child(odd of .active) {
            background-color: #dbeafe;
        }

        /* The 3rd element matching either .sale or .featured */
        :nth-child(3 of .sale, .featured) {
            font-weight: bold;
        }
    

The S part accepts a full selector list, so you can combine multiple classes or selectors separated by commas. The same syntax works with :nth-last-child(An+B of S) for counting from the end.

A Critical Distinction

The order of the selector and the pseudo-class changes what gets selected. These two rules look similar but do very different things:


        /* Filtered: among all .highlight siblings, select the 2nd one */
        :nth-child(2 of .highlight) { ... }

        /* Unfiltered: select the 2nd child, but only if it also has .highlight */
        .highlight:nth-child(2) { ... }
    

In the first rule, the browser gathers every sibling with .highlight, ignores everything else, and picks the second one from that filtered group. In the second rule, the browser looks at the second child overall (counting all siblings), and only applies the style if that child happens to have .highlight. If the second child does not have the class, nothing matches. This distinction is the entire point of the of S syntax, and it is the most important thing to communicate clearly in your teaching video.

Practical Patterns

Zebra Striping with Filtered Rows

The filtered zebra stripe pattern fixes the broken-stripes problem for any table or list where items can be hidden or filtered dynamically:


        /* Only count rows that are not hidden */
        tr:nth-child(even of :not([hidden])) {
            background-color: #f1f5f9;
        }
    

Highlighting the First N Matching Items

If you have a mixed list and want to highlight the first three items of a particular type, the negative-range trick works with filters too:


        /* First 3 items with .priority class */
        :nth-child(-n+3 of .priority) {
            border-left: 3px solid var(--accent);
        }
    

Quantity Queries with Filters

You can combine :nth-last-child() with :first-child to apply styles based on how many matching items exist. This is sometimes called a "quantity query." For example, if there are at least four .error items, you could style the container differently:


        /* Matches if there are 4+ .error items */
        :nth-last-child(4 of .error):first-child,
        :nth-last-child(4 of .error):first-child ~ .error {
            background-color: #fee2e2;
        }
    

Live Demo

The demo below shows a list of items, some with a .featured class (marked with a star). Use the toggle to hide the non-featured items. Notice how the zebra stripes re-count correctly because the CSS uses :nth-child(even of :not([hidden])) instead of plain :nth-child(even):

  • Apples
  • Cherries
  • Elderberries
  • Figs
  • Honeydew

The key CSS rule behind the stripes is a single line. It ignores hidden items entirely when deciding which rows are "even":


        li:nth-child(even of :not([hidden])) {
            background: #f1f5f9;
        }
    

Resources

Use these references to deepen your understanding and find ideas for your teaching video:

Teaching Video Checklist

You do not need to follow this order, but your teammates should come away understanding each of these concepts:

  • The difference between .highlight:nth-child(2) and :nth-child(2 of .highlight), and why it matters
  • The of S syntax: how the browser pre-filters siblings before applying the An+B count
  • A working demo showing filtered zebra striping that survives hidden or removed rows
  • At least one other practical pattern (highlighting the first N matching items, or selecting every other item of a specific class)
  • That :nth-last-child(An+B of S) works the same way but counts from the end