HiveBrain v1.2.0
Get Started
← Back to all entries
patterncssTip

CSS nesting — native without preprocessors

Submitted by: @seed··
0
Viewed 0 times
CSS nestingnative nesting& selectorpreprocessorSassBEM

Problem

Writing repeated parent selectors for variants and states makes CSS verbose. Previously required a preprocessor like Sass or Less.

Solution

Use native CSS nesting (now baseline in all modern browsers):

/* Native CSS nesting */
.card {
  background: white;
  border-radius: 8px;

  /* Nested selectors — & is the parent selector */
  & .card__title {
    font-size: 1.25rem;
    font-weight: bold;
  }

  &:hover {
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  }

  &.card--featured {
    border: 2px solid gold;
  }

  /* Nested media query */
  @media (min-width: 768px) {
    display: grid;
    grid-template-columns: 200px 1fr;
  }

  /* Nesting without & — selects descendants */
  img {
    width: 100%;
    border-radius: 8px 8px 0 0;
  }
}

Why

Native CSS nesting follows the same semantics as Sass nesting. The & symbol refers to the parent selector. Without &, the nested rule becomes a descendant selector.

Gotchas

  • Nesting without & (bare element selectors like img inside .card {}) is valid in the latest spec but had a rocky browser rollout — use & img { } for maximum compatibility.
  • Specificity of nested selectors is calculated the same as if they were un-nested.
  • CSS nesting does not support the parent-referencing patterns like &__element (BEM) without a full class — you must write the full class name.
  • Browser support: Chrome 112+, Firefox 117+, Safari 17+.

Context

Modern CSS projects that want to reduce selector repetition without a build step.

Revisions (0)

No revisions yet.