Modern CSS: Nesting, Containers and More
CSS spent a decade as a language you wrote around rather than with: no variables that computed, no nesting without a preprocessor, no way to ask how wide a component's parent was. That era ended. Native nesting, custom properties, container queries, logical properties and relational selectors are supported in every current browser, and together they change how component CSS is structured. This tour explains what each feature actually solves, where it fits in a real stylesheet, and how to introduce it without abandoning the browsers your users still run.
What changed and why it matters
The through-line of modern CSS is that stylesheets can finally express relationships that previously required JavaScript or build tooling. Nesting removes the flat-selector sprawl that made large stylesheets hard to scan. Custom properties make design tokens a runtime concept instead of a compile-time constant. Container queries let a component respond to the space it was given rather than the size of the viewport, which is what component-driven design always needed. Logical properties write direction-aware layouts in one rule set. And :has() — often called the parent selector — closes the last major gap in selector power by letting a container react to its children.
None of these features require a new workflow. They are plain CSS, parsed by the browser, styleable in dev tools, and cascade like everything else. The migration path is incremental: adopt a feature where it removes pain, keep the fallback rule above it, and move on.
Nesting without a preprocessor
Native nesting lets rules live inside other rules, using & to reference the parent selector. The browser accepts it directly — no Sass, no PostCSS step — and dev tools display the flattened result so you can still inspect exactly what was matched.
.card {
--card-bg: #ffffff;
--card-radius: 12px;
background: var(--card-bg);
border-radius: var(--card-radius);
border: 1px solid #e2e8f0;
padding: 1.25rem;
transition: box-shadow 200ms ease, transform 200ms ease;
& .card-title {
font-size: 1.25rem;
font-weight: 650;
margin-block-end: 0.5rem;
& a {
color: inherit;
text-decoration: none;
&:hover { text-decoration: underline; }
&:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 2px;
}
}
}
& .card-meta {
color: #64748b;
font-size: 0.875rem;
}
& :is(img, video) {
inline-size: 100%;
border-radius: calc(var(--card-radius) - 4px);
}
&:hover {
box-shadow: 0 10px 24px rgb(15 23 42 / 0.08);
transform: translateY(-2px);
}
@media (prefers-reduced-motion: reduce) {
transition: none;
transform: none;
}
&[data-featured="true"] {
border-color: #2563eb;
box-shadow: 0 0 0 1px #2563eb;
}
}
Two conventions keep nested CSS maintainable. Nest selectors only — bare declarations inside a nested block are valid now, but they can accidentally override the parent in confusing ways, so scoping every declaration under an explicit selector keeps intent obvious. And limit depth to two or three levels; deeper nesting is a signal that the component wants to be split rather than nested further.
Container queries change component design
For years, responsive design meant media queries keyed to viewport width. That works for page scaffolding and fails for components: the same card in a sidebar and in a main column is a different design problem, and a viewport query cannot tell the two apart. Container queries invert the question. You mark a parent as a sizing container, then write queries against the container's own inline size.
.article-grid {
display: grid;
grid-template-columns: minmax(0, 1fr) 18rem;
gap: 2rem;
align-items: start;
}
.article-card-container {
container-type: inline-size;
container-name: article-card;
}
.article-card {
display: grid;
gap: 0.75rem;
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 12px;
}
@container article-card (min-width: 26rem) {
.article-card {
grid-template-columns: 9rem 1fr;
align-items: center;
}
}
@container article-card (min-width: 38rem) {
.article-card {
grid-template-columns: 12rem 1fr;
padding: 1.25rem 1.5rem;
}
.article-card .excerpt {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
}
@container article-card (max-width: 25.99rem) {
.article-card .thumb {
display: none;
}
}
The same card markup now stacks in a narrow rail, becomes a two-column layout at medium widths and gains a clamped excerpt when space allows — with no viewport breakpoints anywhere. Keep container-type: inline-size on the sizing wrapper rather than the component root when the component also participates in the parent grid; containers establish an independent formatting context, and isolating that from the layout container avoids surprising interactions.
Logical properties for direction-aware layouts
Physical properties — margin-left, padding-top, border-right — encode a writing direction into the stylesheet. Logical properties replace them with flow-relative names: margin-inline-start, padding-block, border-inline-end, inset-inline, text-align: start. For left-to-right pages they resolve to the same pixels, and for right-to-left locales the whole layout flips without a single override rule.
.callout {
padding-block: 1rem;
padding-inline: 1.25rem;
margin-block-end: 1.5rem;
border-inline-start: 4px solid #2563eb;
background: #eff6ff;
}
.callout p {
text-align: start;
margin-block: 0.5rem;
}
.sidebar-layout {
display: grid;
grid-template-columns: 1fr 16rem;
gap: 1.5rem;
padding-inline: clamp(1rem, 4vw, 3rem);
}
.sidebar-layout .content { min-inline-size: 0; }
.sidebar-layout .rail { inset-inline-end: 0; }
Migrate gradually: physical properties still work, so convert components as you touch them. The win compounds — every converted component becomes RTL-ready for free.
:has(), :is() and :where() in practice
The relational and functional selectors round out the modern toolkit. :is() accepts a selector list and takes the highest specificity of its arguments; :where() accepts the same list but contributes zero specificity, which makes it ideal for low-priority defaults you expect authors to override. :has() matches an element based on what it contains — the missing piece for form validation styling, empty-state layouts and media-aware cards.
:where(h1, h2, h3, h4) {
line-height: 1.2;
font-weight: 650;
text-wrap: balance;
}
:is(.btn-primary, .btn-submit) {
background: #2563eb;
color: #ffffff;
}
.form-field {
border: 1px solid #cbd5e1;
border-radius: 8px;
padding: 0.5rem 0.75rem;
}
.form-field:has(:invalid:not(:placeholder-shown)) {
border-color: #dc2626;
}
.form-field:has(:focus-visible) {
border-color: #2563eb;
box-shadow: 0 0 0 3px rgb(37 99 235 / 0.25);
}
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
gap: 1rem;
}
.gallery:has(:nth-child(1):last-child) {
grid-template-columns: minmax(0, 24rem);
}
.article:has(img.hero) { grid-column: 1 / -1; }
The :has(:nth-child(1):last-child) rule is a compact example of the new expressive power: a gallery with exactly one child widens it instead of leaving a lonely thumbnail in a corner. Write these selectors sparingly and comment them; power that is not self-evident becomes maintenance debt.
Rolling modern CSS into production
Support is no longer the barrier — evergreen browsers ship all the features above — but legacy enterprise browsers and cached old versions still exist in some audiences. Measure your real traffic with a feature-detection library or server-side user-agent data before declaring a floor. Then structure each adoption the same way: base rule with the classic behavior, feature rule after it, optional @supports guard when you want explicit control over which capability triggers the upgrade. Teams that skip the measurement step usually discover their floor the hard way — a key account emails a screenshot of a collapsed layout from a browser the team assumed was extinct. Ten minutes with analytics prevents that conversation, and it also tells you when a fallback block has become dead weight that can finally be deleted.
One more habit helps reviews: annotate each modern rule with the feature it depends on. A comment naming the capability turns a mysterious layout difference between browsers into a ten-second diagnosis for whoever touches the file next year.
| Feature | Solves | Fallback pattern | Baseline |
|---|---|---|---|
| Nesting | Flat selector sprawl, duplicated prefixes | Duplicate the flattened selectors above | Current evergreen |
| Custom properties | Tokens, theming, runtime theming | Hard-coded values or preprocessor variables | Universal |
| Container queries | Component-level responsiveness | Viewport media query with similar widths | Current evergreen |
| Logical properties | RTL and mixed-writing layouts | Keep physical properties until converted | Current evergreen |
| :has() | Parent and sibling-state styling | Small class toggled by script | Current evergreen |
| :is() / :where() | Specificity-controlled selector lists | Repeated selectors in a comma list | Current evergreen |
| text-wrap: balance | Headline typography | Browser falls back to normal wrapping | Progressive |
Modern CSS rewards a component-first mindset: tokens on :root, nested rules per component, container queries for internal layout, logical properties for direction. Together they cut the distance between design intent and stylesheet syntax — usually without adding a single line of JavaScript.
Common Mistakes
Nesting five levels deep until the selector graph is unreadable
Fix: Cap nesting at two or three levels and split components that demand more depth into their own rule blocks.
Putting container-type on the page grid instead of the component wrapper
Fix: Mark a dedicated sizing wrapper so the container query measures the component's own space, not the page layout.
Mixing logical and physical properties inside one component
Fix: Convert a component fully when you touch it — padding-inline with margin-left on the same box produces RTL bugs.
Using :is() where you meant :where() and accidentally inflating specificity
Fix: :is() takes the highest argument specificity; :where() adds zero — pick deliberately and comment the choice.
Assuming every user is on the newest browser because yours is
Fix: Check your actual traffic, keep a classic fallback rule above each modern override, and re-test the floor quarterly.
Skipping dev tools inspection after adopting nesting
Fix: Confirm the flattened rule matches your intent; unexpected & expansion is the most common nesting bug.
Key Takeaways
- Native nesting, custom properties, container queries, logical properties and :has() are all supported in current browsers and need no build step.
- Nest selectors, not bare declarations, and keep depth shallow so components stay scannable.
- Container queries let one component adapt to sidebar and main-column contexts without viewport breakpoints.
- Logical properties write direction-aware layouts once, making RTL support nearly free.
- :is() raises specificity, :where() contributes none, and :has() styles containers based on their children.
- Adopt each feature incrementally: classic base rule, modern override, measured support floor — no big-bang migration required.