Sub topic 9
Running the same check in JavaScript with CSS.supports()
This chapter assumes you can write a basic @supports rule, know how not, and and or combine, and can write simple JavaScript. You will learn how to run the same checks from a script.
Why check support from JavaScript
An @supports rule is the right tool when the only thing that changes is a style. Sometimes a script needs to know the answer too. A script might load a different widget, skip an animation setup, or add a class that other code reads. In those cases, asking the browser directly is better than guessing from its name.
CSS.supports() is a method on the global CSS object. It returns true if the browser understands the condition and false if it does not. It uses the same parser as @supports, so the two always agree.
Two ways to call it
The method accepts two forms.
- Two arguments: a property name and a value.
- One argument: a full condition string, written as you would write it after
@supports.
if (CSS.supports('display', 'grid')) {
document.documentElement.classList.add('has-grid');
}
const canBlur = CSS.supports('(backdrop-filter: blur(4px)) or (-webkit-backdrop-filter: blur(4px))');
const canUseHas = CSS.supports('selector(:has(a))');The first call uses the two-argument form. You pass the property and the value as separate strings, with no parentheses and no colon.
The second and third calls use the one-argument form. Here the string is a full condition, so each declaration needs its own parentheses. This form supports not, and, or and selector(), exactly as the stylesheet does.
Common mistakes with the call syntax
The two forms have different rules, and mixing them up is the most frequent error.
- In the one-argument form, a bare declaration without parentheses is invalid.
CSS.supports('display: grid')returnsfalsein browsers that support grid, because the parser expects(display: grid). - In the two-argument form, do not add parentheses.
CSS.supports('(display)', '(grid)')returnsfalse. - Mixed
andandorstill need explicit parentheses around each group. Without them the whole condition is invalid and the result isfalse, not an error.
A worked example: a script that depends on CSS
Suppose a gallery script computes tile sizes by hand. If the stylesheet already provides a grid layout, the script should skip that work. The script can read support once and set a class on the root element. Other code and the stylesheet can then use that class.
const root = document.documentElement;
const features = {
grid: CSS.supports('display', 'grid'),
blur: CSS.supports('(backdrop-filter: blur(4px)) or (-webkit-backdrop-filter: blur(4px))'),
has: CSS.supports('selector(:has(a))'),
};
for (const [name, supported] of Object.entries(features)) {
root.classList.add(supported ? `has-${name}` : `no-${name}`);
}
if (!features.grid) {
layoutGalleryManually();
}
function layoutGalleryManually() {
const tiles = document.querySelectorAll('.gallery > *');
const width = Math.floor(100 / 3);
tiles.forEach((tile) => {
tile.style.width = `${width}%`;
});
}This code adds either has-grid or no-grid to the <html> element, and does the same for the other two features. Run it in the browser console and inspect document.documentElement.className to see the result.
When to use the script and when not to
Prefer @supports in the stylesheet when you only change styles. It needs no JavaScript, it works before scripts load, and it avoids a flash of unstyled content.
Use CSS.supports() when behavior depends on the answer. Two cases are common:
- Choosing between two code paths, such as a native layout and a script-driven one.
- Reporting or logging which features a user's browser has.
Be careful with a class-based approach for styling. The classes appear only after the script runs. If the script fails or loads late, the page falls back to the baseline, which is a safe outcome as long as the baseline works on its own.
| Need | Better tool |
|---|---|
| Change a style when a property is supported | `@supports` in CSS |
| Run different code depending on support | `CSS.supports()` |
| Style with a class set by a script | Class from `CSS.supports()`, with a working baseline |
Exercise
A teammate writes CSS.supports('display: grid') and gets false in a modern browser. What is wrong, and how do you fix it?
Show answer
The one-argument form needs a full condition, so the declaration must be wrapped in parentheses. Use CSS.supports('(display: grid)'), or use the two-argument form CSS.supports('display', 'grid').
Exercise
Write a call that returns true only when the browser supports both display: grid and gap: 1rem.
Show answer
CSS.supports('(display: grid) and (gap: 1rem)')