Sub topic 10
Limits and common pitfalls
This chapter assumes you can read specificity as an (ID, class, type) triple and that you have used :is() and :where() for simple grouping. Both pseudo-classes are safe to use, but a few rules catch people out. Most of them fail silently, so you see no error and the style just doesn't apply.
Pseudo-elements do not work inside the list
Pseudo-elements such as ::first-line, ::before and ::marker are not allowed in the argument list. The browser drops them, so the rule never matches. Put the pseudo-element after the function.
/* Works */
:is(p, li)::first-line {
font-weight: bold;
}
/* Never matches */
:is(p::first-line, li::first-line) {
font-weight: bold;
}Relative selectors are not allowed
A selector that starts with a combinator is a relative selector. :has() accepts these, but :is() and :where() do not.
/* Valid: :has() takes relative selectors */
section:has(> img) {
padding: 1rem;
}
/* Invalid: the leading ">" makes this argument unusable */
section :is(> img, > video) {
max-width: 100%;
}Write section > :is(img, video) instead.
The most specific argument always counts
:is() takes the specificity of its most specific argument. This holds even when the element matched a less specific one.
:is(#main, .note) p {
color: blue;
}A paragraph inside .note that has no #main ancestor still gets specificity (1,0,1). One ID anywhere in the list raises the weight of every match. If you want the grouping without that weight, move the ID selector out of the list or use :where().
Forgiving parsing hides mistakes
An invalid argument is ignored instead of invalidating the rule. This is useful for new selectors that older browsers don't know. It also means a typo disappears without a trace.
:is(.card, .crad, .panel) {
border: 1px solid gray;
}The misspelled .crad is still a valid class selector, so it simply matches nothing. A selector that is syntactically invalid, such as :is(.card, >> .panel), is silently skipped. Check the DevTools "Styles" pane when a grouped rule seems to have no effect.
An empty list is also valid. :is() matches nothing.
Pitfalls at a glance
| Pitfall | What happens | Fix |
|---|---|---|
| Pseudo-element inside the list | Argument is dropped, no match | Put it after the function |
| Leading combinator in an argument | Argument is dropped | Place the combinator outside |
| ID in an `:is()` list | Whole selector gets ID weight | Use `:where()` or restructure |
| Typo in an argument | Silent non-match | Test in DevTools |
| `:where()` for rules that must win | Zero specificity loses to nearly anything | Use `:is()` or a normal selector |
Zero specificity cuts both ways
A :where() rule loses to any other rule, including a plain p type selector. If an element is unexpectedly unstyled, look for a :where() rule that is being overridden by a reset or a third-party stylesheet.
:where(.btn) {
background: navy;
}
button {
background: lightgray; /* wins: (0,0,1) beats (0,0,0) */
}Exercises
Exercise
A rule reads :is(h1, h2)::before, :is(h3::before) { content: "#"; }. Which headings get the content, and why?
Show answer
Only h1 and h2. The first selector puts the pseudo-element outside the function, so it works. In the second, ::before is inside the argument list, so that argument is dropped and h3 never matches.
Exercise
What is the specificity of :is(.a, #b, p) span, and does it change for an element that only matches .a?
Show answer
It is (1,0,1). The ID #b is the most specific argument, and it counts for every match, including one that only matches .a.