How to select the first child element with CSS
By Flavio
Published · Updated
Tagged: CSS
Learn the difference between :first-child and :first-of-type in CSS, and select the first list inside a container without adding a class.
Problem: in my markdown file I had the need to style the first ul, the table of contents, and I couldn’t add a class or an id to target it with CSS.
So I looked into how to target it with a selector.
The HTML structure was this:
<div id="content">
<ul>
...the TOC
</ul>
...rest of the HTML
</div>
I tested every selector in this post in Chrome 157.
:first-child
If the ul is definitely the first child element, use :first-child:
#content > ul:first-child {
/* styles */
}
Read it as “a ul that is the first child of its parent, where the parent is #content”. It does not mean “the first child of the ul”. To style the first item inside the list, put :first-child on the li:
#content > ul > li:first-child {
/* styles */
}
Only elements count. Text and HTML comments before the ul don’t stop it from being the first child:
<div id="content">
Some text <!-- a comment -->
<ul>...</ul> <!-- still matches ul:first-child -->
</div>
:first-of-type
If another element might appear before the list, but you still want the first ul, use :first-of-type:
#content > ul:first-of-type {
/* styles */
}
With this HTML, ul:first-child matches nothing, because the h2 is the first child. ul:first-of-type matches the first ul:
<div id="content">
<h2>Contents</h2>
<ul>...</ul> <!-- ul:first-of-type -->
<ul>...</ul>
</div>
The difference matters. :first-child means the element must be the first sibling of any type. :first-of-type means it must be the first sibling with that tag name.
Don’t forget the >
Without the child combinator >, the selector also matches nested elements. A nested list inside the TOC is a ul and it’s the first child of its li, so it matches too:
<div id="content">
<ul> <!-- matches -->
<li>
Intro
<ul> <!-- also matches #content ul:first-child -->
<li>Sub item</li>
</ul>
</li>
</ul>
</div>
#content ul:first-child and #content ul:first-of-type both match the 2 lists. #content > ul:first-child only matches the outer one.
:first-of-type ignores classes
This one looks like it should work, but it doesn’t:
.toc:first-of-type {
/* styles */
}
“Type” means the tag name, not the class. This selects an element with the toc class if it’s also the first of its tag among its siblings. With this HTML, it matches nothing:
<div>
<ul>...</ul>
<ul class="toc">...</ul> <!-- not matched by .toc:first-of-type -->
<ul class="toc">...</ul>
</div>
To get the first element with a class, use :nth-child() with the of syntax:
:nth-child(1 of .toc) {
/* styles */
}
This counts only the siblings that match .toc, and picks the first one. It works in Chrome 111+, Firefox 113+ and Safari 9+, according to MDN’s compatibility data.
Related selectors
They all follow the same rules as above:
| Selector | Matches |
|---|---|
:last-child | the last child element of its parent |
:last-of-type | the last sibling with that tag name |
:nth-child(2) | the second child element |
:nth-of-type(2) | the second sibling with that tag name |
:only-child | an element that has no siblings |
> :first-child | the first child, whatever its tag (for example #content > :first-child) |
A common use of :first-child is removing the top margin of the first element in a container, whatever it is:
.card > :first-child {
margin-top: 0;
}
For the full list of pseudo-classes, see my CSS pseudo-classes post.
Want me to talk about your product? You can sponsor this site.