Sub topic 11
Keeping :has() selectors fast
This chapter assumes you can write :has() selectors with descendant, child and sibling arguments, and that you know how the cascade and style recalculation work at a basic level.
Why :has() has a cost
Browsers normally match a selector from the right end to the left. For .card img, the browser looks at an img, then checks whether an ancestor is .card. A change inside the page only forces the browser to re-check elements near that change.
:has() reverses the direction. The browser must look inside or after the subject element to decide whether it matches. A change deep in the DOM, such as a class change, an inserted node or a toggled :checked state, can change whether an ancestor or an earlier sibling matches. The browser has to find those elements and re-evaluate them.
Current browser engines add optimizations to limit this work, so ordinary use is fine. The cost grows when the set of candidate subjects is large, so the main goal is to keep that set small.
Scope the subject
The subject is the part of the selector to the left of :has(). A broad subject means many elements to re-check.
/* Broad: every element is a candidate subject */
*:has(img) {
position: relative;
}
/* Scoped: only .card elements are candidates */
.card:has(img) {
position: relative;
}Give the subject a class, or at least a specific element type. A bare :has(img) is the same as *:has(img), so avoid it.
Keep the argument narrow
The argument decides how far the browser searches. A descendant argument searches the whole subtree. A child argument with > searches only direct children.
/* Searches every descendant of each .item */
.item:has(.badge) {
border-color: gold;
}
/* Searches only direct children */
.item:has(> .badge) {
border-color: gold;
}Use > when your markup guarantees the target is a direct child. Use + instead of ~ when the next sibling is enough, because ~ searches all following siblings.
Be careful with high-level subjects
A :has() on :root or body is a legitimate pattern, as in a theme toggle. The risk is a broad argument combined with a high-level subject, because that subject can then be affected by changes anywhere in the page.
/* Acceptable: specific ID in the argument */
:root:has(#dark-toggle:checked) {
color-scheme: dark;
}
/* Risky: any change to any link or image may affect the root */
:root:has(a:hover, img) {
color-scheme: dark;
}The first rule depends on one element. The second depends on every link and image in the document.
Keep arguments simple
Long chains inside :has() are harder to evaluate than short ones.
/* Harder to evaluate */
.field:has(div > div > input:invalid:not(:placeholder-shown)) {
outline: 2px solid crimson;
}
/* Simpler */
.field:has(input:invalid:not(:placeholder-shown)) {
outline: 2px solid crimson;
}If the extra structure is not needed to distinguish elements, drop it. You can also put a class on the target element so the argument is a single class selector.
Measure before optimizing
Most pages never have a problem with :has(). Treat the guidelines above as defaults, not as a reason to rewrite working CSS. If a page feels slow after DOM changes, check with the browser's tools:
- Open the Performance panel in DevTools and record the interaction.
- Look at the Recalculate Style entries and note their duration.
- Compare the timing with the
:has()rule disabled.
Chromium-based browsers can also record selector statistics in the Performance panel. These show which selectors cost the most time during style recalculation.
Summary of guidelines
| Practice | Reason |
|---|---|
| Give the subject a class or type | Fewer candidate elements to re-check |
| Use `>` or `+` when possible | Smaller search area than descendant or `~` |
| Avoid broad arguments on `:root` or `body` | Any DOM change can affect the match |
| Keep arguments short | Less work per check |
| Measure with DevTools | Avoids optimizing rules that are not a problem |
Exercise
A page has the rule :has(.error) { background: #fee; }. Rewrite it so it is cheaper to evaluate, assuming the error is always a direct child of an element with the class form-row, and explain the two changes.
Show answer
Use .form-row:has(> .error) { background: #fee; }. First, the subject changed from the implicit * to .form-row, so only form rows are candidates instead of every element. Second, > limits the search to direct children instead of the whole subtree.