Start with the layout that must work everywhere
Modern CSS features are safe to use when the baseline experience is intentionally built first. I usually structure components in three layers: readable HTML and normal document flow, a widely supported visual layout, and a small enhancement layer guarded by @supports. The enhanced layer should add precision, not carry essential content.

A useful question is: what happens if the new feature is ignored entirely? If the answer is “content disappears,” “events become unreachable,” or “rows overlap,” keep that behavior in the fallback. If the answer is merely “edges are less perfectly aligned,” enhancement is appropriate.

Grid: use the older layout as the floor, not as an imitation
Grid now has strong support, but components still need to survive partial implementations, embedded web views, and long-lived enterprise browsers. Start with flexbox or block flow, then add Grid only where two-dimensional placement matters.
.card-grid {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(min(260px, 100%), 1fr));
}
@supports not (display: grid) {
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card-grid > * {
flex: 1 1 260px;
}
}
Do not spend effort rebuilding every Grid feature in floats. Flex wrapping gives a usable card wall; Grid adds equal tracks and better placement.
| Baseline | Enhancement condition | Effect not worth faking | Maintenance risk |
|---|---|---|---|
| Block or flex wrapping | @supports (display: grid) | True two-dimensional track alignment | Keeping two spacing systems synchronized |
Failure modes to catch
- Fixed heights with Grid children cause overflow when translated content grows.
- Auto-fit grids can create too many tiny columns in narrow embedded views.
- Absolutely positioned Grid children are removed from track sizing.
Subgrid: align nested components without leaking layout assumptions
Subgrid is useful when a card component sits inside a parent grid and its header, body, and button must line up across cards. The DOM constraint matters: the subgrid element must be a grid item, and its children become aligned to the parent’s tracks.
<div class="product-grid">
<article class="product-card">
<h2>Title</h2>
<p>Description</p>
<button>Buy</button>
</article>
</div>
<style>
.product-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
.product-card {
display: grid;
grid-template-rows: auto 1fr auto;
gap: .75rem;
}
@supports (grid-template-rows: subgrid) {
.product-card {
grid-row: span 3;
grid-template-rows: subgrid;
}
}
</style>
Without subgrid, each card manages its own three rows. That is the fallback. With subgrid, all cards share the parent row tracks. If a title wraps to three lines, every card’s body and button line up.
Keep the wrapper structure stable. If a design-system card inserts an extra wrapper, the header, paragraph, and button are no longer direct subgrid participants. Also test long names, localized labels, and empty descriptions; shared rows can make one card’s content affect the visual rhythm of every card.
| Baseline | Enhancement condition | Effect not worth faking | Maintenance risk |
|---|---|---|---|
| Independent card rows with internal spacing | @supports (grid-template-rows: subgrid) | Nested children aligned on shared parent tracks | Extra DOM wrappers silently break participation |
Container queries: component-level responsive behavior
Container queries should be enabled progressively. Put container-type inside the support test; otherwise unsupported browsers still receive a containment context they cannot use, which can surprise scrolling and sizing behavior.
.media {
display: flex;
gap: 1rem;
flex-direction: column;
}
@supports (container-type: inline-size) {
.media-wrap {
container-type: inline-size;
}
@container (min-width: 32rem) {
.media {
flex-direction: row;
align-items: center;
}
.media__image {
width: 12rem;
flex: 0 0 auto;
}
}
}
The baseline is a stacked component. The enhanced version responds to its actual container, so the same component can work in a narrow sidebar and a wide main column without viewport hacks.
| Baseline | Enhancement condition | Effect not worth faking | Maintenance risk |
|---|---|---|---|
| Viewport-independent stacked or flex layout | @supports (container-type: inline-size) | Styling based on component width rather than screen width | Forgetting that containment changes sizing context |
anchor(): enhance a working popover, don’t make positioning a dependency
Anchor positioning is the easiest place to overreach. The accessible popover must first work as normal flow or as a conventional absolute/fixed overlay. Anchor positioning can then improve placement, collision behavior, and scroll tracking.
Baseline: clickable, closable, scroll-safe overlay
<div class="menu-field">
<button id="menuBtn" popovertarget="menuPanel" aria-expanded="false" aria-controls="menuPanel">
Share
</button>
<div id="menuPanel" class="menu-panel" popover="manual">
<a href="#copy">Copy link</a>
<a href="#email">Email</a>
<button type="button" id="closeMenu">Close</button>
</div>
</div>
<style>
.menu-field {
position: relative;
display: inline-block;
}
.menu-panel {
position: absolute;
top: calc(100% + .5rem);
left: 0;
z-index: 1000;
min-width: 10rem;
background: #fff;
border: 1px solid #cbd5e1;
border-radius: .75rem;
padding: .5rem;
box-shadow: 0 1rem 2rem rgb(15 23 42 / .18);
}
.menu-panel > a,
.menu-panel > button {
display: block;
width: 100%;
padding: .5rem .625rem;
text-align: left;
}
</style>
For browsers without Popover API support, include a small toggle that adds a visible class:
const btn = document.querySelector('#menuBtn');
const panel = document.querySelector('#menuPanel');
const close = document.querySelector('#closeMenu');
function showPanel() {
if (typeof panel.showPopover === 'function') {
panel.showPopover();
} else {
panel.classList.add('is-open');
}
btn.setAttribute('aria-expanded', 'true');
}
function hidePanel() {
if (typeof panel.hidePopover === 'function') {
panel.hidePopover();
} else {
panel.classList.remove('is-open');
}
btn.setAttribute('aria-expanded', 'false');
}
btn.addEventListener('click', () => {
const open = btn.getAttribute('aria-expanded') === 'true';
open ? hidePanel() : showPanel();
});
close.addEventListener('click', hidePanel);
Add the fallback class only when the API is absent:
@supports not selector(:popover-open) {
.menu-panel:not(.is-open) {
display: none;
}
}
Enhancement: anchor positioning
@supports (anchor-name: --menu-button) {
#menuBtn {
anchor-name: --menu-button;
}
#menuPanel:popover-open,
#menuPanel.is-open {
position: fixed;
top: auto;
left: auto;
position-anchor: --menu-button;
inset-area: bottom left;
margin-top: .5rem;
}
}
/* Engines that support the anchor() function directly */
@supports (top: anchor(bottom)) {
#menuPanel:popover-open,
#menuPanel.is-open {
position: fixed;
top: anchor(bottom);
left: anchor(start);
}
}
Prefer checking the property or function you actually use. anchor-name, position-anchor, and top: anchor(bottom) are not interchangeable signals. If a browser recognizes a parser token but does not implement the full positioning model, your component still needs the JavaScript-managed class fallback.
Placement details that matter
- Click position: anchor the panel to the trigger, not the pointer, unless the UI is a context menu. Pointer coordinates make keyboard activation ambiguous.
- Scroll containers:
position: fixedtracks the anchor better across ancestor scroll in supporting engines, but test transformed or filtered ancestors; they can become containing blocks. - Space不足: add a max-height and overflow rule. Do not assume the viewport edge will solve it:
.menu-panel { max-height: min(20rem, 80vh); overflow: auto; }. - Popover support: keep the manual class toggle, focus return, Escape handling, and outside-click behavior in JavaScript for older engines.
| Baseline | Enhancement condition | Effect not worth faking | Maintenance risk |
|---|---|---|---|
| Document flow, absolute dropdown, or fixed overlay with JS state | @supports (anchor-name: --x) or @supports (top: anchor(bottom)) | Overlay remains tied to trigger through layout and scroll changes | Partial browser implementations and differing syntax support |
Executable testing checklist
- Test current Chrome, Firefox, and Safari, plus one older Safari and one embedded WebView.
- Disable browser experimental flags before signing off; do not ship based on flag-only behavior.
- In DevTools, inspect
@supportsblocks. Add temporary outlines to prove which branch rendered. - Test 320px widths, 200% text zoom, long unbroken strings, and translated content.
- For Grid and Subgrid, verify that no card overlaps when one item has three extra lines.
- For containers, render the same component in a narrow rail, wide article, and dialog.
- For anchored panels, test opening by keyboard, clicking trigger again, Escape, page scroll, inner scroll, viewport corners, and
prefers-reduced-motion. - Run a DOM mutation test: insert an extra wrapper around card or popover content and confirm the fallback still works.
The maintainable pattern is not “new CSS vs. old CSS.” It is a boring baseline that survives failure, plus a narrowly scoped enhancement that can be deleted without breaking the component when support changes again.