Sub topic 3
Matching alternatives with :is()
This chapter assumes you know basic selectors (type, class, ID, attribute), combinators, comma-separated selector lists, and the idea that specificity decides which rule wins in the cascade.
What :is() does
:is() is a functional pseudo-class. It takes a selector list as its argument and matches any element that matches at least one selector in that list. You can use it anywhere inside a larger selector, so you can write the shared parts once and list only the parts that differ.
Here is the same rule written both ways:
/* Before */
article h1 a,
article h2 a,
article h3 a {
text-decoration: underline;
}
/* After */
article :is(h1, h2, h3) a {
text-decoration: underline;
}Both versions match the same links. The second one lists article and a once, and only the heading level varies.
Reading an :is() selector
Read article :is(h1, h2, h3) a from left to right. It means: an a that has an h1, h2 or h3 ancestor, and that ancestor has an article ancestor. The browser expands the list for you, so you get the same result as the longer rule.
Where you can place :is()
The list can sit at the start, middle or end of a compound selector. It can also be combined with other simple selectors.
/* Several parents, one child */
:is(header, main, footer) p {
line-height: 1.5;
}
/* Combined with a class on the same element */
:is(button, a).primary {
font-weight: 600;
}
/* Several alternatives on both sides of a combinator */
:is(ul, ol) > :is(li, dt) {
margin-block: 0.25rem;
}The last rule shows the largest saving. Written as a plain list, it would need four separate selectors. With two :is() lists, the number of combinations is multiplied for you.
Specificity in one sentence
:is() itself adds nothing. Instead, the selector takes the specificity of the most specific item in its list. Specificity is written as (ID, class, type).
/* Specificity (1,0,1): the most specific argument, #main, sets the value */
:is(#main, .note) p {
color: blue;
}Here #main is the most specific item, so the whole selector counts as (1,0,1), even when the matched element is really a .note. That surprises many people. A list that mixes an ID with a class gives the class-based match the ID's weight.
/* Both match a paragraph inside .note, but the first is stronger */
:is(#main, .note) p {
color: blue; /* (1,0,1) */
}
.note p {
color: red; /* (0,1,1), loses even though it is later in the file */
}The next chapters cover how :where() differs here and how to choose between the two.
Forgiving selector lists
A normal selector list is all or nothing. If one selector is invalid, for example because the browser does not know a pseudo-class, the browser drops the whole rule.
:is() uses forgiving parsing. It ignores an invalid item and keeps the valid ones.
/* One unknown selector invalidates the whole rule, so nothing matches */
a,
:unsupported-pseudo {
color: red;
}
/* Forgiving parsing ignores the unknown selector, so links still match */
:is(a, :unsupported-pseudo) {
color: red;
}This is useful when you list selectors that only some browsers understand.
A common pitfall
You cannot put a pseudo-element inside the list. An item that contains one is treated as invalid and never matches. Put the pseudo-element outside the function.
/* Works */
:is(p, li)::first-line {
font-weight: bold;
}
/* Matches nothing */
:is(p::first-line, li::first-line) {
font-weight: bold;
}Exercise
Exercise
Rewrite these rules as one rule that uses :is(). Then state its specificity. nav a:hover, nav button:hover, footer a:hover, footer button:hover { color: teal; }
Show answer
nav and footer vary, and so do a and button, so use two lists: :is(nav, footer) :is(a, button):hover { color: teal; }. Every item in both lists is a type selector, and :hover is a pseudo-class. The specificity is (0,1,2): one pseudo-class and two type selectors.
Exercise
A page has <p class="note"> inside <div id="main">. The stylesheet has :is(#main, .note) p { color: blue; } and .note p { color: red; }. Which color does a paragraph inside the .note element get, and why?
Show answer
Blue. The :is() selector takes the specificity of its most specific argument, #main, so it counts as (1,0,1) and beats .note p at (0,1,1). It wins even when the match came through .note rather than #main, and it would also win if it came earlier in the file.