A lot of the JavaScript on older websites exists only because CSS couldn’t do something in 2016. Resize listeners to change a card layout. A script to toggle a class on a parent. Padding hacks to keep a video at 16:9. Modern CSS tricks now replace every one of them.
CSS can do all of that now, natively. The catch is that tutorials written five years ago still rank well, so many developers have never seen the newer properties.
Here are 14 that are worth knowing in late 2026. We loaded every snippet into a test page in headless Chrome 156 and read the computed styles back to confirm each one does what we say. For browser support we went by MDN’s Baseline labels, which track Chrome, Edge, Firefox and Safari on desktop and mobile.
Layout
1. CSS clamp() for fluid font sizes
h1 {
font-size: clamp(1.75rem, 1rem + 3vw, 3rem);
}
Three values: minimum, preferred, maximum. The heading grows with the screen but never goes below 28px or above 48px. In our test it came out at exactly 28px on a 375px phone viewport and 48px on a 1280px laptop, with no media queries at all. Mixing rem into the middle value means it still respects the user’s browser font size setting. MDN: clamp().
2. A responsive grid in one line
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 16px;
}
Columns are at least 200px wide and share leftover space. In a 700px container we got three columns of about 223px, and the fourth card wrapped to a new row. On a phone it drops to one column on its own.
auto-fit or auto-fill? They only differ when there are fewer items than would fill a row. We put a single card in the same 700px grid: with auto-fit it stretched to the full 700px, and with auto-fill it stayed about 233px wide, with empty column slots kept beside it. Pick auto-fill if a lone card stretched across the page would look odd.
3. gap works in flexbox too
People still write margin-right on every child plus :last-child { margin-right: 0 }. Just put gap: 16px on the flex container. gap has worked in flex layouts across all major browsers for years.
4. Centre anything in two lines
.center {
display: grid;
place-items: center;
}
5. aspect-ratio instead of the padding hack
.thumb {
width: 300px;
aspect-ratio: 16 / 9;
}
Chrome computed the height as 168.75px, which is 300 × 9 ÷ 16. Add object-fit: cover on images so they fill the box without stretching. MDN: aspect-ratio.
Common mistake: setting a width, a height and an aspect ratio together. When both sizes are fixed, the ratio has nothing left to decide and is ignored. Our test box with width: 300px, height: 300px and aspect-ratio: 16 / 9 rendered as a 300 × 300 square. Set one dimension and let the ratio work out the other.
6. inset: 0 for full overlays
.overlay {
position: absolute;
inset: 0; /* top, right, bottom and left all 0 */
}
7. 100dvh for mobile full-screen sections
.hero {
min-height: 100dvh;
}
On phones, 100vh ignores the browser’s address bar, so a “full-screen” hero gets cut off at the bottom. The dynamic viewport unit dvh tracks the visible area as the toolbar shows and hides.
When not to use it: because dvh changes as the toolbar moves, the section can resize while someone scrolls, and content under it may shift. If that jump bothers you, svh (the small viewport height) stays fixed at the size with the toolbar showing. Chrome reported support for both svh and lvh in our test.

Selectors and components
8. The CSS :has selector (a parent selector at last)
/* cards with an image lose their padding */
.card:has(img) { padding: 0; }
/* dim the button while any required field is invalid */
form:has(input:invalid) button { opacity: 0.5; }
For years “style a parent based on its children” needed JavaScript. In our test the card with an image computed to 0px padding while its image-free sibling kept 16px, and the button sat at 0.5 opacity while the required input was empty. MDN marks :has() as widely available, in all major browsers since December 2023.
9. CSS container queries
.wrap { container-type: inline-size; }
.item { display: block; }
@container (min-width: 500px) {
.item { display: flex; gap: 16px; }
}
Media queries ask “how wide is the screen?”. Container queries ask “how wide is my box?”. The same card can stack in a narrow sidebar and sit side-by-side in the main column. We put one wrapper at 600px and one at 400px: the first item became flex, the second stayed block.
The step people miss is container-type. Without it on a parent, the @container rule silently never matches.
10. Native nesting
.nav {
a { color: red; }
&.active { font-weight: 700; }
}
The Sass habit, now in plain CSS. A bare a inside means .nav a; &.active means the nav itself when it also has the active class. Both rules applied in our test. MDN’s nesting guide covers the edge cases.
Polish
11. Balanced headlines
h1, h2, h3 { text-wrap: balance; }
No more two-line headings with one lonely word on the second line. The browser evens out the line lengths. According to MDN, it only kicks in for short blocks (six lines or fewer in Chromium, ten in Firefox), so use it on headings and captions, not paragraphs. Browsers that don’t support it just wrap normally, which makes it safe to add today.
12. Dark mode colours with light-dark()
:root { color-scheme: light dark; }
.box {
color: light-dark(#111, #eee);
}
One line instead of a separate prefers-color-scheme block. We switched the test page to dark mode and the colour changed from rgb(17, 17, 17) to rgb(238, 238, 238). It does nothing unless color-scheme is set, a requirement MDN spells out.
To test both themes without touching your system settings, use the color scheme emulation in Chrome’s Rendering tab, one of the Chrome DevTools hidden features worth knowing.
13. Tints without a colour picker: color-mix()
.box {
background: color-mix(in srgb, #0a84ff 20%, white);
}
A 20% tint of your brand blue. Combine it with a custom property (var(--brand)) and every hover and background shade follows when the brand colour changes. MDN: color-mix().
14. Brand-coloured checkboxes with accent-color
input[type="checkbox"],
input[type="radio"] {
accent-color: #0a84ff;
}
Recolours native checkboxes, radio buttons, range sliders and progress bars, and keeps their built-in keyboard and screen-reader behaviour. That’s a better deal than rebuilding a checkbox from a div.
A real example: one card, three places
Picture a blog card that has to appear in a wide main column, a narrow sidebar and a related-posts strip. The old approach meant three CSS variants or a script that measured each slot.
With the tricks above it’s one component. Container queries decide whether the image sits beside the text or above it, based on the slot’s width. :has(img) removes the padding only on cards that actually have an image. clamp() keeps the title readable at every size, and text-wrap: balance stops it ending on one stray word. No JavaScript, and the card works wherever someone drops it.
The catch: “supported” has two levels, and most CSS roundups blur them. MDN labels a feature newly available once it works in the latest version of every major browser, and widely available only after at least 2.5 years of support. light-dark() is still “newly available” (since May 2024), and field-sizing: content, which makes a textarea grow with its text, only reached that level in June 2026. If your visitors include older iPhones that never got updated, a newly available feature can quietly fail for them. Check the label, and keep a fallback for anything that isn’t widely available yet.
Which modern CSS tricks to use today
Safe everywhere, use now: clamp(), gap, aspect-ratio, inset, the auto-fit grid, :has() and container queries. These replace real JavaScript and real hacks.
Use with a fallback: light-dark() and field-sizing. Write the plain property first, then the new one on the next line, and older browsers will skip the line they don’t understand.
And text-wrap: balance is the easiest win of the lot. One line, no downside.
If you’re building pages with an AI assistant, these are worth naming in your prompt (“use container queries, not media queries, for the card”). Models often default to older patterns they saw more of in training. Our AI prompts for coding have a template for this, and the JavaScript one-liners post covers the scripting side.
Next: more small habits that save hours, in 15 Python tricks for beginners.
FAQ
Can I use :has() in production?
Yes. MDN lists it as widely available. If you must support very old browsers, put it inside :is() or :where(), since an unsupported :has() elsewhere makes the whole rule fail.
Do container queries replace media queries?
Not fully. Use container queries for components that appear in different-sized slots, and media queries for page-level layout and for user preferences such as reduced motion.
Do I still need Sass?
Less than before. Native nesting and custom properties cover the two features most people used Sass for. Sass still has mixins, loops and build-time maths that CSS doesn’t.
How do I check if a browser supports a CSS feature?
Look up the property on MDN and check its Baseline label and compatibility table. In code, @supports (text-wrap: balance) { ... } applies rules only where they work.