Modern CSS Features
CSS has grown enormously in recent years. Features that once needed JavaScript or a preprocessor, such as fluid sizes, parent selection, nesting and component-level responsiveness, are now built into every major browser. This page tours the most useful ones.
Fluid sizes with clamp(), min() and max()
clamp(minimum, preferred, maximum) picks the preferred value but never goes below the minimum or above the maximum. It's perfect for headings that grow with the screen:
h1 {
font-size: clamp(1.75rem, 1rem + 4vw, 3.5rem);
}
On a phone the heading is 1.75rem (28px); it grows smoothly as the viewport widens and stops at 3.5rem (56px) on large screens, with no media query. Mixing a rem value into the middle term means the heading still responds when users zoom. min() and max() return the smaller or larger of their arguments: width: min(100%, 40rem) is "full width, but no more than 40rem".
:has(), the parent selector
For years CSS could only select downwards. :has() selects an element based on what it contains or what follows it:
/* cards that contain an image get a different layout */
.card:has(img) {
display: grid;
grid-template-columns: 120px 1fr;
}
/* highlight a form field's wrapper when its input is invalid */
.field:has(input:user-invalid) {
border-color: crimson;
}
/* a heading directly followed by a paragraph */
h2:has(+ p) {
margin-bottom: 0.25rem;
}
Think of A:has(B) as "an A that has a B inside it" (or, with + or ~, "an A that has a B after it").
Native nesting
You can now nest rules inside other rules, which keeps related styles together, just as Sass users have done for years. The & stands for the parent selector.
.card {
padding: 1rem;
border-radius: 8px;
h3 {
margin-top: 0; /* .card h3 */
}
&:hover {
box-shadow: 0 4px 12px rgb(0 0 0 / 15%); /* .card:hover */
}
&.featured {
border: 2px solid gold; /* .card.featured */
}
@media (min-width: 40rem) {
padding: 2rem; /* .card, on wider screens */
}
}
Use & when the nested part must attach directly to the parent (&:hover, &.featured); without it, the nested selector is treated as a descendant. Don't nest more than two or three levels deep, or specificity and readability suffer.
Container queries
Media queries respond to the size of the whole viewport. But a card component might sit in a narrow sidebar on one page and a wide main column on another. Container queries let a component respond to the size of its container instead.
.card-wrapper {
container-type: inline-size; /* make this a size container */
}
.card {
display: grid;
gap: 1rem;
}
@container (min-width: 350px) {
.card {
grid-template-columns: 120px 1fr; /* image beside text */
}
}
container-type: inline-size tells the browser to track the wrapper's width. The @container rule then applies to cards inside any wrapper that's at least 350px wide. Give containers names with container-name: sidebar; and query them with @container sidebar (min-width: 350px). Container units such as cqi (1% of the container's inline size) work like viewport units but relative to the container.
More modern conveniences
| Feature | Example | What it does |
|---|---|---|
| Logical properties | margin-inline: auto; padding-block: 2rem; | Sides that follow the writing direction (inline = along the text, block = down the page) |
aspect-ratio | aspect-ratio: 16 / 9; | Keeps a box (such as a video embed) at a fixed shape as it resizes |
:is() / :where() | :is(header, footer) a | Shorter selector lists; :where() adds zero specificity |
gap in Flexbox | gap: 1rem; | Spacing between flex items without margin hacks |
accent-color | accent-color: #15803d; | Recolours checkboxes, radio buttons and range sliders |
text-wrap: balance | on headings | Evens out line lengths so headings don't leave one lonely word |
color-mix() | color-mix(in srgb, var(--brand) 20%, white) | Creates tints and shades from existing colours |
@layer | @layer base, components; | Orders groups of styles regardless of specificity (see Specificity) |
Before using a very new feature, check its support on a site such as caniuse.com or on MDN's compatibility tables. Everything on this page works in current versions of Chrome, Edge, Firefox and Safari, but people with old phones that no longer get updates may run older browsers. Design so the page still works (even if it looks plainer) when a feature isn't supported.
Try it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Modern CSS</title>
<style>
body {
font-family: system-ui, sans-serif;
margin: 1rem;
display: grid;
grid-template-columns: 1fr 2fr;
gap: 1rem;
}
h1 {
grid-column: 1 / -1;
font-size: clamp(1.5rem, 1rem + 3vw, 3rem);
text-wrap: balance;
}
.slot {
container-type: inline-size;
background: #f1f5f9;
padding: 0.5rem;
border-radius: 8px;
}
.card {
display: grid;
gap: 0.75rem;
background: white;
padding: 0.75rem;
border-radius: 8px;
.thumb {
aspect-ratio: 4 / 3;
background: linear-gradient(135deg, #1e3a8a, #0f766e);
border-radius: 6px;
}
&:has(.badge) {
outline: 3px solid #f59e0b;
}
}
@container (min-width: 300px) {
.card { grid-template-columns: 120px 1fr; }
}
.badge { background: #fde68a; padding: 0.1rem 0.5rem; border-radius: 999px; }
</style>
</head>
<body>
<h1>Same card, two containers, one stylesheet</h1>
<div class="slot">
<div class="card"><div class="thumb"></div><p>Narrow slot: stacked.</p></div>
</div>
<div class="slot">
<div class="card"><div class="thumb"></div><p>Wide slot: side by side. <span class="badge">New</span></p></div>
</div>
</body>
</html>
The heading spans the full width and its size scales with the preview. Below it, the left (narrow) slot shows the card stacked, with the gradient thumbnail above the text, while the right (wider) slot shows the same card with the thumbnail beside the text, as long as that slot is at least 300px wide (on a very wide screen the left slot can pass 300px too). Only the second card has an amber outline, because only it :has() a badge. Make the preview narrow and both cards stack.
Key points
clamp(),min()andmax()create fluid sizes without media queries.:has()styles an element based on its contents or following siblings.- Native nesting groups related rules;
&refers to the parent selector. - Container queries make components respond to their container rather than the screen.
Tip: use the ← and → keys to move between pages.
