The :nth-child() selector, without the maths headache — test it, or build it by clicking

:nth-child() is one of the most useful selectors in CSS and one of the easiest to get wrong: does 3n+1 start at item 1 or item 4, and how do you pick items 4 to 6? Type any formula and see exactly what it matches — or click the items you want and get the shortest selector that picks exactly those. Entirely in your browser.

  • Test any formula — see exactly what matches
  • Click items to generate the formula
  • nth-child, nth-last-child and nth-of-type
  • Free, no account, nothing uploads

Generate or test a :nth-child() selector

Try a preset:
:nth-child( )
2
1

Your list — 20 items

Your selector

Matches nothing yet.

Type a formula above, or switch to Build mode and click items.

Selector

—

Ready-to-paste CSS

/* The selector and its CSS appear here. */

Test a formula, or switch to Build mode and click items.

Privacy you can verify, not just trust

A selector generator has no business uploading anything — the formulas you type and the items you click are yours. This one can't: everything is checked with plain JavaScript on your device, and it never leaves the device — the only third-party requests are the site's own analytics, which count page views. Don't take our word for it:

New cross-origin requests during use: 0
  1. Press F12 to open developer tools, then the Network tab.
  2. Type a formula or click items above. Watch: apart from the site's analytics, nothing new is requested — no uploads, and no data ever leaves your device.
  3. Extra credit: disconnect your internet after this page loads — the generator still works.

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:

  1. 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.
  2. 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.
  3. Copy the CSS. The ready-to-paste block uses .list > li as the example — swap in your own selector, paste it into your stylesheet, and check the result in your browser's developer tools.
  4. 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.
  5. 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.
  6. Count from the right end when it helps. :nth-last-child() counts from the end, so the last two items are -n+2 whatever 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.

Frequently asked questions

What is the :nth-child() selector in CSS?

The :nth-child() selector picks elements by their position among their siblings. li:nth-child(3) styles the third list item, li:nth-child(odd) styles every odd one, and li:nth-child(3n+1) styles the 1st, 4th, 7th and so on. The position is counted from the start of the parent, and every matching sibling is styled — you never need to add a class to each item by hand.

How does the An+B formula in :nth-child() work?

An+B is a recipe: A is the step size and B is where the pattern starts, with n counting 0, 1, 2 and so on. In 3n+1, n = 0 gives item 1, n = 1 gives item 4, n = 2 gives item 7. A negative step counts down from B instead: -n+3 matches items 1, 2 and 3. The keywords odd and even are shortcuts for 2n+1 and 2n. Type any formula into the tester above and it lists exactly which items match, so you can see the maths instead of guessing it.

What is the difference between :nth-child() and :nth-of-type()?

:nth-child() counts every sibling whatever its type, while :nth-of-type() counts only siblings of the same element type. In a list of identical items, such as li elements inside one ul, they match exactly the same positions. They differ in mixed content: in a div holding h2, p and p elements, p:nth-of-type(2) is the second paragraph, while p:nth-child(2) only matches if the second child happens to be a paragraph. This generator lets you switch between all four pseudo-classes so you can see the difference.

How do I select the first three items, or the last two?

For the first three items use :nth-child(-n+3) — the negative step counts down from 3. For the last two use :nth-last-child(-n+2), which counts from the end of the list instead of the start. For a middle range such as items 4 to 6, chain two selectors on the same element: li:nth-child(n+4):nth-child(-n+6). All three are presets in the tester above — click one and the grid shows exactly what it matches.

Can I build a selector by clicking the items I want?

Yes — switch to Build mode above and click the items in the grid. The generator works out the shortest selector that picks exactly those items: a single An+B formula for a regular pattern such as every third item, a two-selector range for a middle run such as items 4 to 6, or a list of single positions where no pattern fits. It never invents a pattern: if a formula would also catch an item you did not click, it lists the items one by one instead.

Does this generator upload anything I type or click?

No. The generator runs entirely in your browser with plain JavaScript: the formulas you type and the items you click are checked on your own device and never leave it. The only third-party requests are the site's own analytics, which count page views. You can verify it yourself with the network-request counter in the privacy section below the generator.