Sub topic 6
Testing selector support with selector()
This chapter assumes you can write basic CSS selectors, understand how the cascade and source order resolve conflicts, and have used @supports with a property and value pair. It covers how to test whether a browser understands a selector, and why that test matters.
Why selectors need their own test
A property test such as @supports (display: grid) asks whether the browser understands a declaration. Selectors fail differently. A browser that cannot parse a selector in a rule's selector list drops the whole rule. That means you cannot write a baseline and an enhancement for a new selector in the same rule and rely on the cascade to pick one.
Here is an example of the problem:
.card:has(img),
.card.with-image {
border-color: rebeccapurple;
}If the browser does not support :has(), it treats the whole selector list as invalid. The rule is discarded, including the .card.with-image part that the browser could have handled. The selector() function lets you ask the question first and keep the two paths separate.
The selector() function
The selector() function goes inside an @supports condition. It takes one selector and is true when the browser can parse it.
.card {
border: 1px solid #ccc;
}
@supports selector(:has(img)) {
.card:has(img) {
border-color: rebeccapurple;
}
}The .card rule is the baseline and applies everywhere. The @supports block applies only where the browser can parse :has(img). Browsers that do not understand selector() itself treat the condition as false, so they skip the block, which is the behavior you want.
Notice the syntax. The selector is written directly inside selector( ), with no extra parentheses around the function. This is different from a property test, which needs its own parentheses: (display: grid).
@supports (display: grid) { /* property test */ }
@supports selector(:has(img)) { /* selector test */ }What the test does and does not tell you
selector() checks that the browser can parse the selector. It does not check that the selector will match what you expect, and it does not check how well the browser implements the feature.
| Question | Answered by `selector()`? |
|---|---|
| Does the browser recognize this syntax? | Yes |
| Does the selector match the elements I intended? | No |
| Are there performance or bug differences between browsers? | No |
Use the test as a gate for syntax support, and still verify the result in real browsers.
Combining selector() with other conditions
You can combine selector() with the operators not, and and or that you have already seen. Parenthesize each part when you mix operators.
@supports selector(:has(img)) and (display: grid) {
.gallery:has(figcaption) {
grid-template-rows: auto 1fr;
}
}A fallback for browsers that lack the selector can use not:
@supports not selector(:has(img)) {
.card.with-image {
border-color: rebeccapurple;
}
}In this case the markup has to carry a class such as with-image for the fallback to work. That is a common pattern: the enhanced path reads the document structure with :has(), and the fallback relies on a class added by your templates or by script.
Selecting a pattern
The diagram shows how a browser decides which rules apply.
Checking the same thing from JavaScript
CSS.supports() accepts the same selector() syntax in a condition string, so scripts can make the same decision:
const canUseHas = CSS.supports('selector(:has(a))');
if (!canUseHas) {
document.querySelectorAll('.card img').forEach((img) => {
img.closest('.card').classList.add('with-image');
});
}This script adds the with-image class only when :has() is not available. The CSS fallback from the previous section then picks it up.
Common mistakes
- Putting the new selector in a shared selector list. One unsupported selector invalidates the whole list. Move the new selector into an
@supportsblock. - Testing a property when you need a selector.
@supports (display: grid)says nothing about:has(). Test what you actually use. - Forgetting the baseline. If the card has no border outside the
@supportsblock, browsers without:has()get an unstyled card. Always write the baseline first. - Testing a simplified selector. Test the exact syntax you use. If you rely on a feature inside the parentheses, such as a complex argument, include it in the test.
Exercises
Exercise
A stylesheet contains .item:has(.badge), .item.has-badge { outline: 2px solid gold; }. In a browser without :has(), the .item.has-badge elements get no outline. Why, and how do you fix it?
Show answer
The browser cannot parse :has(.badge), so it drops the whole selector list, including .item.has-badge. Split the rules. Put .item.has-badge { outline: 2px solid gold; } outside any condition as the baseline, and put .item:has(.badge) inside @supports selector(:has(.badge)).
Exercise
Write a JavaScript line that stores whether the browser can parse the selector :focus-visible.
Show answer
const canFocusVisible = CSS.supports('selector(:focus-visible)');