@supports is commonly reached for to check whether a browser understands a property: value pair. It can also test selectors, via the selector() function:
@supports selector(:nth-child(1 of .foo)) {
}
The selector sits between the parentheses and is what gets evaluated. That example is a useful test in its own right: it is a selector list argument for :nth-child and its n relatives, and support is currently limited to Safari.
Wiring up a fallback
Suppose the selector list form is exactly what you want. Consider a list that carries separators, which is not necessarily a great idea, built on the fact that <ol> and <ul> accept only <li> children:
<ul>
<li class="list-item">List item</li>
<li class="list-item">List item</li>
<li class="separator"></li>
/* ... */
</ul>
Now add zebra striping, which means selecting every other .list-item while skipping .separator:
li:nth-child(odd of .list-item) {
background: lightgoldenrodyellow;
}
Safari is the only browser that supports that selector, so it goes behind a feature test:
@supports selector(:nth-child(1 of .foo)) {
li:nth-child(odd of .list-item) {
background: lightgoldenrodyellow;
}
}
When the fallback doesn't matter, the @supports block can be skipped entirely. But if the fallback does matter, pay attention to what changes. In the supported case the zebra striping can carry most of the UX weight, so a separator only needs some space. Browsers without the selector have no striping, so their separator has to work harder. Both states can be styled explicitly:
@supports selector(:nth-child(1 of .foo)) {
li {
padding: 0.25em;
}
li:nth-child(odd of .list-item) {
background: lightgoldenrodyellow;
}
li.separator {
list-style: none;
margin: 0.25em 0;
}
}
@supports not selector(:nth-child(1 of .foo)) {
li.separator {
height: 1px;
list-style: none;
border-top: 1px dashed purple;
margin: 0.25em 0;
}
}
Should the @when syntax land, the same rule becomes somewhat cleaner:
/* Maybe? */
@when supports(selector(:nth-child(1 of .foo))) {
} @else {
}
The resulting rendering:


Testing support from JavaScript
A JavaScript API for the same check exists, and it works:
CSS.supports("selector(:nth-child(1 of .foo))")
At the time of writing, that call fails in Chrome and passes in Safari.
The rarest case is the useful one
Selectors with oddly uneven cross-browser support are few, and of those, even fewer are worth guarding with @supports rather than simply letting the rule fail. Several candidates turn out to be well supported already: the ::marker pseudo-element, the case-insensitive attribute selector [href$="pdf" i], and the comma-separated :not(a, .b, [c]). A more promising direction might be :fullscreen versus :-webkit-full-screen, since that one is notably unsupported in iOS Safari.



