CSS Nesting is a modern enhancement to CSS that allows developers to write more structured and readable styles by nesting selectors within one another, similar to preprocessor syntaxes like SCSS. This feature improves maintainability and reduces redundancy, making CSS easier to manage.
CSS Nesting has long been a desired feature, inspired by preprocessors such as Sass and Less. However, it was only officially introduced in the CSS Specification as part of CSS Nesting Module Level 1.
- 2013-2016: CSS Working Group discussed native nesting but faced challenges in syntax and parsing.
- 2021: Initial proposals were refined based on feedback from browser vendors and the CSS community.
- 2023: CSS Nesting gained widespread support as browser vendors implemented it.
- December 2023: Full cross-browser support was achieved across major browsers (Chrome, Firefox, Safari, and Edge).
CSS Nesting allows developers to write styles within a parent selector without repeating the parent. This simplifies the code and provides a more intuitive way to structure styles.
Hereβs an example of how CSS Nesting works:
nav {
background: #333;
color: white;
padding: 1rem;
ul {
list-style: none;
margin: 0;
padding: 0;
li {
display: inline;
margin-right: 10px;
a {
color: white;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
}
}
}
/* Without Nesting */
.card {
background: #fff;
}
.card:hover {
background: #f0f0f0;
}
.card .title {
font-size: 1.5rem;
}
/* With Nesting */
.card {
background: #fff;
&:hover {
background: #f0f0f0;
}
.title {
font-size: 1.5rem;
}
}
CSS Nesting also supports at-rules within nested blocks. Hereβs a more detailed example:
.container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
padding: 20px;
.item {
background: lightgray;
padding: 10px;
text-align: center;
@media (width < 600px) {
grid-column: span 2;
background: darkgray;
}
}
}
CSS Nesting also supports at-rules within nested blocks.
.button {
color: white;
background: blue;
@media (width < 600px) {
background: red;
}
}
As of December 2023, all major browsers support CSS Nesting:
- π’β Chrome 112+
- π β Firefox 117+
- π΅β Safari 16.5+
- π£β Edge 112+
- Improved Readability: Reduces the need to repeat selectors, making styles more concise.
- Better Maintainability: Easier to manage styles for complex components.
- Native Performance: Eliminates the need for preprocessors, allowing native browser optimizations.
- Nesting is Implicit: All major browsers support nesting without requiring an explicit
&
or another nested selector as of December 2024. - Avoid Over-Nesting: Excessive nesting can reduce clarity and make styles harder to override.
- Use Responsibly: Nesting should complement, not replace, a structured CSS methodology.
For further reading and verification, refer to these reliable sources:
-
MDN Web Docs β CSS Nesting
https://developer.mozilla.org/en-US/docs/Web/CSS/Nesting -
CSSWG (CSS Working Group) β CSS Nesting Module Level 1
https://www.w3.org/TR/css-nesting-1/ -
Chrome Developers Blog β Native CSS Nesting
https://developer.chrome.com/blog/css-nesting/
CSS Nesting is a game-changing feature that simplifies writing and maintaining styles. With full cross-browser support since December 2023, it is now a viable alternative to preprocessors for many use cases. By following best practices, developers can harness its power while keeping styles clean and manageable.