Native CSS Nesting & The & Selector

For over fifteen years, the ability to nest CSS rules inside one another was the #1 reason developers installed SASS or Less build tools. Today, Native CSS Nesting is supported natively across 100% of modern web browsers, enabling clean, hierarchical stylesheets with zero compile steps.

Basic Nesting Syntax with the Ampersand (&)

The ampersand (&) acts as a reference to the parent selector:

/* Card Component with Native Nesting */
.card {
  background-color: #ffffff;
  border-radius: 8px;
  padding: 20px;

  /* Nests .card-title inside .card */
  .card-title {
    color: #0f172a;
    font-size: 20px;
    margin-bottom: 8px;
  }

  /* Nests pseudo-class: .card:hover */
  &:hover {
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
  }

  /* Target child elements */
  p {
    color: #475569;
    line-height: 1.6;
  }
}

Nesting Media Queries Directly Inside Components

Instead of splitting your component styles across distant @media blocks at the bottom of the stylesheet, nest responsive queries directly inside the component:

.hero-section {
  padding: 30px 15px;

  @media (width >= 768px) {
    padding: 80px 40px;
  }

  @media (width >= 1024px) {
    padding: 120px 60px;
  }
}

Explore Related CSS Tutorials & Next Steps

Boost your CSS and front-end development proficiency by exploring these closely connected topics:

CSS Box Model & border-box

Understand margins, borders, padding, content and the universal box-sizing fix.

CSS Flexbox Complete Guide

Master 1D alignment, justify-content, align-items, flex-wrap and responsive flow.

CSS Grid 2D Layout Matrix

Build powerful two-dimensional grid layouts with template areas and fr units.

CSS Specificity & Cascade

Eliminate styling conflicts and master selector weights from 0,0,0,1 to !important.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. Is the & ampersand always required in Native CSS Nesting?

Under the original spec it was required, but modern CSS engines now support relaxed nesting: you can nest element selectors like 'p { ... }' directly inside a class without an ampersand.

Q2. What is the difference between SASS nesting and Native CSS nesting?

SASS compiles nested rules into flat selectors ahead of time (e.g., .parent .child). Native CSS is parsed directly by the browser engine using the :is() selector under the hood, meaning nested rules have the specificity of :is().

Q3. Can I concatenate class names like &--modifier in Native CSS?

No! Native CSS nesting does not support string concatenation like '&__element' or '&--modifier' (popular in SASS BEM). You must write the full selector.

Q4. Does deep nesting harm browser performance?

Over-nesting (e.g., nesting 6+ levels deep) increases selector specificity and makes styles harder to maintain. Best practice is to keep nesting to 2 or 3 levels max.
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!