A css selector first child rule, written as li:first-child, matches an li only when it is the first element child of its parent. Use :last-of-type for the last element of one tag among its siblings, :nth-child(even) and :nth-child(odd) for striping, and :not() to skip items such as a disabled one. The input is a small HTML list or table, and the output is a set of styled rows that you can predict before you test them. I build the markup in the HTML Code Editor and draft the rules in the CSS Editor.
I also compare each selector with jQuery’s zero-based eq(), because that is where many readers arrive from. This is an evergreen tutorial: the selector behavior below follows the Selectors Level 4 Working Draft dated January 22, 2026, and the one newer syntax I show is flagged so you can check it against the browsers you target.
Table of Contents

What a CSS selector first child rule actually tests
Structural selectors test an element’s position among its siblings in the document tree, not where it ends up on screen. Counting starts at 1, and only elements are counted, so whitespace between tags and text nodes never shift the numbers. Reordering items visually with CSS does not change their position in the tree either, so the selectors keep matching the source order.
The detail that causes most mistakes is that li:first-child does not mean the first li, whatever comes before it. It matches an li only when that li is the first element child of its parent. If a heading sits before the first list item inside the same parent, no li in that parent is a first child.
The second common mistake is a stray space. Compare these two selectors:
li:first-child { font-weight: 700; }
li :first-child { font-weight: 700; }The first targets the li itself. The second, with a space, targets any first-child element that is a descendant of an li, such as a span inside it. Omit the space when you mean the li. A related whitespace trap appears in my guide on why a CSS minifier removes the space before :focus, which is a good reason to keep your readable source file after minifying.

Build the test list in the HTML Code Editor
Open the HTML Code Editor and paste the markup below. The editor offers syntax highlighting, auto-closing tags and a sandboxed live preview. It does not validate, so I check by eye that every tag is closed. The style element sits above the list so a single document holds both markup and rules.
<style>
.list { max-width: 16rem; padding: 0; list-style: none; }
.list > li { padding: 0.4rem 0.6rem; }
</style>
<ul class="list">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li class="disabled">Four</li>
</ul>Four items with one marked as disabled is enough to separate every selector in this article. The base rules only add spacing, so the preview should show four plain rows.
The CSS Editor gives you syntax highlighting, property auto-completion and a live preview, but that preview uses a fixed sample page, so it will not show your list. The two editors do not share a live document. I draft rules in the CSS Editor, then paste the tested rules into the style element in the HTML Code Editor to compare them against the same markup. My guide to a visual CSS editor with a live preview and its limits goes further into what that sample page can and cannot show.

Predict first, then test :first-child, odd, even and :not()
Add the rules one at a time and say out loud which items you expect to change before you look at the preview. That habit is what makes the positions stick.
.list > li:first-child { font-weight: 700; }
.list > li:nth-child(odd) { background: #e8f0fe; }
.list > li:nth-child(even) { background: #fdecea; }
.list > li:not(.disabled) { border-left: 4px solid #2563eb; }- :first-child matches only One, because it is the first element child of the list.
- :nth-child(odd) matches positions 1, 3, 5 and so on, so One and Three. It is the same as
2n+1. - :nth-child(even) matches positions 2, 4, 6, so Two and Four. It is the same as
2n. - :not(.disabled) matches every
liwithout that class, so One, Two and Three. Four is excluded.
This css even odd selector pair covers every child exactly once, which is why it is the standard way to stripe rows. Rules can overlap: One is both first and odd. Different properties simply combine. If two matching rules set the same property, the one with higher specificity wins, and a tie goes to the later rule in the source.
CSS selectors last of type versus :last-child
The type-based pseudo-classes count only siblings with the same element name. That makes :last-of-type the right choice when different element types are mixed in one parent. Use this separate, valid example:
<div class="post"> <p>First</p> <p>Second</p> <small>Note</small> </div>
.post > p:last-of-type { color: #b00020; }
.post > p:last-child { text-decoration: underline; }Second turns red, because it is the last p among the siblings, even though a small follows it. Nothing is underlined, because the last child of the div is the small, and the selector requires a p in that spot. Delete the small line and Second gets both styles, since the two selectors then agree.
The same logic applies to :first-of-type. When your rules grow beyond a few lines, the CSS Beautifier formats pasted CSS with clean indentation, which makes it easier to spot where two selectors overlap. It improves layout only, and it will not tell you whether a selector matches.
The not selector css rule and its specificity
The not selector css form, :not(), excludes elements that match its argument. Scope it to an element type, as in li:not(.disabled). A bare :not(.disabled) matches every element in the document that lacks the class, including html, body and wrappers, which is rarely what you want.
Specificity comes from the most specific argument inside the parentheses, not from an extra weight for the pseudo-class itself. So li:not(.disabled) scores 0-1-1: one class from the argument and one type selector. That is the same as li.enabled, and a rule such as .list li at 0-1-1 ties with it, leaving source order to decide.
When a rule matches but still loses, the cause is nearly always the cascade, not the selector. My article on why a CSS :where rule loses to footer a walks through scoring a matching rule that is outweighed, and the same method works for :not(). For an introduction, keep the argument to one simple selector. Complex or comma-separated arguments deserve their own compatibility check against the browsers you support.
Table rows, even stripes and the hidden-row pitfall
Row striping is the classic use of :nth-child(even). Select rows inside one row group so header rows do not shift the count. The HTML parser wraps loose rows in a tbody, so write the tbody explicitly and select tbody > tr.
<table>
<caption>Team availability</caption>
<thead><tr><th>Name</th><th>Status</th></tr></thead>
<tbody>
<tr><td>Ana</td><td>Open</td></tr>
<tr><td>Ben</td><td>Busy</td></tr>
<tr hidden><td>Cy</td><td>Away</td></tr>
<tr><td>Dee</td><td>Open</td></tr>
</tbody>
</table>tbody > tr:nth-child(even) { background: #f1f5f9; }Ben and Dee get the stripe. Cy is hidden but still occupies position 3, so Dee, the third visible row, counts as fourth and is striped as even. The visible stripes now break the alternating pattern. If you need a starting table, the HTML Table Generator builds a table in a spreadsheet-style grid and lets you copy the markup. Check the copied output for the row grouping you need and adjust it by hand.
The Selectors Level 4 draft shows tr:nth-child(even of :not([hidden])) for exactly this case, counting only rows that are not hidden. MDN documents basic :nth-child() as widely available since July 2015 but cautions that support varies for parts of the syntax, so verify the of form against your target browsers before relying on it.
Pick the nth item the way jQuery's eq() does
The jQuery documentation defines .eq(index) as an index into the matched set, starting at zero, so .eq(2) picks the third element of that set. A negative value such as .eq(-2) counts back from the end. CSS counts from one and counts siblings, not a matched set.
li:nth-child(3) is therefore an analogue only when the items you care about are siblings under the same parent and their child positions line up with the set. It is not a general replacement for .eq(2) across several parents or a filtered set. Likewise :nth-last-child(2) is the sibling-based counterpart of .eq(-2).
When the set is the siblings that carry a class, the two forms differ. Take this markup:
<ul> <li>Intro</li> <li class="item">A</li> <li class="item">B</li> <li class="item">C</li> </ul>
.item:nth-child(3) { color: #b00020; }
:nth-child(3 of .item) { outline: 2px solid #2563eb; }.item:nth-child(3) tests whether an .item is third among all siblings, so it matches B. :nth-child(3 of .item) counts only the siblings that match .item, so it matches C, which is the true analogue of .eq(2) on that set. Treat the of form separately for browser support and check it before you ship it.
Which selector fits: css selector first child and its relatives
| Selector | Matches | Use it when |
|---|---|---|
.list > li:first-child | An li that is its parent’s first element child | You know the first child is the item you want |
.list > li:last-of-type | The last li among siblings, even if another type follows | Different tags are mixed in one parent |
.list > li:nth-child(odd) | Positions 1, 3, 5 | You want alternating stripes |
.list > li:nth-child(even) | Positions 2, 4, 6 | You want the other half of the stripes |
.list > li:not(.disabled) | List items without that class | You need to skip one state |
:nth-child(3 of .item) | The third sibling that matches .item | You want an eq-style pick, after checking support |
A clean preview proves only that the rule matched this markup. To verify a selector properly, change the markup and see what moves:
- Write down which items you expect to match before you look.
- Insert an extra element before the first item and confirm that
:first-childstops matching. - Add a different tag at the end and confirm that
:last-of-typestill matches while:last-childdoes not. - Hide a row and check whether your stripes still alternate.
- Compare against any competing rule using the three-column specificity score.
Before minifying, keep a readable copy of your rules, since minification is a separate step that does not check selector logic.
Frequently asked questions
What does the CSS selector first child (:first-child) match?
It matches an element only when that element is the first element child of its parent. Text and whitespace do not count, but an earlier element of any type does. So li:first-child fails if a heading comes before the first li inside the same parent.
What is the difference between :last-of-type and :last-child?
The :last-child pseudo-class requires the element to be the very last child of its parent. The :last-of-type pseudo-class only requires it to be the last sibling of its own tag. In a div containing two p elements followed by a small element, p:last-of-type matches the second p, while p:last-child matches nothing.
How do I use the CSS even odd selector for striped rows?
Use :nth-child(odd) and :nth-child(even) on the rows, for example tbody > tr:nth-child(even). Odd matches positions 1, 3, 5 and even matches 2, 4, 6. Select rows inside one row group so header rows do not shift the count.
How does the not selector css work with classes?
The :not() pseudo-class matches elements that do not match its argument, so li:not(.disabled) matches every li without the disabled class. Scope it to an element type, because a bare :not(.disabled) matches many unrelated elements. Its specificity comes from its argument, so li:not(.disabled) scores 0-1-1.
Is li:nth-child(3) the same as jQuery's .eq(2)?
Only in a narrow case. The eq() method indexes the matched set from zero, while nth-child counts sibling positions from one. They line up when the matched items are siblings under one parent and their positions align with the set. For a set of class-matching siblings, :nth-child(3 of .item) is closer, but check its browser support.
Why is my :first-child rule not applying?
Common causes are an earlier sibling element, an accidental space that turns li:first-child into li :first-child, or another matching rule with higher specificity. Check the markup order first, then the selector spacing, then compare specificity scores.
Does :nth-child(even) count hidden rows?
Yes. Hidden elements keep their positions in the document tree, so ordinary :nth-child(even) still counts them and can break visible stripes. The Selectors Level 4 draft shows tr:nth-child(even of :not([hidden])) for this case, but verify support for the of syntax in your target browsers.
Next steps
The css selector first child rule, along with :last-of-type, :nth-child(even), :nth-child(odd) and :not(), always asks a question about sibling position or class, never about what you see on screen. Pick :first-child when position among all siblings is the point, a type-based selector when tags are mixed, nth-child for stripes, and a scoped :not() to exclude a state. Your next step: paste the list from this article into the HTML Code Editor, add one rule at a time, then insert a stray element and a hidden row to see exactly which selectors break.
Related tools and resources on HTML Editor Online:
- CSS Editor
- CSS Minifier Removes Space Before Focus
- HTML Code Editor
- Visual CSS Editor Browser Live Preview
