Sub topic 7
Reacting to state with :has()
This chapter assumes you know the basic :has() syntax and the state pseudo-classes :checked, :focus-visible, :invalid and :placeholder-shown. It also assumes you understand basic specificity.
Why state needs :has()
A state pseudo-class such as :checked describes one element. Before :has(), a rule could only style that element, or elements after it through sibling and descendant combinators. A wrapper around the element could not react.
:has() removes that limit. A wrapper, or the whole page, can now change its styles based on the state of something inside it. The state lives in the HTML, and the CSS reads it. No JavaScript is needed.
Highlighting a field while it has focus
Wrap each input and its label in a container. Then style the container.
<div class="field">
<label for="email">Email</label>
<input id="email" type="email" required placeholder="[email protected]">
</div>.field:has(input:focus-visible) {
outline: 2px solid royalblue;
}The outline appears on the whole .field when the input inside it matches :focus-visible. Use :focus-visible instead of :focus so that buttons and checkboxes show the ring for keyboard focus but not for mouse clicks. Text inputs show it for any kind of focus.
Marking invalid fields only after the user types
An empty required input matches :invalid as soon as the page loads. Showing errors at that point is unhelpful. The fix is to also require that the placeholder is no longer visible, which means the user has typed something.
.field:has(input:invalid:not(:placeholder-shown)) label {
color: crimson;
}The rule colors the label, which is a sibling of the input, so it reaches an element that the input alone could not style.
A page-level toggle
The same idea works at the top of the document. Put a checkbox anywhere in the page:
<label><input type="checkbox" id="dark-toggle"> Dark mode</label>
<main>Page content</main>Then let the root element watch it:
:root:has(#dark-toggle:checked) {
color-scheme: dark;
}Checking the box makes :root match, and color-scheme: dark switches the built-in colors for the whole page. Because the rule targets :root, you can add custom property overrides in the same rule, and every element that uses them will update. The choice is not saved. It resets when the page reloads, so persisting it needs JavaScript or a server.
Common errors
- No placeholder attribute.
:placeholder-shownonly matches when the input has aplaceholder. Without one,:not(:placeholder-shown)is always true, so an empty required field looks invalid immediately. Addplaceholder=" "if you do not want visible text. - Wrapper contains several inputs.
:has(input:focus-visible)matches when any one input inside has focus. Give each input its own wrapper if you need per-field styling. - Label outside the wrapper. A descendant selector after
:has()can only reach elements inside the wrapper. Keep the label inside.field. - Unexpected specificity.
.field:has(input:invalid:not(:placeholder-shown)) labelcounts the:has()argument at full weight. It scores (0,3,2), so it beats a plain.field labelrule.
Exercise
Write a rule that gives a .field wrapper a green 2px solid outline when it contains a checked checkbox.
Show answer
.field:has(input[type="checkbox"]:checked) { outline: 2px solid green; } The :has() argument tests the checkbox's :checked state, and the outline is applied to the wrapper, not the checkbox.