Native CSS Nesting
suggest changeCSS can nest related style rules without a preprocessor:
.card {
padding: 1rem;
border: 1px solid light-dark(#ddd, #444);
border-radius: 0.75rem;
& > h2 {
margin-block-start: 0;
}
&:is(:hover, :focus-within) {
border-color: currentColor;
}
@media (width >= 48rem) {
padding: 1.5rem;
}
}
The & nesting selector represents the parent selector. Use it when the nested selector is attached to the parent, such as &:hover, or when it makes the relationship clearer.
Keep nesting shallow. Deeply nested selectors remain difficult to reuse and can acquire more specificity than intended. Native nesting changes syntax, not the cascade.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents