The :checked CSS pseudo-class selector represents any radio (<input type="radio">), checkbox (<input type="checkbox">), or option (<option> in a <select> element) that is checked or toggled to an on state.
:checked
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Try it
label,
input[type="submit"] {
display: block;
margin-top: 1em;
}
input:checked {
border: none;
outline: 2px solid deeppink;
}
<form>
<p>How did you find out about us?</p>
<label
><input name="origin" type="radio" value="google" checked /> Google</label
>
<label><input name="origin" type="radio" value="facebook" /> Facebook</label>
<p>Please agree to our terms:</p>
<label
><input name="newsletter" type="checkbox" checked /> I want to subscribe to
a personalized newsletter.</label
>
<label
><input name="privacy" type="checkbox" /> I have read and I agree to the
Privacy Policy.</label
>
<input type="submit" value="Submit form" />
</form>
The user can engage this state by checking/selecting an element, or disengage it by unchecking/deselecting the element.
Note: Because browsers often treat <option>s as replaced elements, the extent to which they can be styled with the :checked pseudo-class varies from browser to browser. Customizable select element functionality can be used to enable full customization of <option> elements just like any regular DOM element, in supporting browsers.
Syntax
:checked {
/* ... */
}
Examples
Basic example
HTML
<div> <input type="radio" name="my-input" id="yes" value="yes" /> <label for="yes">Yes</label> <input type="radio" name="my-input" id="no" value="no" /> <label for="no">No</label> </div> <div> <input type="checkbox" name="my-checkbox" id="opt-in" /> <label for="opt-in">Check me!</label> </div> <select name="my-select" id="fruit"> <option value="opt1">Apples</option> <option value="opt2">Grapes</option> <option value="opt3">Pears</option> </select>
CSS
div,
select {
margin: 8px;
}
/* Labels for checked inputs */
input:checked + label {
color: red;
}
/* Radio element, when checked */
input[type="radio"]:checked {
box-shadow: 0 0 0 3px orange;
}
/* Checkbox element, when checked */
input[type="checkbox"]:checked {
box-shadow: 0 0 0 3px hotpink;
}
/* Option elements, when selected */
option:checked {
box-shadow: 0 0 0 3px lime;
color: red;
}
Result
Toggling elements with a hidden checkbox
This example utilizes the :checked pseudo-class to let the user toggle content based on the state of a checkbox, all without using JavaScript.
HTML
<input type="checkbox" id="expand-toggle" />
<table>
<thead>
<tr>
<th>Column #1</th>
<th>Column #2</th>
<th>Column #3</th>
</tr>
</thead>
<tbody>
<tr class="expandable">
<td>[more text]</td>
<td>[more text]</td>
<td>[more text]</td>
</tr>
<tr>
<td>[cell text]</td>
<td>[cell text]</td>
<td>[cell text]</td>
</tr>
<tr>
<td>[cell text]</td>
<td>[cell text]</td>
<td>[cell text]</td>
</tr>
<tr class="expandable">
<td>[more text]</td>
<td>[more text]</td>
<td>[more text]</td>
</tr>
<tr class="expandable">
<td>[more text]</td>
<td>[more text]</td>
<td>[more text]</td>
</tr>
</tbody>
</table>
<label for="expand-toggle" id="expand-btn">Toggle hidden rows</label>
CSS
/* Hide the toggle checkbox */
#expand-toggle {
display: none;
}
/* Hide expandable content by default */
.expandable {
visibility: collapse;
background: #ddd;
}
/* Style the button */
#expand-btn {
display: inline-block;
margin-top: 12px;
padding: 5px 11px;
background-color: #ff7;
border: 1px solid;
border-radius: 3px;
}
/* Show hidden content when the checkbox is checked */
#expand-toggle:checked ~ * .expandable {
visibility: visible;
}
/* Style the button when the checkbox is checked */
#expand-toggle:checked ~ #expand-btn {
background-color: #ccc;
}
Result
Specifications
| Specification |
|---|
| HTML # selector-checked |
| Selectors Level 4 # checked-pseudo |
Browser compatibility
| Desktop | Mobile | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | |
:checked |
1On macOS, styling<option> elements has no effect. |
12On macOS, styling<option> elements has no effect. |
1["From Firefox 56,<option> elements cannot be styled.", "On macOS, styling <option> elements has no effect."] |
9On macOS, styling<option> elements has no effect. |
3.1Styling<option> elements has no effect. |
18 | 4From Firefox 56,<option> elements cannot be styled. |
10.1 | 2Styling<option> elements has no effect. |
1.0 | 2 |
See also
- Web forms — working with user data
- Styling web forms
- Related HTML elements:
<input type="checkbox">,<input type="radio">,<select>, and<option> - Replaced elements
© 2005–2024 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/:checked