CSS Reference ยท Pseudo-classes
The :checked pseudo-class
Reference for the CSS :checked pseudo-class: style checked checkboxes, selected radio buttons and chosen options, with the sibling-selector toggle trick.
The :checked pseudo-class matches form controls that are currently on: a checkbox that is ticked, a radio button that is selected, or an <option> that is selected. It follows the live state, so the style changes the moment the user clicks, and it works without any JavaScript.
Because the checkbox can sit right before the element you want to style, :checked combined with the sibling combinators is the classic way to build toggles, tabs and accordions in pure CSS.
input[type="checkbox"]:checked + label { font-weight: 700; }
Try it
Syntax
:checked { /* declarations */ }
- Applies to: checkbox and radio
<input>elements and<option>elements. - Specificity: the same as any pseudo-class (0,1,0).
- Inherited / animatable: not applicable. It is a selector, not a property.
Values
| Syntax and arguments | Description |
|---|---|
:checked |
Takes no arguments. Matches a checkbox or radio whose checkedness is true, and an option whose selectedness is true. |
Usage notes
- State, not attribute.
:checkedtracks the current state. The HTMLcheckedattribute only sets the initial state, so[checked]stops matching what the user sees after the first click. - Hidden inputs. If you hide the real input for a custom design, keep it focusable (use
opacity: 0and position it, notdisplay: none) so keyboard and screen reader users can still reach it, and add a visible:focus-visiblestyle. - Style the control itself with care. Native checkboxes accept little styling. Use
accent-colorfor a quick colour change, orappearance: nonefor a fully custom control. - Style the label. A label that wraps its input needs
:has(:checked); a label after the input can useinput:checked + label. - Options. Browsers only let you style a few properties on
<option>, and it varies by platform. - Indeterminate boxes. A checkbox can be both
:checkedand:indeterminate-looking only through script; check both states if you style them.
Browser support
Supported in every browser for a very long time.
Related
:indeterminateโ mixed-state controls:disabledโ controls that cannot be used:has()โ style a parent based on a checked child- Lesson: Styling Forms
- Lesson: Pseudo-classes