Purely visual UI state — an open panel, a flipped card, an icon in a different mode — does not always need to travel through JavaScript. Keeping that logic in CSS keeps behavior next to presentation and avoids JS overhead. Interactions that carry business logic, need persistence, depend on data, or coordinate several moving parts remain a JavaScript job.
The starting point: binary toggles
The checkbox hack is the best-known way to give CSS a piece of built-in state it can react to. A hidden checkbox sits at the top of the document; a label connected to it lets the user toggle it from anywhere; and CSS reacts through :checked and sibling combinators. The technique can restyle the checkbox itself, toggle menus, control inner visuals, reveal hidden sections, or switch a theme.
#state-toggle:checked ~ .element {
/* styles when the checkbox is checked */
}
.element {
/* default styles */
}
Before :has(), the checkbox had to precede everything it influenced, because CSS could only select later elements in the DOM. Now a checkbox can sit right next to its label and still control elements that come before it, which loosens the HTML structure considerably.
Hiding the input with the HTML hidden attribute is common but problematic: the attribute also removes the element from assistive technologies, and a label has no interactive behavior of its own. A better fix is to make the checkbox the button itself — no hidden input, no label — and restyle it with appearance. That approach carries through to the radio-based examples below.
From one checkbox to mutually exclusive states
A checkbox offers two states, which is not enough when a component needs three, four, or seven modes. The radio state machine uses a group of radio buttons instead: they share a name (here state), so only one can be selected at a time, and each carries a unique data-state for CSS to target. The checked attribute sets the default state.
<div class="state-button">
<input type="radio" name="state" aria-label="state one" checked>
<input type="radio" name="state" aria-label="state two">
<input type="radio" name="state" aria-label="state three">
</div>
Keeping the controls usable
The radios are styled like buttons with appearance: none. Only the button for the next state in sequence is shown; the rest are hidden with three properties rather than display: none, which would make them inaccessible:
position: fixed;to take them out of normal flow.pointer-events: none;so they cannot be clicked.opacity: 0;so they are invisible.
The next radio is revealed with the adjacent sibling combinator (+) when the current one is checked. An optional rule shows the first radio when the last is checked, making the cycle circular.
input[name="state"] {
/* other styles */
position: fixed;
pointer-events: none;
opacity: 0;
&:checked + & {
position: relative;
pointer-events: all;
opacity: 1;
}
}
Because the checked radio is always hidden, its focus outline is hidden too. Adding an outline to the radio container keeps keyboard users oriented as they move through the states.
.state-button:has(:focus-visible) {
outline: 2px solid red;
}
Per-state styles then hang off :checked combined with data-state. The same pattern scales to steppers, view switchers, card variations, visual filters, layout modes, and CSS-only demos.
Variables instead of repeated style blocks
Assigning final properties per state means re-targeting the element each time, and selectors grow noisy as a component expands. A cleaner approach assigns state values to custom properties at a higher level and consumes them inside the component, relying on the cascade. Defining --left and --top per state, for example, lets the element itself simply read those variables.
body {
/* ... */
&:has([data-state="one"]:checked) {
--left: 48%;
--top: 48%;
}
&:has([data-state="two"]:checked) {
--left: 73%;
--top: 81%;
}
/* other states... */
}
.map::after {
content: '';
position: absolute;
left: var(--left, 50%);
top: var(--top, 50%);
/* ... */
}
State can also be treated as a number. A single numeric --state variable can drive calculations, such as a background color, across any element.
body {
/* ... */
&:has([data-state="one"]:checked) { --state: 1; }
&:has([data-state="two"]:checked) { --state: 2; }
&:has([data-state="three"]:checked) { --state: 3; }
&:has([data-state="four"]:checked) { --state: 4; }
&:has([data-state="five"]:checked) { --state: 5; }
}
.card {
background-color: hsl(calc(var(--state) * 60) 50% 50%);
}
With a per-item index variable like --i — at least until sibling-index() is more widely available — each item's position and opacity can be computed relative to the active state and its place in the sequence. One variable then drives the whole visual system: a rule is written once and each item supplies its own index.
.card {
position: absolute;
transform:
translateX(calc((var(--i) - var(--state)) * 110%))
scale(calc(1 - (abs(var(--i) - var(--state)) * 0.3)));
opacity: calc(1 - (abs(var(--i) - var(--state)) * 0.4));
}
Linear and two-way flows
Not every flow should loop. Dropping the circular rule and adding a disabled placeholder radio that appears in the final state produces a real endpoint, suitable for onboarding, checkout progress, or multi-step setup. Keyboard navigation still reaches the states, which is usually desirable; to prevent it, swap the three hiding properties for display: none by default and display: block (or inline-block) on the visible one, making the flow truly linear.
<input type="radio" name="state" disabled>
Going backwards requires revealing a second radio per state: the next button as before via +, and the previous button via :has() looking for a checked state on the next button (:has(+ :checked)).
input[name="state"] {
position: fixed;
pointer-events: none;
opacity: 0;
/* other styles */
&:has(+ :checked),
&:checked + & {
position: relative;
pointer-events: all;
opacity: 1;
}
/* Set text to "Next" as a default */
&::after {
content: "Next";
}
/* Change text to "Previous" when the next state is checked */
&:has(+ :checked)::after {
content: "Previous";
}
}
Accessibility and scope
Building on real form controls provides a solid baseline, but deliberate choices still matter:
- Make radios clearly interactive — cursor, size, spacing — with explicit wording.
- Keep visible focus styles so keyboard users can track their position.
- Signal unavailable steps in the UI, not by color alone.
- Respect reduced-motion preferences when state changes animate layout or opacity.
- Hand anything with business meaning — validation, persistence, async data — to JavaScript and treat CSS state as the visual layer.
The pattern fits state that is mostly visual, local, and interaction-driven. It does not fit flows built on business rules, external data, persistence, or complex orchestration. The useful skill is not proving CSS can manage everything, but knowing where it genuinely shines.



