Definition and Usage
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. The user can change this state by clicking on the element, or selecting a different value, in which case the :checked pseudo-class no longer applies to this element, but will to the relevant one.
Syntax
element:checked { style properties }
Examples
Example selectors
07 | input[type="radio"]:checked { |
11 | input[type="checkbox"]:checked { |
input[type="radio"]:checked - Represents all radio buttons on the page that are checked
input[type="checkbox"]:checked - Represents all checkboxes on the page that are checked
option:checked - Represents all select’s options on the page that are selected
Using hidden checkboxes in order to store some CSS boolean values
The :checked pseudo-class applied to hidden checkboxes appended at the begin of your page could be employed in order to store some dynamic booleans to be used by a CSS rule. The following example shows how to hide/show some expandable elements simply clicking on a button.
04 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
05 | <title>Expandable elements</title> |
06 | <style type="text/css"> |
09 | display: inline-block; |
10 | font: 12px / 13px "Lucida Grande", sans-serif; |
11 | text-shadow: rgba(255, 255, 255, 0.4) 0 1px; |
13 | border: 1px solid rgba(0, 0, 0, 0.6); |
14 | background-color: #969696; |
16 | -moz-border-radius: 3px; |
17 | -webkit-border-radius: 3px; |
19 | -moz-box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white; |
20 | -webkit-box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white; |
21 | box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white; |
23 | #isexpanded:checked ~ #expand-btn, #isexpanded:checked ~ * #expand-btn { |
25 | -moz-box-shadow: inset rgba(0, 0, 0, 0.4) 0 -5px 12px, inset rgba(0, 0, 0, 1) 0 1px 3px, rgba(255, 255, 255, 0.4) 0 1px; |
26 | -webkit-box-shadow: inset rgba(0, 0, 0, 0.4) 0 -5px 12px, inset rgba(0, 0, 0, 1) 0 1px 3px, rgba(255, 255, 255, 0.4) 0 1px; |
27 | box-shadow: inset rgba(0, 0, 0, 0.4) 0 -5px 12px, inset rgba(0, 0, 0, 1) 0 1px 3px, rgba(255, 255, 255, 0.4) 0 1px; |
29 | #isexpanded, .expandable { |
32 | #isexpanded:checked ~ * tr.expandable { |
36 | #isexpanded:checked ~ p.expandable, #isexpanded:checked ~ * p.expandable { |
43 | <input type="checkbox" id="isexpanded" /> |
44 | <h1>Expandable elements</h1> |
47 | <tr><th>Column #1</th><th>Column #2</th><th>Column #3</th></tr> |
50 | <tr class="expandable"><td>[cell text]</td><td>[cell text]</td><td>[cell text]</td></tr> |
51 | <tr><td>[cell text]</td><td>[cell text]</td><td>[cell text]</td></tr> |
52 | <tr><td>[cell text]</td><td>[cell text]</td><td>[cell text]</td></tr> |
53 | <tr class="expandable"><td>[cell text]</td><td>[cell text]</td><td>[cell text]</td></tr> |
54 | <tr class="expandable"><td>[cell text]</td><td>[cell text]</td><td>[cell text]</td></tr> |
57 | <p>[some sample text]</p> |
58 | <p><label for="isexpanded" id="expand-btn">Show hidden elements</label></p> |
59 | <p class="expandable">[another sample text]</p> |
60 | <p>[some sample text]</p> |
Using hidden radioboxes in order to store some CSS boolean values
You can also use the :checked pseudo-class applied to hidden radioboxes in order to build, for example, an image gallery with full-size images shown only when mouse clicks on previews.
Note: For an analogous effect, but based on :hover pseudo-class and without hidden radioboxes, see this demo, taken from the :hover page.
Compatibility
Desktop browsers
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
| Basic support | 1.0 | 1.0 (1.7 or earlier) | 9.0 | 9.0 | 3.1 |
Mobile browsers
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
| Basic support | 2.1 | 1.0 (1.0) | 9.0 | 9.5 | 3.1 |