CSS feature detection lets you write modern styles while keeping older browsers functional. Instead of asking “which browser is this?”, you ask “does this browser support this property?” and serve the right CSS either way. This guide shows you how to use @supports and the CSS.supports() API, what to check for, and how to avoid common pitfalls.
What You’ll Need
- A text editor (VS Code, Sublime, etc.)
- Basic understanding of CSS and HTML
- A browser with developer tools (Chrome, Firefox, Safari, Edge)
- Access to a testing tool like BrowserStack or physical devices if you want to verify older browsers
Step-by-Step
- Identify the CSS feature you want to use. For example,
display: grid,gapin flexbox,backdrop-filter, oraspect-ratio. Check current support on caniuse.com to know which browsers lack it. - Write the fallback styles first. Put the basic, universally supported version of the layout or effect in your regular CSS. This is what older browsers will get.
- Add the modern style inside an
@supportsrule. The syntax is@supports (property: value) { ... }. For example:/* Fallback */ .card { display: block; } /* Modern */ @supports (display: grid) { .card { display: grid; grid-template-columns: 1fr 1fr; } }The browser only applies the inner rules if it understands
display: grid. - Use
not,and, andorfor complex checks. For instance, to check for both grid and gap:@supports (display: grid) and (gap: 1rem) { .grid { display: grid; gap: 1rem; } }To target browsers that do not support something:
@supports not (display: grid) { .grid { display: flex; flex-wrap: wrap; } } - For JavaScript, use
CSS.supports(). If you need to conditionally add classes or run scripts:if (CSS.supports('display', 'grid')) { document.body.classList.add('grid-support'); }The method takes either two arguments (property, value) or one string like
'display: grid'. - Test with a feature that is missing in an old browser. For example, open your page in Internet Explorer 11 (if you must support it) and confirm the fallback shows. In modern browsers, confirm the enhanced version appears.
- Combine with @media queries. Feature detection works inside media queries, so you can do responsive and feature-based styling together:
@media (min-width: 768px) { @supports (display: grid) { .layout { display: grid; } } } - Use feature detection for new units and functions. For example,
clamp()ormin()can be checked with@supports (width: clamp(1px, 2vw, 3px)). - Keep the fallback fully functional, not just visually. If you use
position: sticky, ensure the fallback still allows scrolling and content access. - Consider using a tool like Modernizr for very old browsers that don’t support
@supports(IE11 and earlier). Modernizr adds classes to the<html>element, but it’s an extra dependency. For most modern projects, native@supportsis enough.
Common Mistakes
- Checking a property but not the value.
@supports (display: grid)is fine, but sometimes you need to check the exact value, e.g.,@supports (display: inline-grid)because some browsers support one but not the other. - Forgetting the fallback for the fallback. If you use
@supports not (display: grid), the browser must support@supportsitself. IE11 doesn’t, so it will ignore the whole rule and use the base styles. - Overcomplicating with
orandand. Keep conditions simple. Long chains are hard to read and may have unexpected results. - Testing only in modern browsers. You must test in the browsers you claim to support, especially the old ones.
- Using feature detection for properties that are widely supported. It’s unnecessary overhead. Use it only for features with partial support.
Tips
- Write the fallback first, then the progressive enhancement. This ensures the base experience works everywhere.
- Use the “CSS Feature Query” panel in Firefox DevTools to see which @supports rules are active.
- For JavaScript, check
window.CSSexists before callingCSS.supports()to avoid errors in very old browsers. - Combine feature detection with
@media (prefers-reduced-motion)for accessibility, but don’t mix them in one condition unless necessary. - Keep your fallback styles simple. They don’t need to be pixel-perfect; they need to be usable.
Troubleshooting
My fallback shows in a modern browser. Check that the property/value you’re testing is actually supported. For example, @supports (display: grid) works in all modern browsers, but @supports (gap: 1rem) might fail in older Safari. Use the browser’s dev tools to inspect which rules are applied.
The @supports rule is ignored entirely. Make sure you haven’t misspelled the property or value. Also check for a missing semicolon or closing brace. Validate your CSS.
IE11 doesn’t show the fallback. IE11 doesn’t support @supports. Your base styles (outside any @supports) should be the fallback. If you put fallback styles inside @supports not (...), IE11 will ignore them. Use a separate stylesheet or Modernizr for IE-specific fallbacks.
JavaScript CSS.supports() throws an error. In very old browsers, CSS may be undefined. Wrap it in a check: if (window.CSS && CSS.supports(...)).
Feature detection works, but the layout still breaks. The issue might be that the fallback relies on properties that are also unsupported. For example, using flex as a fallback for grid might not work in IE10 (which has old flex syntax). Test your fallback separately.
Frequently Asked Questions
What is the difference between @supports and @media?
@media checks the environment (screen size, resolution, etc.), while @supports checks the browser’s CSS capabilities. They can be combined to serve styles based on both.
Can I use @supports to detect vendor prefixes?
Yes, you can check for prefixed properties, e.g., @supports (-webkit-backdrop-filter: blur(1px)). But it’s usually better to use the standard property and let the browser decide.
Does @supports work in all modern browsers?
Yes, all current versions of Chrome, Firefox, Safari, and Edge support @supports. The only major holdout is Internet Explorer 11 and earlier.
Can I use feature detection in inline styles?
No, @supports only works in stylesheets. For inline styles, you’d need JavaScript with CSS.supports().
Is it better to use @supports or a polyfill?
Feature detection is cleaner and faster than polyfills because it doesn’t load unnecessary code. Use a polyfill only if you need the feature to work identically in old browsers, not just a fallback.
How do I test for a property with a custom value like a specific color?
You can test any valid value, e.g., @supports (color: oklch(50% 0.1 200)). If the browser doesn’t understand the value, the check fails.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Disable Autocorrect on iPhone or iPad: Quick Guide
- Microsoft Store Error 0x80073d12: Fixes That Work
- View Notification Count on iPhone: Full Guide
- Custom Keyboard Shortcuts on Windows 11 & macOS
- Restore Windows 11 Display Scaling to Recommended
- Semiconductor Cycle: How Investors Track It0.0