CSS :checked Selector - Styling Checkboxes and Radios
Style checked checkboxes, radio buttons, and options with CSS :checked. Build custom toggle switches, accordion menus, and interactive UIs without JavaScript.
Detailed Explanation
The :checked Pseudo-class
The :checked selector matches form elements that are currently checked or selected: checkboxes, radio buttons, and <option> elements.
Basic Usage
input[type="checkbox"]:checked {
accent-color: #3b82f6;
}
input[type="radio"]:checked + label {
font-weight: bold;
color: #3b82f6;
}
Custom Checkbox
/* Hide native checkbox */
input[type="checkbox"] {
appearance: none;
width: 20px;
height: 20px;
border: 2px solid #ccc;
border-radius: 4px;
cursor: pointer;
}
input[type="checkbox"]:checked {
background: #3b82f6;
border-color: #3b82f6;
background-image: url("data:image/svg+xml,...");
}
CSS-Only Toggle Switch
<label class="switch">
<input type="checkbox" />
<span class="slider"></span>
</label>
.switch input:checked + .slider {
background-color: #3b82f6;
}
.switch input:checked + .slider::before {
transform: translateX(24px);
}
CSS-Only Accordion
The checkbox hack enables interactive components without JavaScript:
<input type="checkbox" id="section1" hidden />
<label for="section1">Section Title</label>
<div class="content">Content here...</div>
.content {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s;
}
input:checked ~ .content {
max-height: 500px;
}
The :indeterminate State
Checkboxes can have an indeterminate state (set via JavaScript) that represents a partial or unknown selection:
input:indeterminate + label {
color: #999;
font-style: italic;
}
Specificity
:checked has specificity (0, 1, 0) — same as a class selector.
Use Case
The :checked selector powers custom-styled checkboxes and radio buttons, toggle switches, tab interfaces, accordion menus, and CSS-only interactive components. The checkbox hack technique (using :checked with sibling combinators) enables building interactive UI patterns without JavaScript, which is useful for progressive enhancement and lightweight interfaces.