Sub topic 2
Detect features, not browsers
This chapter assumes you know basic CSS syntax, how the cascade resolves conflicts, and what an at-rule such as @media looks like. You do not need to know @supports yet. The next chapters cover it in detail. Here you learn why feature detection is the right question to ask.
Two ways to ask "can I use this?"
Every enhancement depends on one question: does this browser understand the CSS I want to write? There are two ways to answer it.
- User-agent sniffing reads the browser's name and version, then guesses what it supports. Example: "If the browser is Safari 14 or older, skip
gapfor flexbox." - Feature detection asks the browser directly whether it can handle a specific property, value, or selector. Example: "If the browser understands
display: grid, use it."
Feature detection is the better choice. It tests the thing you care about, not a proxy for it.
Why sniffing fails
The user-agent string is a piece of text the browser sends to identify itself. Sniffing relies on it, and that causes several problems.
- The string can be wrong. Browsers have long included the names of other browsers in their strings to avoid being blocked. Users and extensions can also change it.
- Version tables go stale. You have to maintain a list of which version added which feature. Each new release makes the list less accurate.
- Names do not map cleanly to features. Two builds of the same browser can differ. A feature can be turned off by a setting, an enterprise policy, or a missing platform library.
- New and unknown browsers are ignored. A browser you did not list gets the wrong treatment, even if it supports everything.
Feature detection avoids all of these. If a browser supports display: grid, the test passes. It does not matter who made the browser, which version it is, or what it calls itself.
What feature detection looks like in CSS
CSS gives you two tools for this, and you will meet both in this tutorial.
- The cascade itself. A browser ignores declarations it cannot parse. If you write a safe declaration first and a newer one after it, old browsers keep the safe one.
- The
@supportsrule. It wraps styles in a condition that the browser evaluates. The styles apply only if the browser says "yes".
The next chapter looks at the cascade approach. Here is a small preview of the second tool. It asks the browser a direct question about a property and value:
.gallery {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.gallery > * {
flex: 1 1 12rem;
}
@supports (display: grid) {
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
}
}Read it in two parts. The first rules are the baseline, and every browser applies them. The @supports block is the enhancement. A browser applies it only if it understands display: grid. Nothing in this code mentions a browser name or a version.
How the browser decides
The flow below shows what happens when a browser reads a stylesheet that uses feature detection.
Both paths end with a working page. That is the point. A user on an older browser gets a usable layout, and a user on a modern browser gets the better one.
Comparing the approaches
| Question | User-agent sniffing | Feature detection |
|---|---|---|
| What does it test? | The browser's name and version | The exact property, value, or selector |
| Can it be wrong? | Yes, the string can be spoofed or misleading | Only if the browser's support is buggy |
| Does it need updates? | Yes, as new versions ship | No, new browsers pass automatically |
| Handles unknown browsers? | Poorly | Correctly |
A common mistake to avoid
Do not write a feature test that is really a browser test in disguise. For example, adding a class such as is-safari and then styling around it assumes that all Safari versions behave alike. Instead, name the capability: has-grid, has-backdrop-filter. Styles tied to a capability stay correct as browsers change.
Exercises
Exercise
A teammate proposes hiding a layout enhancement from "Browser X version 13 and older" by checking the user-agent string. Give two reasons feature detection is a better choice, and name the CSS rule you would use.
Show answer
Any two of these: the user-agent string can be spoofed or inaccurate; the version list must be updated with every release; unknown browsers get the wrong treatment even if they support the feature; a supported feature can be disabled in a specific build, which sniffing cannot see. The rule is @supports, for example @supports (display: grid) { ... }, with the baseline styles written outside it.
Exercise
You want to enhance a layout only where display: grid works. Should the flexbox baseline be written inside the @supports block, outside it, or both? Explain.
Show answer
Outside it. The baseline must apply in every browser, including those that fail the test, so it cannot live inside the @supports block. The block holds only the enhancement that overrides the baseline where grid is supported.