Sub topic 9
Putting it together: cards, forms, and a theme toggle
This chapter assumes you know the :has() syntax, the difference between descendant and child matching, :not(), and the state pseudo-classes :checked, :focus-visible, and :invalid. You will combine them into three patterns you can reuse.
All three patterns work the same way. An element deep in the page changes, either because it exists or because its state changes. An ancestor reacts to that change through :has().
Content-aware cards
A card with an image needs a two-column layout. A card without one should stay a single column with more padding. Without :has(), you would add a modifier class such as .card--media in the markup. With :has(), the CSS reads the content.
<div class="card">
<img src="photo.jpg" alt="A lighthouse at dusk" width="200" height="150">
<div>
<h3>Lighthouse</h3>
<p>Built in 1872.</p>
</div>
</div>
<div class="card">
<h3>Text only</h3>
<p>No image here.</p>
</div>.card {
padding: 1rem;
}
.card:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
gap: 1rem;
}
.card:not(:has(img)) {
padding: 2rem;
}Use :not(:has(img)) for "no image anywhere inside". Do not write .card:has(:not(img)). That matches any card with at least one descendant that is not an image, which is true for almost every card.
Form field states
A wrapper can react to the input inside it, so the label and the border change together.
<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;
}
.field:has(input:invalid:not(:placeholder-shown)) label {
color: crimson;
}The second rule needs two checks. A required, empty input is already :invalid, so :invalid alone would turn the label red before the user types anything. Adding :not(:placeholder-shown) limits the rule to fields that contain text. This only works if the input has a placeholder attribute. Without one, :placeholder-shown never matches, so :not(:placeholder-shown) is always true and the label is red from page load. A single space placeholder=" " is enough if you do not want visible hint text.
A page-level theme toggle
The last pattern puts the state on :root. The checkbox can sit anywhere in the document.
<label><input type="checkbox" id="dark-toggle"> Dark mode</label>
<main>Page content</main>:root:has(#dark-toggle:checked) {
color-scheme: dark;
}color-scheme: dark switches only the browser defaults, such as the page background, text color, and form controls. For your own colors, use custom properties that the same rule overrides.
:root {
--bg: #ffffff;
--fg: #1a1a1a;
}
:root:has(#dark-toggle:checked) {
color-scheme: dark;
--bg: #121212;
--fg: #eeeeee;
}
body {
background: var(--bg);
color: var(--fg);
}Two limits apply. The choice is lost on reload, because plain CSS cannot store it. Also, the checkbox must exist in the DOM. If display: none removes it from the layout, the state still works, but keyboard users can no longer reach the toggle. Hide it visually while keeping it focusable instead.
Common errors
- Writing
:has(img)when you meant:has(> img). The first matches an image at any depth, and the second only matches a direct child. - Forgetting that the specificity of
:has()comes from its most specific argument..card:has(#hero)beats.card.featured. - Wrapping everything in
:has()onbodyor:rootwith a broad argument. Keep the subject close to the content, as in.card:has(img), so the browser has less to re-check on each change. - Not providing a fallback. Put layout-critical rules behind
@supports selector(:has(*))and keep a sensible base style outside it.
Exercise
A form has <input class="name" required> with no placeholder attribute inside .field. You write .field:has(input:invalid:not(:placeholder-shown)) label { color: crimson; }. The label is red as soon as the page loads. Why, and what is the smallest fix?
Show answer
Without a placeholder, :placeholder-shown never matches, so :not(:placeholder-shown) is always true. The empty required input is :invalid, so the rule matches immediately. The smallest fix is to add a placeholder attribute, even placeholder=" ".