:checked
:checked CSS 伪类选择器表示任何处于选中或开启状态的单选按钮(<input type="radio">)、复选框(<input type="checkbox">)或选项(<option>,位于 <select> 元素中)。
试一试
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>
用户可以通过选中/选择元素来启用此状态,或通过取消选中/取消选择元素来禁用此状态。
语法
css
:checked {
/* ... */
}
示例
基本示例
HTML
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
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;
}
结果
使用隐藏的复选框切换元素
此示例利用 :checked 伪类,让用户根据复选框的状态切换内容,所有这些都无需使用 JavaScript。
HTML
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
css
/* Hide the toggle checkbox */
#expand-toggle {
display: none;
}
/* Hide expandable content by default */
.expandable {
visibility: collapse;
background: #dddddd;
}
/* Style the button */
#expand-btn {
display: inline-block;
margin-top: 12px;
padding: 5px 11px;
background-color: #ffff77;
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: #cccccc;
}
结果
规范
| 规范 |
|---|
| HTML # selector-checked |
| 选择器 Level 4 # checked-pseudo |
浏览器兼容性
加载中…