For over fifteen years, the ability to nest CSS rules inside one another was the #1 reason developers installed SASS or Less build tools. Today, Native CSS Nesting is supported natively across 100% of modern web browsers, enabling clean, hierarchical stylesheets with zero compile steps.
The ampersand (&) acts as a reference to the parent selector:
/* Card Component with Native Nesting */
.card {
background-color: #ffffff;
border-radius: 8px;
padding: 20px;
/* Nests .card-title inside .card */
.card-title {
color: #0f172a;
font-size: 20px;
margin-bottom: 8px;
}
/* Nests pseudo-class: .card:hover */
&:hover {
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}
/* Target child elements */
p {
color: #475569;
line-height: 1.6;
}
}
Instead of splitting your component styles across distant @media blocks at the bottom of the stylesheet, nest responsive queries directly inside the component:
.hero-section {
padding: 30px 15px;
@media (width >= 768px) {
padding: 80px 40px;
}
@media (width >= 1024px) {
padding: 120px 60px;
}
}
Boost your CSS and front-end development proficiency by exploring these closely connected topics:
Understand margins, borders, padding, content and the universal box-sizing fix.
Master 1D alignment, justify-content, align-items, flex-wrap and responsive flow.
Build powerful two-dimensional grid layouts with template areas and fr units.
Eliminate styling conflicts and master selector weights from 0,0,0,1 to !important.