: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.
The :checked
CSS pseudo-class selector represents any radio (), checkbox (
), or option (
in a
element) that is checked or toggled to an
on
state.
Try it
label,
input[type="submit"] {
display: block;
margin-top: 1em;
}
input:checked {
border: none;
outline: 2px solid deeppink;
}
The user can engage this state by checking/selecting an element, or disengage it by unchecking/deselecting the element.
Note:
Because browsers often treat 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 elements just like any regular DOM element, in supporting browsers.
Syntax
:checked {
/* ... */
}
Examples
Basic example
HTML
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
Column #1
Column #2
Column #3
[more text]
[more text]
[more text]
[cell text]
[cell text]
[cell text]
[cell text]
[cell text]
[cell text]
[more text]
[more text]
[more text]
[more text]
[more text]
[more text]
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
See also
- Web forms — working with user data
- Styling web forms
- Related HTML elements:
,
,
, and
- Replaced elements