Skip to content
Tutorials Center
My learning

Topics

Lesson 106 of 139 ยท Modern CSS

Feature Queries (@supports)

Use @supports to test whether a browser understands a CSS property, value or selector, and build pages that improve gracefully as browsers add new features.

CSS keeps growing. Every year brings new properties, values and selectors, and browsers adopt them at slightly different speeds. So how do you use something brand new without breaking the page for people whose browser doesn't know it yet?

The answer is a feature query. The @supports at-rule asks the browser "do you understand this?" and applies a block of styles only if the answer is yes. In this lesson you will write feature queries for properties and selectors, combine conditions, check features from JavaScript, and learn the mindset that makes all of this work: progressive enhancement.

CSS already fails safely

Before reaching for @supports, remember one of CSS's best design decisions: a browser ignores any declaration it doesn't understand and carries on with the rest. That gives you a free fallback mechanism, just by writing the old value first:

CSS
.panel {
  background: #3366cc;                     /* every browser */
  background: oklch(55% 0.17 260);         /* browsers that understand oklch() */
}

A browser that knows oklch() uses the second line, because it comes later. A browser that doesn't drops that line as invalid and keeps the first. No feature query required.

This trick covers many cases. It stops working when a new feature needs several declarations to work together, or when the fallback needs different properties from the enhanced version. That is the job of @supports.

Writing a feature query

A feature query wraps a declaration in parentheses. The rules inside apply only if the browser supports that property with that value:

CSS
@supports (display: grid) {
  .gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
  }
}

The test is real: the browser checks whether it would accept display: grid as a valid declaration. A browser that knows the display property but not the grid keyword answers "no".

The query only checks whether the browser parses the declaration. It cannot tell you whether the feature is implemented without bugs, and it says nothing about whether the element is visible or the layout looks right.

A basic feature query

Every current browser supports aspect-ratio, so you will see the green box. Now change the condition to a made-up value such as (aspect-ratio: banana) and run it again: the query fails and the red fallback appears.

Combining conditions

Feature queries understand three logical keywords: not, and and or. Each condition still needs its own parentheses:

CSS
/* Only when the feature is missing */
@supports not (display: grid) {
  .gallery > * { float: left; width: 33%; }
}

/* Both must be supported */
@supports (display: grid) and (gap: 1rem) {
  /* ... */
}

/* Either one is enough */
@supports (backdrop-filter: blur(4px)) or (-webkit-backdrop-filter: blur(4px)) {
  .glass { background: rgb(255 255 255 / 0.4); }
}

When you mix and and or, wrap groups in extra parentheses so there is no doubt about the order: @supports ((a) or (b)) and (c). Writing (a) or (b) and (c) without the grouping is invalid.

Be careful with not for fallbacks. A browser too old to understand @supports itself ignores the whole block, so @supports not (...) styles never reach it. Write the fallback as normal, unconditional CSS, then use a positive @supports (...) block to upgrade.

Testing selectors with selector()

You can also ask whether a browser understands a selector. Wrap it in selector():

CSS
@supports selector(:has(img)) {
  .card:has(img) {
    grid-template-columns: 8rem 1fr;
  }
}

This matters because of how selectors fail. If a browser doesn't understand one selector in a comma-separated list, it throws away the entire rule. Guarding a new pseudo-class with selector() lets you provide a clear alternative instead of losing the rule silently. You can learn more about :has() in :is(), :where(), :not() and :has().

Detecting :has() support

Tick the checkbox. The whole form changes color, which used to be impossible without JavaScript because CSS could not style a parent based on its children.

Progressive enhancement

Feature queries are a tool for a wider strategy called progressive enhancement:

  1. Start with a baseline that works in every browser: readable text, usable links, a simple layout.
  2. Layer improvements on top for browsers that support them: a fancier layout, smoother effects, better typography.
  3. Make sure that if any layer is missing, the page is still complete and usable, just a little plainer.

The opposite approach, sometimes called graceful degradation, starts from the full design and patches problems in older browsers. Progressive enhancement tends to be more robust, because the fallback is the thing you write first rather than an afterthought.

A practical example: a "frosted glass" header using backdrop-filter. Without the blur, a semi-transparent background would make text hard to read, so the fallback uses a solid color:

CSS
.site-header {
  background: #ffffff;        /* solid, always readable */
}

@supports (backdrop-filter: blur(8px)) {
  .site-header {
    background: rgb(255 255 255 / 0.6);
    backdrop-filter: blur(8px);
  }
}

Here the two declarations really do depend on each other, which is exactly when @supports beats the simple "write it twice" trick.

Checking from JavaScript

JavaScript has the same test built in as CSS.supports(). It accepts either a property and value, or a full condition string:

CSS.supports() in JavaScript

Your results depend on your browser. That is the point: the same page can adapt to whatever it finds. Use the JavaScript version when a script needs to choose a different behavior, for example loading a polyfill only when a feature is missing.

A short history of vendor prefixes

Before feature queries, browsers shipped experimental features behind vendor prefixes: -webkit- (Safari, and Chrome in its early years), -moz- (Firefox), -ms- (Internet Explorer and old Edge) and -o- (old Opera). Developers wrote the same declaration four or five times:

CSS
.old-school {
  -webkit-transition: opacity 0.3s;
  -moz-transition: opacity 0.3s;
  -o-transition: opacity 0.3s;
  transition: opacity 0.3s;
}

It went badly. Sites relied on prefixed versions forever, so browsers could never change or remove them. Some browsers even had to support other vendors' prefixes to render popular sites correctly.

Today, browsers ship new features behind settings flags during testing, then turn them on unprefixed. A handful of prefixed properties still matter in practice, for example -webkit-line-clamp for multi-line truncation and -webkit-text-stroke. If your project has a build step, a tool such as Autoprefixer can add any remaining prefixes automatically, based on the browsers you say you support. Don't write long prefix stacks by hand.

Where to check support

Feature queries tell you about the browser in front of you. To plan which features to use at all, look up support data in a compatibility table, and watch for the term Baseline: a label that means a feature works in all major browser engines. "Newly available" Baseline features work in the latest versions; "widely available" means they have worked for at least two and a half years. For features that are not yet Baseline, such as anchor positioning, a feature query with a sensible fallback is the safe way to start using them.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does @supports (display: grid) actually test?

    Feature queries check whether the browser can parse the declaration. They cannot detect bugs or check the layout.
  2. 2. Which condition is written correctly?

    Each condition needs parentheses, and when you mix and and or you must group them with extra parentheses.
  3. 3. How do you test whether a browser understands the :has() pseudo-class?

    The selector() function tests whether a selector is supported.
  4. 4. Why is a plain unconditional fallback usually better than @supports not (...)?

    A browser too old for feature queries skips every @supports block, including not ones. Normal CSS reaches every browser.
  5. 5. When do you actually need @supports instead of simply writing a fallback value first?

    Repeating a declaration works for single values. When the fallback and the enhancement need different sets of properties, like a translucent background plus backdrop-filter, use @supports.

Summary

  • Browsers ignore declarations they don't understand, so writing a fallback value first is often enough.
  • @supports (property: value) applies rules only when the browser accepts that declaration.
  • Combine conditions with and, or and not, and group mixed conditions with parentheses.
  • selector() tests selector support; CSS.supports() does the same checks in JavaScript.
  • Progressive enhancement means building a working baseline first, then layering improvements.
  • Vendor prefixes are mostly history; let a tool add the few that remain.

Next: Scroll Snap, where you will build smooth carousels and galleries with pure CSS.