Vanta Logo
SPONSOR
Automate SOC 2 & ISO 27001 compliance with Vanta. Get $1,000 off.
Up to date
Published
3 min read

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

CSS Supports Nesting Now

CSS nesting is finally supported in all major browsers. Write cleaner, organized stylesheets without Sass or Less

Writing CSS used to mean lots of repetition. For years, we had to write long selectors to style nested elements. Now, native CSS nesting changes everything.

I used SCSS for years just to get nesting in my CSS. But now browsers support nesting right out of the box. No build tools needed. Let me show you how it works.

Remember writing CSS like this?

Now we can write it like this:

The nested version is more concise and shows the relationship between elements more clearly. The indentation reflects the DOM structure.

CSS nesting works with any valid selector. Here’s how you can nest different types:

The & symbol represents the parent selector. It’s particularly useful for modifiers and pseudo-classes:

You can nest media queries too, keeping related styles together:


Browser Support and Fallbacks

As of late 2023, CSS nesting is supported in all major browsers. Still, if you need to support older browsers, you have options:

  1. Use @supports to provide fallbacks
  2. Use a CSS preprocessor
  3. Use PostCSS with the nesting plugin

Here’s how you can use @supports to provide a fallback:


Best Practices

While nesting is powerful, it’s important to use it judiciously:

  1. Avoid deep nesting (more than 3 levels) as it can make styles hard to maintain
  2. Use BEM or similar naming conventions to keep specificity in check
  3. Consider the output CSS and performance implications
  4. Keep selectors as shallow as possible

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.


More Articles You Might Enjoy

If you enjoyed this article, you might find these related pieces interesting as well. If you like what I have to say, please check out the sponsors who are supporting me. Much appreciated!

Webdev
4 min read

LH and RLH: The CSS Units That Make Vertical Spacing Easy

Exploring new CSS line-height units that eliminate guesswork from vertical rhythm

Dec 3, 2024
Read article
Webdev
3 min read

Form Validation That Doesn't Annoy Users: CSS :user-valid and :user-invalid

The new pseudo-classes :user-valid and :user-invalid give us a smarter way to style form validation states based on user interaction

Dec 12, 2024
Read article
Webdev
3 min read

CSS @supports: Write Future-Proof CSS

Detect CSS feature support and provide smart fallbacks with @supports

Dec 6, 2024
Read article
Webdev
6 min read

Micro Frontends: The LEGO Approach to Web Development

Explore the concept of micro frontends in web development, understand their benefits, and learn when this architectural approach is most effective for building scalable applications.

Oct 2, 2024
Read article
Webdev
3 min read

CVE-2025-29927 - Next.js Middleware Bypass Explained In Simple Terms

The vulnerability skips Next.js middleware security checks by adding a single HTTP header

Apr 6, 2025
Read article
Webdev
12 min read

Frontend Security Checklist

Tips for Keeping All Frontend Applications Secure

Jul 30, 2024
Read article
Webdev
13 min read

10 Essential Terminal Commands Every Developer Should Know

List of useful Unix terminal commands to boost your productivity. Here are some of my favorites.

Aug 21, 2024
Read article
Webdev
8 min read

Become a Web Developer in 180 Days

A comprehensive roadmap to becoming a proficient web developer

Oct 29, 2019
Read article
Webdev
5 min read

WebAssembly (Wasm): When (and When Not) to Use It

Understanding the real use cases for WebAssembly beyond the performance hype

Nov 25, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/css-supports-nesting-now. It was written by a human and polished using grammar tools for clarity.