Unpacking Native CSS Nesting

Giga Prakosa Hikmata
mamitech
Published in
3 min readJun 21, 2024
Photo by Didssph on Unsplash

If you’ve spent any time wrestling with CSS (and let’s be honest, who hasn’t?), you know it can get a bit chaotic. Selectors piling up, rules scattered all over the place, and it’s enough to make any developer crave for a simpler way.

Well, good news, guys! Our pleas have been heard. We now have native CSS nesting. It might sound like a bird’s cozy home, but trust me, it’s way cooler than that.

What’s the Big Deal?

In the olden days (okay, a few years ago), if you wanted to nest your CSS rules to make them more organized, you had to use a preprocessor like Sass or Less. These tools were great, but they added an extra step to your workflow. You had to compile your code before the browser could understand it.

Native CSS nesting lets you nest your rules directly in your CSS files, without needing any extra tools. It’s like finally getting that built-in cabinet organizer you’ve always wanted (everything has its place), and it’s so much easier to find what you need.

Show Me the Code!

Okay, enough talk, let’s see how this works with some examples:

Basic Nesting:

/* Without Nesting */
.card { background-color: white; padding: 20px; }
.card h2 { font-size: 24px; }
.card p { line-height: 1.6; }
.card a { color: blue; }


/* With Nesting */
.card {
background-color: white;
padding: 20px;

h2 { font-size: 24px; }
p { line-height: 1.6; }
a { color: blue; }
}

Nesting with Pseudo-classes:

/* Without Nesting */
.btn { /* ...button styles... */ }
.btn:hover { background-color: #0056b3; color: white; }
.btn:active { box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125); }


/* With Nesting */
.btn {
/* ...button styles... */

&:hover {
background-color: #0056b3;
color: white;
}

&:active {
box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
}
}

Nesting with Media Queries:

/* Without Nesting */
.container { width: 960px; margin: 0 auto; }
@media (max-width: 768px) {
.container { width: 100%; padding: 0 15px; }
}


/* With Nesting */
.container {
width: 960px;
margin: 0 auto;

@media (max-width: 768px) {
width: 100%;
padding: 0 15px;
}
}

Why You Should Embrace the Nest

  • Readability: Nesting keeps related styles together, making your code easier to read and understand (and who doesn’t love clean code?).
  • Maintainability: When you need to change something, it’s much simpler to find the relevant rules.
  • Less Repetition: You don’t have to keep typing the same parent selector over and over. (Let’s be honest, we all have better things to do.)
  • Efficiency: Native CSS nesting can even lead to slightly smaller file sizes, as you’re eliminating repetitive selectors.

A Few Caveats

  • Browser Support: Nesting is fairly new, so make sure you check which browsers support it before going all-in. (You can use tools like caniuse.com to check compatibility.)
  • Over-Nesting: Don’t go too crazy with nesting. Too many levels can make your code harder to follow. (Find the nesting level that’s “just right” for your project.)

Let’s Get Nested!

Native CSS nesting is a simple but powerful way to improve the structure and readability of your stylesheets. So, what are you waiting for? Start nesting and see the difference it makes in your workflow.

Dive into the documentation to explore more about Native CSS Nesting:

And hey, if you found this helpful, don’t forget to follow me! I like to share tips, insights, and updates about Software Engineering, especially in the exciting world of web development. Happy styling!

--

--