Modern CSS Patterns
CSS features that have changed how I build interfaces—container queries, :has(), and more.
cssfrontenddesign

CSS has evolved dramatically. Features that once needed JavaScript are now native. Here's what I use daily.
Container Queries
Components that respond to their container, not just the viewport:
css
.card-container {
container-type: inline-size;
}
.card {
display: grid;
gap: 1rem;
}
@container (min-width: 400px) {
.card {
grid-template-columns: 150px 1fr;
}
}Game-changing for reusable components.
The :has() Selector
The parent selector we've always wanted:
css
/* Card with image gets different layout */
.card:has(img) {
grid-template-rows: 200px 1fr;
}
/* Label changes when input is focused */
.field:has(input:focus) label {
color: var(--accent);
}
/* Hide empty sections */
section:not(:has(*)) {
display: none;
}CSS Variables for Theming
css
:root {
--bg: #ffffff;
--text: #1a1a1a;
--accent: #6366f1;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #09090b;
--text: #fafafa;
}
}
.button {
background: var(--accent);
color: white;
}Theme switching without JavaScript.
Grid for Page Layout
css
.page {
display: grid;
grid-template-columns: 1fr min(65ch, 100%) 1fr;
min-height: 100vh;
}
.page > * {
grid-column: 2;
}
/* Full-width breakout */
.page > .wide {
grid-column: 1 / -1;
}Logical Properties
Write CSS that works in any writing direction:
css
.sidebar {
margin-inline-start: 2rem; /* not margin-left */
padding-block: 1rem; /* not padding-top/bottom */
border-inline-end: 1px solid var(--border);
}Aspect Ratio
No more padding hacks:
css
.video {
aspect-ratio: 16 / 9;
width: 100%;
}
.avatar {
aspect-ratio: 1;
border-radius: 50%;
}Modern CSS is powerful enough that I rarely reach for CSS-in-JS anymore. The platform caught up.