← Back to HTML & CSS, page 7

html-css-0135 · Medium mid

How does @supports work, and how do you use it to add a modern feature with a safe fallback?

@supports applies rules only if the browser understands a property, a value or a selector. Write a working fallback first, then override it inside @supports. For a single declaration, the old value first and the new one after it often works without a query.

Full answer

@supports is a feature query. It applies the rules inside it only if the browser understands the condition, so you can use a new feature without breaking older browsers.

Forms of the condition:

  • @supports (display: grid) { ... } is true when the browser knows the property and the value.
  • @supports not (display: grid) { ... } is true when it does not. Use it for fallback rules only when you cannot arrange the fallback first.
  • and, or and brackets combine conditions: @supports (display: grid) and (gap: 1rem).
  • @supports selector(:has(a)) { ... } tests selector support.

The best pattern is progressive enhancement. First write the simple rules that work everywhere, the fallback. Then, inside @supports, write the modern version, which overrides the fallback in browsers that understand it. Older browsers skip the block and keep the fallback.

Many features need no @supports. If a browser does not understand a declaration, it ignores that single declaration, so writing the old value first and the new one after it is enough. Use @supports when:

  • You must change several rules together, such as turning on a new layout and changing the child rules too.
  • A rule is only safe when the feature exists. For example, position: sticky or a grid layout needs matching changes on the children.
  • The browser understands the property name but the value would cause a worse result than the fallback.

From JavaScript, CSS.supports("display", "grid") runs the same test.

The cards use the grid version in this browser, so they sit in two columns. The nonsense feature is not supported, so the red outline is never applied. The paragraph that contains a link turns green because the browser understands :has().

Test the fallback too, for example by editing the condition to something false.

html
<style>
  .cards { display: flex; flex-wrap: wrap; gap: 8px; width: 400px; }
  .cards > div { flex: 1 1 100px; background: #cde; padding: 4px; }
  @supports (display: grid) {
    .cards { display: grid; grid-template-columns: repeat(2, 1fr); }
  }
  @supports (foo: bar) { .cards { outline: 3px solid red; } }
  .tag { color: black; }
  @supports selector(:has(a)) { .tag:has(a) { color: green; } }
</style>
<div class="cards" id="c"><div>1</div><div>2</div><div>3</div></div>
<p class="tag" id="t"><a href="#">link</a></p>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • @supports tests properties, values and selector().
  • Fallback first, enhancement inside the query.
  • Unknown declarations are ignored anyway.
  • Use it when several rules must change together.
  • CSS.supports() tests from JavaScript.

Common mistakes

  • Writing the modern version first and having no fallback.
  • Using @supports for single declarations where writing the old value first is enough.
  • Never testing the fallback path.

Follow-ups

  • What does @supports not do?
  • How can JavaScript test for CSS support?

Tips

  • Describe progressive enhancement: fallback first, enhancement inside @supports.
  • Say that unknown declarations are ignored, so @supports is not always needed.