The CSS :nth-child() selector targets elements by their order among a parent’s children, making patterns such as every third list item easy to style. Learn how expressions like 3n work, and how position-based rules can shape a layout without adding extra classes or IDs.

Multiple Choice

What does the nth-child selector do in CSS?

The nth-child selector in CSS is designed to select elements based on their position within a parent element's list of child elements. It allows for targeted styling of elements that match a specific pattern according to their order number. This can be useful for applying styles like background colors, margins, or other properties to every nth child element, which can help create visually appealing layouts or effects without the need for additional classes or IDs. For instance, if you wanted to style every third list item in a series of list items, you could use the nth-child selector to achieve that with a rule like `li:nth-child(3n)`. This functionality provides significant flexibility in design, enabling developers to fine-tune the appearance of elements based solely on their sequence in the document structure, without altering HTML markup. The other choices do not accurately describe the function of the nth-child selector, emphasizing its specific focus on element positioning rather than attributes like ID or class.

CSS :nth-child(): Styling Elements by Their Place in Line

CSS can style an element because of its tag, class, or ID. But sometimes the order of the elements matters more. Maybe a menu needs a different background on every second item, or a table should shade alternating rows. That’s where the :nth-child() selector comes in: it targets elements according to their position among a parent’s children.

The selector can add visual rhythm to a page without adding extra classes to the HTML. Think of a row of chairs: you can pick the third chair, every other chair, or every chair after the first two. CSS can do the same with elements.

What does :nth-child() look at?

The :nth-child() pseudo-class matches an element based on its numbered position among its parent’s child elements. The number starts at one, not zero.

For example, this rule selects the third list item:


li:nth-child(3) {

color: tomato;

}

If a <ul> contains several <li> elements, only the third one gets the tomato-colored text. The selector doesn’t need an ID or a special class on that item; its position does the work.

That distinction matters. A class describes an element by a label you assign, while :nth-child() describes it by where it sits in the structure. Both tools are useful, but they solve different problems.

The simple patterns: odd, even, and every few items

You can use familiar keywords to target alternating elements:


tr:nth-child(odd) {

background-color: #f2f5f8;

}

tr:nth-child(even) {

background-color: white;

}

This creates striped table rows, often called zebra striping. The pattern makes it easier to follow a line across a wide table, especially when several columns are packed with data. It’s a small style choice, but it can make a page feel much clearer.

For repeating groups, use a formula. To select every third list item:


li:nth-child(3n) {

font-weight: bold;

}

The n stands for a sequence of whole-number steps. As it grows, 3n produces positions 3, 6, 9, 12, and so on. In plain terms, CSS counts forward in groups of three.

You can also combine multiplication with an offset:


li:nth-child(3n + 1) {

color: navy;

}

That matches positions 1, 4, 7, 10, and onward. The formula is more flexible than it may look at first. A pattern such as 4n + 2 selects positions 2, 6, 10, 14—every fourth item, starting at the second.

A small formula with a big reach

The general form is an + b. Here, a controls the step between matches, while b shifts the starting point. You don’t need to use a formula for every design, of course. For a single item, a plain number is easier to read. But when a pattern repeats, the formula keeps the stylesheet tidy.

For example, imagine a gallery that displays three cards in each row. You might want to remove the right margin from every third card:


.card:nth-child(3n) {

margin-right: 0;

}

This relies on the layout and markup lining up as expected. If the number of cards per row changes on a smaller screen, the spacing rule may need to change too. Responsive design often asks us to revisit these patterns: a selector can be perfectly valid and still need a different formula at another screen width.

The parent element matters

Here’s a detail that can catch people out: :nth-child() counts all child elements of the parent, not only elements of the tag named in the selector.

Consider this markup:


<div class="list">

<h2>Featured items</h2>

<p>First message</p>

<p>Second message</p>

<p>Third message</p>

</div>

The first paragraph is the second child of .list, because the heading comes before it. So this rule won’t select that first paragraph:


p:nth-child(1) {

color: teal;

}

There is no paragraph in the first child position. The heading is there.

If you want to count only children of a particular type, :nth-of-type() may be a better fit:


p:nth-of-type(1) {

color: teal;

}

This selects the first <p> among its sibling paragraphs, even though a heading appears earlier. The difference is simple but important:

  • :nth-child() counts an element’s position among all element siblings.

  • :nth-of-type() counts its position among siblings with the same tag name.

Choosing between them depends on the structure you’re styling. When a selector doesn’t match what you expected, checking the parent’s children is often the fastest way to find the reason.

Why use a positional selector at all?

A positional selector can keep HTML cleaner. Instead of adding class="highlight" to every third item, you can describe the pattern once in CSS. That’s handy for repeated content, such as navigation links, card grids, lists, and table rows.

It also keeps styling connected to structure. If a new item is added in the middle of a list, the pattern updates automatically. That can be exactly what you want. On the other hand, if a particular item must stay highlighted no matter where it moves, a named class is usually a clearer choice. Position is great for patterns; classes are better for meaning or a specific role.

This is one of those CSS decisions that seems tiny but shapes how easy a project is to maintain. A rule based on “every third card” communicates a layout pattern. A class such as .important-card communicates purpose. They may create a similar visual effect, but they tell future developers different things.

A few quick details to keep in mind

The count begins at one. The selector is written with a colon, as in :nth-child(), and its argument can be a number, odd, even, or an an + b formula. It applies to elements in the document structure; it doesn’t count text nodes or whitespace between tags.

You can also use negative values to create limited ranges. For instance, :nth-child(-n + 3) selects the first three children. That can be useful when a design needs to emphasize an opening group without adding a separate class to each item.

Still, readable CSS is the goal. If a selector starts to feel like a puzzle, a class may be the more sensible choice. CSS gives you options, not a rule that every layout must be built from formulas.

A useful way to think about it

The :nth-child() selector styles elements by their position in a parent’s child list. It can pick one specific position, alternate between odd and even items, or follow a repeating formula such as every third child. Used well, it brings order to repeated designs with very little extra markup.

Just remember the counting rule: CSS counts all element children of the parent. Once that detail clicks, :nth-child() becomes less like a bit of code magic and more like a clear instruction—style this item because of where it sits.