How to use the :nth-child() generator
:nth-child() styles elements by their position among their siblings, so a list, a table or a grid can be striped, ranged or picked apart without adding a class to every item. The formula inside the brackets is An+B: A is the step, B is where the pattern starts, and n counts 0, 1, 2 and so on. The hard part is doing the arithmetic in your head — this page does it for you, in both directions.
To use it:
- Test a formula. Pick the pseudo-class, type a formula such as 3n+1, and watch the grid: every matching item lights up, with the exact list of positions and a plain-English description of what the formula does. Drag the A and B sliders to feel how the step and the start move the pattern.
- Build from clicks. Switch to Build mode and click the items you want styled. The generator returns the shortest selector that picks exactly those items — one An+B formula for a regular pattern, a two-selector range for a middle run, or a list of single positions where no pattern fits.
- Copy the CSS. The ready-to-paste block uses
.list > lias the example — swap in your own selector, paste it into your stylesheet, and check the result in your browser's developer tools. - Try the presets. Zebra stripes, every third item, the first three, from the fourth onwards, the last two, and a middle range — each one shows the formula, the matches and the explanation, so the pattern sticks.
- Check ranges the same way. A middle range such as items 4 to 6 has no
single An+B form. Tick "Combine with a second selector" and the tester chains
:nth-child(n+4):nth-child(-n+6)— both must match, so only items 4, 5 and 6 are picked. - Count from the right end when it helps. :nth-last-child() counts from
the end, so the last two items are
-n+2whatever the list length is — much simpler than working out the number of the third-to-last item.
What makes this generator different
The pages that rank for this search are testers: they take a formula and show what it matches, and stop there. None of them works backwards. The usual real-world question is the opposite one — "I want these items, what is the selector?" — and answering it by hand means solving An+B on paper and hoping the pattern closes exactly where the list ends. This page answers both directions in one place: test any formula against a live grid, or click the items you want and get the shortest selector that picks exactly those items — with a plain-English explanation, the full match list, and copy-ready CSS. It also refuses to invent a pattern: if a formula would catch an item you did not click, it lists the items one by one instead of handing you a selector that styles the wrong element.
When is this the right tool?
Whenever a selector has to hit positions, not classes: zebra-striping table rows, styling every third card in a grid, highlighting the first three items of a list, fading in a staggered animation by row, or picking a range of gallery thumbnails. It is also the fastest way to learn the syntax — drag the sliders and watch the pattern move — and the fastest way to check a formula someone else wrote before it goes into your stylesheet.
Honest limitations
The grid assumes identical items
In the grid every item is the same type, so :nth-of-type() matches exactly the same positions as :nth-child(). In mixed content they differ — a paragraph that is the second child is not necessarily the second paragraph. The FAQ above explains the difference; check your real markup before trusting a type-based selector.
The "of selector" filter is not supported
CSS Selectors Level 4 lets you write :nth-child(2n of .picture), which counts only siblings matching a selector. That filter is about matching selectors, not positions alone, so a position-only generator cannot evaluate it. The tester rejects it plainly instead of pretending to check it.
A generated formula is minimal for the list shown
The reverse generator works against the list size on screen (20 items by default). A range such as items 4 to 6 behaves the same at any list length, but a pattern such as 3n+1 keeps matching past item 10 — so if your real list is longer, re-check the formula against that length by changing the item count above.
It cannot read your page
The generator works only from the formula you type or the items you click. It cannot open your stylesheet or your HTML, and it cannot tell you which selector your page actually needs — that judgement is yours. Paste the CSS into your own file and verify it in your browser's developer tools.
Generated CSS uses a placeholder selector
The output block styles .list > li as an example. Replace it with your
own selector — the table rows, cards or list items you actually want styled — before
the CSS does anything on your page.
Rules from the CSS Selectors specification
The rules here follow CSS Selectors Level 3 and Level 4 as they read in November 2026: An+B with n counting from 0, the odd and even keywords, negative steps counting down from B, and :nth-last-child() counting from the end. Browsers implement :nth-child() identically; the Level 4 "of" filter is the only part this page does not evaluate.