Definition and Usage
The :checked pseudo-class selector selects any radio button (<input type="radio">) or checkbox (<input type="checkbox">) that is checked.
It is used to dynamically change styles based on the checked state of radio buttons or checkboxes to create interactive user experiences.
Note
The specification states that, in addition to radio buttons and checkboxes, an <option> element within a <select> element can also be matched by the :checked pseudo-class selector when selected.
However, because the extent to which these elements can be styled varies significantly across browsers, and some browsers do not support styling them at all, they are difficult to use in practice. For this reason, they are not covered in this article.
See Browser Compatibility.
Syntax
:checked {
/* ... */
}
Basic Example
The following example shows how to select radio buttons (<input type="radio">) and checkboxes (<input type="checkbox">) when they are checked:
<form>
<fieldset>
<legend>Select an Interesting Topic</legend>
<input type="radio" name="favorite" id="html" checked>
<label for="html">HTML</label>
<br>
<input type="radio" name="favorite" id="css">
<label for="css">CSS</label>
</fieldset>
<fieldset>
<legend>Privacy Policy Agreement</legend>
<input type="checkbox" name="privacy" id="privacy" checked>
<label for="privacy">Yes, I agree.</label>
</fieldset>
</form>
/* Select checked input elements */
input:checked {
border: none;
outline: 3px solid red;
}
/* Select checked input elements and
style the label element immediately following them */
input:checked + label {
background-color: yellowgreen;
color: white;
}
Code Explanation
For :checked + label, see CSS Next-Sibling Combinator (+) – Select the Next Sibling Element.
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
:checked
|
1 | 12 | 1 | 3.1 |
- From Firefox 56,
<option>elements cannot be styled. - On macOS, styling
<option>elements has no effect. - In Safari on iOS, styling
<option>elements has no effect.
Specifications
| Specification | |
|---|---|
:checked
|
HTML #selector-checked |
:checked
|
Selectors Level 4 #checked |