日本語

Using CSS :only-child to Change Styles When an Element Is the Sole Child

​​CSS​HTML,Playwright

With the CSS :only-child pseudo-class, you can change the style of an element only when it is the single child of its parent. You can use it to widen a card to the full width when a card list or search result has just one item, or to hide the delete button on the last remaining list item. You can write these adjustments in CSS alone, without counting items in JavaScript.

Note

I tested on Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened the HTML in Playwright, added outline to the selectors, and used getComputedStyle() to check which elements the rule applied to. I also read widths and display values with getBoundingClientRect() and getComputedStyle(). I did not test Firefox or Safari.

Widening a single card to the full row width

In a card list laid out as a 3-column grid, you can make a card span all three columns when it is the only card by applying :only-child and setting grid-column.

css
.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.card:only-child {
  grid-column: 1 / -1;
}

I set the width of .cards to 800px, changed the number of cards, and measured the widths.

Number of cards Width of each card
1 800px
2 256px, 256px
3 256px, 256px, 256px

Only when there is one card does :only-child match, so grid-column: 1 / -1 takes effect and the card spans the three columns at 800px. With two or more cards, the rule no longer matches, and the layout returns to the normal three columns.

Two stacked screenshots. The top one shows three cards in three equal-width columns. The bottom one shows a single card, outlined in pink, stretched across the full width of all three columns
The same CSS applied with three cards and with one card (the card matched by :only-child is outlined in pink)

Hiding the delete button on the last remaining item

In a list where items can be removed, you can also use :only-child to prevent the last item from being deleted.

html
<ul class="list">
  <li class="item">項目1 <button class="delete">削除</button></li>
  <li class="item">項目2 <button class="delete">削除</button></li>
</ul>
css
.item:only-child .delete {
  display: none;
}

With two items, the display of both delete buttons was inline-block. When I removed one li with JavaScript, the delete button of the remaining item became none. When I added an li back, both buttons returned to inline-block. Adding or removing elements changes whether :only-child matches, immediately. You do not need JavaScript that counts items and changes classes.

A list of three items. The cursor clicks the delete button twice. Each click removes one item, and when one item remains, its delete button disappears and a pink message appears
Clicking the delete button twice; the delete button disappears when one item remains (the pink outline and message were added for clarity)

However, CSS only hides the button visually. If there is a rule about how many items can be deleted, check the remaining count in the delete logic as well.

What counts as a sibling and what does not

:only-child matches an element that has no sibling elements. The MDN description of :only-child says the same. I checked which nodes count as siblings using several HTML snippets.

HTML Selector Result
<p>前の文<!-- コメント --><b>強調</b>後ろの文</p> b:only-child Matches
A <ul> with line breaks and indentation, containing one <li> li:only-child Matches
A div with ::before and ::after, containing one span span:only-child Matches
<ul><li>表示中</li><li hidden>非表示</li></ul> li:only-child Does not match

Even with text or comments before and after it, b matched :only-child. Adding line breaks and indentation to make the HTML easier to read did not change the result. The structural pseudo-classes section of Selectors Level 4 states that when counting positions among children, things that are not elements, such as text, are not counted. Pseudo-elements created with ::before and ::after did not count as siblings either.

On the other hand, an li hidden with the hidden attribute still counted as a sibling. Even if only one item is visible on screen, :only-child does not match. If you filter items by hiding them with hidden instead of removing them from the DOM, the single-item style will not switch on.

Using :only-of-type when there is only one of that element name

I tested a box that contains one heading and one paragraph, and selected the p for the body text.

html
<div class="box">
  <h3>見出し</h3>
  <p>本文</p>
</div>

p:only-child matched nothing, because the h3 is a sibling. p:only-of-type matched the p containing "Body text". As the MDN description of :only-of-type explains, it only checks whether there is a sibling with the same element name. Use :only-of-type when you want to select an element even though other kinds of elements are next to it.

Matching the same elements as :first-child:last-child

The specification's definition of :only-child says that :only-child is the same as :first-child:last-child and :nth-child(1):nth-last-child(1), except that its specificity is lower. I tested this with a ul that has one child and a ul that has three children. All three selectors matched only the li in the ul with one child.

The difference in specificity shows up when other rules conflict with it.

css
li:first-child:last-child { color: rgb(255, 0, 0); }
li:only-child { color: rgb(0, 0, 255); }

In a ul with one child, the color of the li was red (rgb(255, 0, 0)). li:only-child, written later, loses because its specificity is lower than li:first-child:last-child, which has two pseudo-classes. If old CSS still uses :first-child:last-child and you try to override it with :only-child, the override may not take effect.

Using :has() to change the parent when it has only one child

:only-child matches the child. If you want to change the background or spacing of the parent when it has only one child, put :only-child inside :has().

css
.cards:has(> :only-child) {
  background: rgb(238, 250, 253);
}

The background of .cards changed to rgb(238, 250, 253) only when it contained one card. With two cards, it stayed white. For more on :has(), see the MDN description of :has().

​Posted by 86