Sub topic 5
Selecting by Position: :first-child, :last-child, :only-child, :empty, :root
This chapter assumes you know HTML elements and attributes, CSS rule syntax, type, class, ID and attribute selectors, and the descendant and child combinators. It also assumes you can tell a pseudo-class from a pseudo-element by its syntax.
What structural pseudo-classes do
A structural pseudo-class selects an element by where it sits in the document tree. It does not look at classes, IDs or user actions. The browser checks the element's parent and siblings, and the rule applies if the position matches.
This removes the need for classes such as first or last in your HTML. When the markup changes, the styling follows.
:first-child and :last-child
:first-child matches an element that is the first child of its parent. :last-child matches the last one.
li {
border-bottom: 1px solid #ccc;
}
li:last-child {
border-bottom: 0;
}
li:first-child {
font-weight: bold;
}Each list item gets a bottom border, then the final item removes its own. The result is a divider between items with none after the last.
A common mistake is reading p:first-child as "the first paragraph inside its parent". It means "an element that is a p and is also the first child of its parent". If the first child is a heading, no paragraph in that parent matches.
<div>
<h2>Title</h2>
<p>First paragraph</p>
</div>Here p:first-child matches nothing, because the h2 is the first child. To count only elements of the same type, use :first-of-type, which a later chapter covers.
:only-child
:only-child matches an element that has no siblings. It is the same as :first-child:last-child together.
.tags li:only-child {
list-style: none;
}This removes the marker when a list has a single item.
:empty
:empty matches an element with no children at all. Element children and text nodes both count, so even a single text character prevents a match. Comments and processing instructions are ignored.
.notice:empty {
display: none;
}Take care with whitespace. In <div class="notice"> </div>, the space is a text node. Under the CSS Selectors Level 3 definition that element does not match. Selectors Level 4 allows whitespace-only elements to match, but browser support for that has been slow, so write the markup with no space between the tags if you depend on it.
:root
:root matches the top element of the document. In HTML that is <html>. Its specificity is that of a pseudo-class, which is higher than the html type selector. The usual use is to declare custom properties that the whole page can read.
:root {
--brand: #1a73e8;
--space: 1rem;
}
a {
color: var(--brand);
}Quick reference
| Selector | Matches |
|---|---|
| `:first-child` | The first child of its parent |
| `:last-child` | The last child of its parent |
| `:only-child` | A child with no siblings |
| `:empty` | An element with no children and no text |
| `:root` | The document's top element |
Exercise
Given <section><h2>News</h2><p>Hello</p></section>, does p:first-child match the paragraph? Explain why or why not.
Show answer
No. The h2 is the first child of the section, and :first-child checks the element's position among all siblings, not just siblings of the same type.
Exercise
Write a rule that hides any <p class="status"> that contains nothing at all.
Show answer
p.status:empty { display: none; }