CSS :not(:first-child) selector: style all but the first element
Codemia Knowledge Hub ·
Use :not(:first-child) to target every element that is not the first child of its parent. It combines the negation pseudo-class :not() with :first-child, so styles apply to all siblings except the first. Typical uses are spacing between list items, separators, and repeated components where the first one needs different treatment.
That one rule adds vertical spacing between list items without touching the first, with no extra class or override rule.
How :not(:first-child) matches
:first-child matches an element that is the first child of its parent. :not() inverts the selector passed to it. Together they match every child that is not in the first position.
"Banana" and "Cherry" turn gray. "Apple" keeps the default color because it is the first child.
Practical uses
Separators between items
Add a top border to every item except the first:
This avoids the stray border above the first item that a blanket border-top rule would produce.
Spacing between sections
When stacking sections, you usually want space between them but not above the first:
This is cleaner than giving every section margin-top and then resetting the first one with margin-top: 0.
Spacing form fields
In a form with stacked inputs, you can space fields without a wrapper:
:not(:first-child) vs :not(:first-of-type)
These behave differently when the parent contains mixed element types.
| Selector | What It Checks | When to Use |
:not(:first-child) | Is this element the first child overall? | Uniform lists of the same element type |
:not(:first-of-type) | Is this the first of its element type? | Mixed-content containers with headings, paragraphs, etc. |
Use :not(:first-of-type) when the parent holds different element types and you want all but the first of one type. Mixing these two up is the most frequent mistake with this selector.
Combining :not() with other selectors
:not() combines with classes, attributes and other pseudo-classes:
CSS Selectors Level 4 (supported in modern browsers) lets :not() take a comma-separated list:
Watch the nesting level. div :not(:first-child), with a space, means any descendant that is not a first child, not just direct children. Use div > :not(:first-child) for direct children only.
Specificity of :not(:first-child)
:not() adds no specificity of its own; the selector inside it does. So :not(:first-child) has the same specificity as :first-child alone: one pseudo-class (0-1-0).
Rules using :not(:first-child) and :first-child have equal specificity, so normal cascade order decides. Keep this in mind when debugging overrides: the weight comes from the argument, not from :not().
Alternatives to :not(:first-child)
Adjacent sibling selector
Before :not() was widely supported, the adjacent sibling combinator did the same job:
This works for consecutive siblings of the same type and has broader legacy browser support, though that rarely matters today.
Lobotomized owl selector
A general pattern that spaces all adjacent siblings:
It is broader than :not(:first-child) but solves the same problem: space between items without extra space at the start.
gap in flexbox and grid
If the parent is a flex or grid container, gap is simpler and more maintainable than margin-based :not(:first-child) spacing:
Browser support
:not(:first-child) works in all modern browsers, including Chrome, Firefox, Safari, Edge and Opera, and the single-argument form has been supported since IE 9. The Level 4 multi-argument form (:not(A, B)) is supported in Chrome 88+, Firefox 84+ and Safari 9+.
| Browser | Single-Arg :not() | Multi-Arg :not() |
| Chrome | 1+ | 88+ |
| Firefox | 1+ | 84+ |
| Safari | 3.1+ | 9+ |
| Edge | 12+ | 88+ |
| IE | 9+ | Not supported |
Related reading
- How can I use axios in lambda?
- How can I use jQuery in a Chrome extension?
- How can I use *ngIf else?
- How can I use Tensorflow with react-native?
- How can I use the variables from views.py in JavasScript, script/script in a Django template?
- How can I vertically align elements in a div?
- How can I vertically center a div element for all browsers using CSS?
- How can I write ''a:hover'' in inline CSS?
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.