What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
CSS :only-child selects an element when it has no sibling elements—that is, when it is the sole element child of its parent. Use it to apply a rule only when a container has one child, such as changing the layout of a one-card list.
What does :only-child select?
The selector tests the selected element’s position among its siblings. It matches when there are no other element children beside it. For example:
li:only-child {
color: rebeccapurple;
}
This rule colors a list item only when it is the sole element child of its parent list. If another element child is added, the item no longer matches; if that sibling is removed, it matches again. The selector follows the document structure, including changes made dynamically.
The test is about siblings, not the selected element’s own contents. An element with no children of its own is a different condition, tested by :empty. See MDN’s pseudo-class reference for the distinction.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitches#1 Best Overall
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
How is :only-child different from :only-of-type?
:only-child asks whether the element has any sibling elements at all. :only-of-type asks whether it has any siblings of the same element type. So a paragraph can be the only p child and match p:only-of-type even if its parent also contains a h2; it would not match p:only-child in that structure. These are distinct structural selectors, as reflected in MDN’s pseudo-class reference.
Is it the same as :first-child:last-child?
For matching, yes. The W3C Selectors Level 4 specification says :only-child is equivalent to :first-child:last-child and to :nth-child(1):nth-last-child(1): each form selects an element that is first and last among its siblings. The W3C notes that :only-child has lower specificity than either two-pseudo-class alternative.
Rank #2
| Selector | What it expresses | Specificity |
|---|---|---|
E:only-child |
An E element with no siblings |
Lower than the two alternatives below |
E:first-child:last-child |
An E element that is both first and last among siblings |
Higher; it uses two pseudo-classes |
E:nth-child(1):nth-last-child(1) |
An E element that is first from both ends |
Higher; it uses two pseudo-classes |
When the intended condition is “this is the only child,” :only-child is the direct, more specific-to-intent expression without adding specificity. Refer to the W3C Selectors Level 4 specification for the normative definition.
How can you use it in a layout?
Use the selector when a component should behave differently with one child than with several. For example, a card list might use a grid when it has multiple cards but allow a single card to span the available width:
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Rank #3
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
.cards:has(.card) {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.card:only-child {
grid-column: 1 / -1;
}
The selector itself only detects the sibling condition; declarations such as grid alignment, spacing, or color depend on the design. A simpler list example is:
li:only-child {
text-align: center;
}
That alignment applies only when the list item has no element siblings. If another item appears, the rule stops applying without needing a separate class or script.
Rank #4
Does :only-child work across browsers?
MDN describes :only-child as broadly available across browsers and devices. That is general guidance, not a promise about every historical browser or a particular minimum version. If your project has strict legacy-browser requirements, check its target browser matrix against a current compatibility reference before relying on a specific version claim.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Free tools Windows power users keep installed
One-click scans. No signup required.

