brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: CSS @supports applies styles only when the browser understands them

Css
~/tips/tip-css-supports-feature-query

@supports (display: grid) {

Wraps CSS in a feature check, so newer features get a fallback in browsers that lack them.

New CSS features arrive faster than everyone updates their browser. With @supports you write the new version for browsers that understand it, and keep a fallback for the rest.

.layout { display: block; }

@supports (display: grid) {
  .layout { display: grid; grid-template-columns: 1fr 3fr; }
}

The rule inside only applies when the browser supports that property and value. You can combine conditions with and, or and not:

@supports (display: table-cell) and (display: list-item) { }
@supports not (transform-origin: 10em 10em 10em) { }

It also checks selectors with selector(). MDN’s example uses it for a fallback when :has() is missing:

@supports not selector(:has(a, b)) {
  /* styles for browsers without :has() */
}

That is where I find it most useful today: features like :has() that change how you write a layout, where a missing feature would leave the page broken instead of plain.

Caveats. When you mix and and or, you need parentheses around the parts; MDN warns that without them the whole rule is ignored. It also only asks whether the browser parses the declaration, not whether it renders it without bugs. And it adds work: every @supports block is a second path to test, so I use it for features that matter for the layout, not for small extras.

Further reading: MDN: @supports, On this site: CSS object-fit: cover fills a box without stretching

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link