--- title: ':checked' slug: 'Web/CSS/:checked' translation_of: 'Web/CSS/:checked' ---
:checked CSS 伪类选择器表示任何处于选中状态的radio(<input type="radio">), checkbox (<input type="checkbox">) 或("select") 元素中的option HTML元素("option")。
/* 匹配任意被勾选/选中的radio(单选按钮),checkbox(复选框),或者option(select中的一项) */
:checked {
margin-left: 25px;
border: 1px solid blue;
}
用户通过勾选/选中元素或取消勾选/取消选中,来改变该元素的 :checked 状态。
Note:因为浏览器经常将<option>视为可替换元素,因此不同的浏览器通过:checked伪类渲染出来的效果也不尽相同.
<div> <input type="radio" name="my-input" id="yes"> <label for="yes">Yes</label> <input type="radio" name="my-input" id="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>
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;
}
{{EmbedLiveSample("Basic_example")}}
这个例子利用了:checked伪类,让用户基于复选框的状态切换内容,而无需使用JavaScript。
<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>
/* 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;
}
{{EmbedLiveSample("Toggling_elements_with_a_hidden_checkbox", "auto", 220)}}
同时,可以使用隐藏的radioboxes中的:checked伪类来构建一个只有在鼠标单击“预览”时,图片才会以全尺寸展示的图片相册,查看演示。
| 规格 | 级别 | 附注 |
|---|---|---|
| {{ SpecName('HTML WHATWG', '#selector-checked', ':checked') }} | {{ Spec2('HTML WHATWG') }} | No change. |
| {{ SpecName('HTML5 W3C', '#selector-checked', ':checked') }} | {{ Spec2('HTML5 W3C') }} | Defines the semantic regarding HTML. |
| {{ SpecName('CSS4 Selectors', '#checked', ':checked') }} | {{ Spec2('CSS4 Selectors') }} | No change. |
| {{ SpecName('CSS3 Basic UI', '#pseudo-checked', ':checked') }} | {{ Spec2('CSS3 Basic UI') }} | Link to Selectors Level 3. |
| {{ SpecName('CSS3 Selectors', '#checked', ':checked') }} | {{ Spec2('CSS3 Selectors') }} | Defines the pseudo-class, but not the associated semantic |
{{Compat("css.selectors.checked")}}