Sub topic 3
Matching alternatives with :is()
This chapter assumes you can write type, class, ID and attribute selectors, use the descendant, child and sibling combinators, and group selectors with commas. It also assumes you know that specificity decides which of two conflicting rules wins. The chapter covers what :is() matches, where you can put it, and how it expands internally. Specificity and error handling get only a short mention here, because later chapters treat them in full.
What :is() does
:is() is a functional pseudo-class. Its argument is a selector list, and the pseudo-class matches an element if the element matches at least one selector in that list.
:is(h1, h2, h3) {
margin-block: 1.5em 0.5em;
}This rule applies to every h1, h2 and h3. It is equivalent to writing the three selectors separated by commas. The difference appears when the list is only one part of a longer selector.
Removing repeated parts
Without :is(), a selector list repeats every shared part:
/* Before */
article h1 a,
article h2 a,
article h3 a {
text-decoration: underline;
}
/* After */
article :is(h1, h2, h3) a {
text-decoration: underline;
}Both rules match the same elements. The second states the shared context (article ... a) once, and the variable part sits in the middle.
You can use :is() at any position in a selector: at the start, in the middle, or at the end. It can also share a compound selector with other simple selectors, such as a type, class or attribute selector.
/* Links or buttons that are hovered or keyboard-focused */
:is(a, button):is(:hover, :focus-visible) {
outline: 2px solid currentColor;
}
/* A link inside a nav that is inside a header or footer */
:is(header, footer) nav a[href] {
font-weight: 600;
}The first rule uses two :is() calls in one compound selector. An element must satisfy both: it must be an a or a button, and it must be hovered or focus-visible.
What can go inside the list
The argument is a selector list. Each item can be:
- A simple selector:
p,.note,#main,[hidden]. - A compound selector:
a.external,input[type="checkbox"]. - A complex selector with combinators:
ul > li,h2 + p,nav a. - Another
:is(), or other pseudo-classes such as:hoveror:not(...).
:is(ul > li, ol > li, dl > dd) {
line-height: 1.6;
}An empty :is() is valid but matches nothing.
Two kinds of selector are not allowed inside the list:
- Pseudo-elements such as
::beforeor::first-line. They are covered in the limits chapter. - Relative selectors that start with a combinator, such as
:is(> p). These belong to:has().
How the expansion works
Conceptually, a browser treats :is() as shorthand for the equivalent list of full selectors. When a selector contains several :is() calls, the result is the cross product of all the alternatives.
:is(.sidebar, .footer) :is(a, button) {
color: teal;
}This is equivalent to four selectors:
.sidebar a,
.sidebar button,
.footer a,
.footer button {
color: teal;
}The number of generated selectors multiplies with each list. Two lists of three items produce nine selectors. You never write that expansion, which is the main benefit of :is(). It also means that a selector that looks short can describe a large set of matches, so keep your lists purposeful.
A complete example
The following page is complete and can be saved as an .html file and opened in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>:is() demo</title>
<style>
article :is(h1, h2, h3) a {
text-decoration: underline;
color: darkblue;
}
:is(header, footer) nav a {
color: crimson;
}
</style>
</head>
<body>
<header>
<nav><a href="#top">Top link (crimson)</a></nav>
</header>
<article>
<h2><a href="#one">Heading link (dark blue, underlined)</a></h2>
<p><a href="#two">Paragraph link (default style)</a></p>
</article>
<footer>
<nav><a href="#end">Footer link (crimson)</a></nav>
</footer>
</body>
</html>The first rule styles only links inside headings within the article. The paragraph link is not inside an h1, h2 or h3, so it keeps the browser default. The second rule applies to links in a nav inside either header or footer.
Complex selectors inside :is() are not scoped
A complex selector inside :is() is matched against the whole document. It is not limited to the context written outside the function. This affects selectors that put a combinator before :is():
.card :is(.promo .title) {
color: orange;
}This matches an element that has the class title, has a .promo ancestor, and also has a .card ancestor. The .promo and .card ancestors can be different elements, and neither needs to contain the other. Here is the expanded form:
.card .promo .title,
.promo .title.card-descendant-placeholder {
color: orange;
}That expansion is not valid CSS, and it is not a real equivalent, because plain selectors cannot express "both ancestors in any order". The practical rule is simpler. Treat each complex selector in the list as a test the element must pass on its own, and treat the text outside as a separate test. The element must pass both.
Specificity and parsing, briefly
Two behaviors of :is() are easy to miss. Both have their own chapters.
- Specificity.
:is()adds the specificity of its most specific argument. This holds even for elements that matched a less specific item in the list. In:is(#main, .note) p, a paragraph inside.notestill gets the weight of#main. - Forgiving parsing. If one item in the list is invalid or unsupported,
:is()ignores that item and keeps the others. A normal selector list drops the entire rule instead.
The table compares a plain selector list with :is() for the same input.
| Behavior | Plain selector list | `:is()` |
|---|---|---|
| Shared parts | Repeated for each item | Written once |
| One invalid item | Whole rule is dropped | Only that item is ignored |
| Specificity | Each selector has its own value | The most specific argument sets the value for all items |
| Pseudo-elements in the list | Allowed as part of a selector | Not allowed inside the argument |
The specificity row is the one that most often surprises people. A rule written with :is() can win conflicts that its expanded form would lose, because every alternative is promoted to the weight of the heaviest one.
Common mistakes
- Putting a pseudo-element inside the list. Write
:is(p, li)::first-line, not:is(p::first-line, li::first-line). The second form never matches. - Assuming each alternative keeps its own specificity. A single ID in the list raises the weight of the whole selector.
- Starting an argument with a combinator.
:is(> p)is not valid here. Use the combinator outside, as insection > :is(p, ul). - Reading the scope of an inner complex selector as relative to the outer one. As shown above, it is matched against the document.
- Building very large cross products. Long lists in several positions are hard to read and hard to debug, even though the browser handles them.
Checking your understanding
Use these questions to confirm that you can read and write :is() selectors. Work out the expansion for each rule before checking.
Exercise
Rewrite the following rule using :is() so that it matches the same elements with no repeated parts. Then state how many plain selectors the original list contains. .post h2 em, .post h2 strong, .post h3 em, .post h3 strong { color: tomato; }
Show answer
.post :is(h2, h3) :is(em, strong) { color: tomato; } The original list has four selectors, which is the cross product of two heading types and two inline types (2 x 2 = 4). The shared part, .post, is written once, and each variable part moves into its own :is() list.
Exercise
Why does the rule :is(p::first-line, li::first-line) { font-weight: bold; } never match anything, and how do you fix it?
Show answer
Pseudo-elements are not allowed inside the argument list of :is(), so the selector matches nothing. Move the pseudo-element outside the function: :is(p, li)::first-line { font-weight: bold; }.