Native CSS Nesting

Write cleaner, more maintainable stylesheets with native CSS nesting--supported by all major browsers since 2023. Learn the syntax, best practices, and how it compares to preprocessors like SCSS.

What Is Native CSS Nesting?

CSS nesting is a native feature of the CSS language that allows you to nest selectors within one another, creating a hierarchical structure that mirrors your HTML document. When you nest selectors, the browser automatically creates the appropriate descendant combinator between them, reducing the repetitive writing of parent selectors.

Unlike preprocessor nesting, which is compiled away before the code reaches the browser, native CSS nesting is parsed directly by the browser's rendering engine. This means no build step is required, and the CSS you write is the CSS that executes.

For teams practicing modern web development, native CSS nesting aligns perfectly with the goal of reducing build complexity while maintaining clean, maintainable code.

Browser Support and Compatibility

Native CSS nesting has achieved broad browser support across all major engines:

  • Chrome/Edge: Supported since version 112 (April 2023)
  • Firefox: Supported since version 117 (August 2023)
  • Safari: Supported since version 16.5 (May 2023)

For projects requiring support for older browsers, PostCSS with the postcss-nesting plugin can provide a compilation step.

Native CSS Nesting Adoption

3

Major browsers with native support

2023

Year native nesting landed

0

Build steps required

100%

Native browser parsing

Basic Syntax and How It Works

The basic syntax of CSS nesting involves writing child selectors inside a parent selector's block using curly braces. The browser automatically handles the relationship between the parent and child selectors.

Simple Nesting Example

.parent {
 color: blue;

 .child {
 font-size: 16px;
 }
}

The browser parses this as two separate rules:

.parent {
 color: blue;
}

.parent .child {
 font-size: 16px;
}

Without explicit use of the & nesting selector, nested selectors are automatically combined with a descendant combinator (space).

The & Nesting Selector

The & nesting selector is a powerful tool that explicitly references the parent selector. It becomes essential when you need to create compound selectors, use pseudo-classes, or attach selectors without the automatic whitespace.

When to Use &

The & selector is required when you want to:

  1. Attach pseudo-classes: Create hover, focus, or other state-based styles
  2. Create compound selectors: Combine parent with additional classes or attributes
  3. Use pseudo-elements: Style ::before, ::after, and other pseudo-elements
  4. Apply combinators: Use +, >, or ~ selectors relative to the parent

Required & Usage Examples

/* Without & - creates descendant selector (WRONG) */
.button {
 background: navy;
 :hover { background: darkblue; }
}

/* With & - creates compound selector (CORRECT) */
.button {
 background: navy;
 &:hover { background: darkblue; }
}

Compound Selectors and Combinators

The & selector enables sophisticated selector patterns:

.card {
 /* Compound class selector */
 &.featured { border: 2px solid gold; }

 /* Pseudo-element */
 &::before { content: "★"; }

 /* Adjacent sibling */
 & + .card { margin-top: 1rem; }

 /* Child combinator */
 > .title { font-size: 1.5rem; }

 /* Attribute selector */
 &[disabled] { opacity: 0.5; }
}

Reversing Parent Context

A powerful pattern involves placing & in a different position to reverse the selector context:

.card {
 background: white;

 /* Creates: .featured .card */
 .featured & { border-color: gold; }
}
CSS Nesting vs SCSS/SASS Preprocessors
FeatureSCSS/SASSNative CSS
Variables$primary-color--primary-color
Mixins@mixin supportedNot supported
FunctionsColor manipulation, mathcalc(), min(), max()
Loops@for, @eachNot supported
Partials_partial.scss files@import (less efficient)
Build stepRequiredNot required
Extends@extend supportedNot supported

CSS Nesting vs SCSS/SASS Preprocessors

Can Native CSS Nesting Replace Preprocessors?

Native CSS nesting is sufficient when:

  • Your project primarily needs selector nesting and organization
  • You're using CSS custom properties for theming
  • You're not heavily dependent on mixins or complex functions
  • Build simplicity is a priority
  • You're using utility-first frameworks like Tailwind CSS

Preprocessors may still be beneficial when:

  • Your project has extensive use of mixins for reusable style patterns
  • You need complex color manipulation or math functions
  • You rely on loops for generating repetitive styles
  • You use partials for modular file organization
  • Your team has established SCSS patterns and expertise

For teams building modern web applications with streamlined tooling, our web development services can help you evaluate the best approach for your CSS architecture.

Practical Code Examples

Card Component with Hover Effects

.card {
 background: white;
 border-radius: 8px;
 box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
 padding: 16px;
 transition: transform 0.2s ease;

 &:hover {
 transform: translateY(-4px);
 box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
 }

 .title {
 font-size: 1.5rem;
 color: #333;
 margin-bottom: 8px;
 }

 .button {
 background: #007bff;
 color: white;
 padding: 8px 16px;
 border-radius: 4px;

 &:hover { background: #0056b3; }
 &:focus { outline: 2px solid #007bff; outline-offset: 2px; }
 }

 @media (max-width: 600px) {
 padding: 12px;
 .title { font-size: 1.25rem; }
 }
}

Navigation Menu with Responsive Design

.nav {
 display: flex;
 gap: 16px;
 padding: 16px;
 background: #f8f9fa;

 .nav-item {
 text-decoration: none;
 color: #333;
 font-weight: 500;
 padding: 8px 12px;
 border-radius: 4px;

 &:hover {
 color: #007bff;
 background: rgba(0, 123, 255, 0.1);
 }

 &.active {
 color: #007bff;
 background: rgba(0, 123, 255, 0.15);
 border-bottom: 2px solid #007bff;
 }

 &.disabled {
 opacity: 0.5;
 pointer-events: none;
 }
 }

 @media (max-width: 768px) {
 flex-direction: column;
 gap: 8px;
 .nav-item { text-align: center; }
 }
}

Best Practices and Performance Considerations

Limit Nesting Depth

Avoid nesting more than 3-4 levels deep. Deeply nested selectors create several problems:

  1. Specificity inflation: Each nesting level increases specificity
  2. Maintenance burden: Harder to understand and modify
  3. Coupling to HTML structure: Changes in HTML require CSS updates
  4. Selector performance: Deep selectors are slower to match
/* BAD: Too deep */
.header .nav .list .item .link:hover { color: blue; }

/* BETTER: Shallow nesting */
.header-nav { /* Component-level styling */ }

.nav-item-link:hover { color: blue; }

Use Explicit & for Pseudo-Classes

Always use the & nesting selector when targeting pseudo-classes, pseudo-elements, or creating compound selectors:

.button {
 background: #007bff;
 &:hover { background: #0056b3; }
 &:focus { outline: 2px solid #007bff; outline-offset: 2px; }
 &::before { content: ""; }
 &.primary { background: #0056b3; }
}

Advanced Patterns

Nesting with @media Queries

.card {
 width: 100%;

 @media (min-width: 768px) { width: 50%; }
 @media (min-width: 1200px) { width: 33.333%; }

 .content {
 padding: 16px;
 @media (max-width: 600px) { padding: 12px; }
 }
}

Nesting with @supports

.button {
 display: inline-flex;

 @supports (gap: 8px) { gap: 8px; }

 &:hover {
 transform: translateY(-2px);
 @supports (backdrop-filter: blur(4px)) {
 backdrop-filter: blur(4px);
 }
 }
}

Migration Guide: SCSS to Native CSS

Variable Conversion

/* SCSS */
$primary-color: #007bff;
$border-radius: 8px;

.button {
 background: $primary-color;
 border-radius: $border-radius;
}
/* Native CSS */
:root {
 --primary-color: #007bff;
 --border-radius: 8px;
}

.button {
 background: var(--primary-color);
 border-radius: var(--border-radius);
}

Mixin Alternatives

Preprocessor mixins can be replaced with:

  • CSS custom properties for values
  • Utility classes for reusable styles
  • @layer for organization
  • Direct property application in components

For projects transitioning from SCSS to native CSS, our web development team can help modernize your CSS architecture while maintaining compatibility with your existing design system.

Conclusion

Native CSS nesting represents a significant advancement in the CSS language, bringing the developer experience improvements that preprocessors offered directly to the browser. With support across all major browsers since 2023, it's now a viable option for production websites.

For modern web development with Next.js and similar frameworks, native CSS nesting offers several compelling advantages:

  • Simplified build processes: No preprocessor compilation step required
  • Smaller bundle sizes: No compiled CSS overhead
  • Future-proof: Part of the CSS specification, not an abstraction layer
  • Browser-native performance: Parsed and optimized by the browser

By following the best practices outlined in this guide--limiting nesting depth, using & explicitly for pseudo-classes, and organizing styles by component--you can write maintainable, performant CSS that takes full advantage of native nesting capabilities.

When building modern web applications, clean CSS architecture contributes to better maintainability and faster iteration cycles. Our team specializes in frontend development using modern CSS techniques including native nesting, custom properties, and component-based styling.

Frequently Asked Questions

Ready to Modernize Your CSS Architecture?

Our team specializes in building performant, maintainable websites using modern CSS techniques including native nesting, custom properties, and component-based styling.