Modern CSS Features
Explore modern capabilities including nesting, cascade layers, :is(), :where(), :has(), color functions, scroll behavior, and progressive enhancement.
Native CSS Nesting
Modern CSS supports nesting directly in stylesheets. Nesting can improve local readability when used carefully, but excessive nesting can recreate the specificity and coupling problems associated with deeply nested preprocessors.
Keep nesting shallow and avoid using it as a substitute for clear component boundaries.
.card {
padding: 1rem;
& .title {
margin: 0;
}
&:hover {
transform: translateY(-2px);
}
}.card rule.- Native nesting reduces the need for preprocessors in many cases.
- Keep nesting shallow to avoid complexity.
What is a major benefit of native CSS nesting?
:is() and :where()
:is() groups selectors and adopts the specificity of its most specific argument. :where() also groups selectors but always has zero specificity.
:where() is particularly useful for framework or component defaults that should be easy for consumers to override.
:is(h1, h2, h3) {
line-height: 1.1;
}
:where(.card, .panel) {
border-radius: 1rem;
}:is()contributes the specificity of its most specific argument.:where()always contributes zero specificity.
What is special about the specificity of `:where()`?
:has() Relational Selector
The :has() pseudo-class allows a selector to match an element based on descendants or relative conditions. It enables patterns that historically required JavaScript or additional classes.
For example, a form field can be styled when it contains an invalid input.
.field:has(input:invalid) {
border-color: #dc2626;
}
.card:has(img) {
padding-top: 0;
}:has()enables parent-aware styling.- It can remove some JavaScript or state-class requirements.
What makes `:has()` powerful?
Progressive Enhancement
Modern CSS features should be introduced in ways that preserve a useful experience when a feature is unavailable. Feature queries using @supports allow CSS to detect support before applying an enhancement.
This is especially useful for advanced layout features, modern color functions, and experimental capabilities.
.layout {
display: flex;
}
@supports (display: grid) {
.layout {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
}@supportsenables feature-based progressive enhancement.- Provide a useful baseline before applying advanced capabilities.
Which at-rule tests whether a CSS feature is supported?