CSS
Web Development
HTML
Web Design
Coding Tips

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.

css
li:not(:first-child) {
  margin-top: 12px;
}

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.

html
1<ul>
2  <li>Apple</li>    <!-- first-child, NOT matched -->
3  <li>Banana</li>   <!-- matched -->
4  <li>Cherry</li>   <!-- matched -->
5</ul>
css
li:not(:first-child) {
  color: #666;
}

"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:

css
1.menu-item:not(:first-child) {
2  border-top: 1px solid #e0e0e0;
3  padding-top: 8px;
4}

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:

html
1<main>
2  <section class="content-block">Hero</section>
3  <section class="content-block">Features</section>
4  <section class="content-block">Pricing</section>
5</main>
css
.content-block:not(:first-child) {
  margin-top: 48px;
}

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:

css
.form-group input:not(:first-child) {
  margin-top: 16px;
}

:not(:first-child) vs :not(:first-of-type)

These behave differently when the parent contains mixed element types.

html
1<div>
2  <h2>Title</h2>
3  <p>First paragraph</p>
4  <p>Second paragraph</p>
5</div>
css
1/* Matches <p> elements that are NOT the first child of <div> */
2/* Both <p> tags match, because <h2> is the actual first child */
3p:not(:first-child) {
4  color: blue;
5}
6
7/* Matches <p> elements that are NOT the first <p> in <div> */
8/* Only "Second paragraph" matches */
9p:not(:first-of-type) {
10  color: red;
11}
SelectorWhat It ChecksWhen 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
1/* All nav links except the first child */
2nav a:not(:first-child) {
3  margin-left: 16px;
4}
5
6/* All cards except the first child that also have .highlighted */
7.card.highlighted:not(:first-child) {
8  border-left: 4px solid #0066cc;
9}
10
11/* Exclude both first and last child */
12li:not(:first-child):not(:last-child) {
13  border-bottom: 1px solid #ccc;
14}

CSS Selectors Level 4 (supported in modern browsers) lets :not() take a comma-separated list:

css
1/* Exclude first and last child in a single :not() */
2li:not(:first-child, :last-child) {
3  padding: 8px 0;
4}

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).

css
1/* Specificity: 0-1-1 (one element + one pseudo-class) */
2li:not(:first-child) { color: red; }
3
4/* Specificity: 0-1-1 (same weight) */
5li:first-child { color: blue; }

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:

css
1/* Apply margin-top to every li that follows another li */
2li + li {
3  margin-top: 12px;
4}

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:

css
* + * {
  margin-top: 1.5em;
}

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:

css
1.flex-container {
2  display: flex;
3  flex-direction: column;
4  gap: 12px;
5}

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+.

BrowserSingle-Arg :not()Multi-Arg :not()
Chrome1+88+
Firefox1+84+
Safari3.1+9+
Edge12+88+
IE9+Not supported

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms