表单
复选框
小型原生凹槽,勾选后填充墨色。
本页内容
复选框是一个小凹槽,选中时填入墨色。将 .checkbox 用于原生 <input type="checkbox">,再用 <label class="check"> 包住复选框及其文字,让整行都可点击。每个复选框都可独立选中或取消选中。若要从多个选项中只选一项,请使用单选按钮。
复选框
<label class="check"> <input type="checkbox" class="checkbox" checked> Email me about releases</label><label class="check"><input type="checkbox" class="checkbox"> Weekly digest</label><> <label className="check"> <input type="checkbox" className="checkbox" defaultChecked /> Email me about releases </label> <label className="check"><input type="checkbox" className="checkbox" /> Weekly digest</label></><label class="check"> <input type="checkbox" class="checkbox" checked> Email me about releases</label><label class="check"><input type="checkbox" class="checkbox"> Weekly digest</label><template> <label class="check"> <input type="checkbox" class="checkbox" checked> Email me about releases </label> <label class="check"><input type="checkbox" class="checkbox"> Weekly digest</label></template>#分组
将相关的复选框放在带有组标题(legend 元素)的 fieldset 中,为整组提供名称。组内可选中任意数量的复选框。
分组
<fieldset class="fieldset"> <legend class="fieldset-legend">Notify me about</legend> <label class="check"> <input type="checkbox" class="checkbox" name="notify" value="failed" checked> Failed deployments </label> <label class="check"> <input type="checkbox" class="checkbox" name="notify" value="certificates" checked> Expiring certificates </label> <label class="check"> <input type="checkbox" class="checkbox" name="notify" value="news"> Product news </label></fieldset><fieldset className="fieldset"> <legend className="fieldset-legend">Notify me about</legend> <label className="check"> <input type="checkbox" className="checkbox" name="notify" value="failed" defaultChecked /> Failed deployments </label> <label className="check"> <input type="checkbox" className="checkbox" name="notify" value="certificates" defaultChecked /> Expiring certificates </label> <label className="check"> <input type="checkbox" className="checkbox" name="notify" value="news" /> Product news </label></fieldset><fieldset class="fieldset"> <legend class="fieldset-legend">Notify me about</legend> <label class="check"> <input type="checkbox" class="checkbox" name="notify" value="failed" checked> Failed deployments </label> <label class="check"> <input type="checkbox" class="checkbox" name="notify" value="certificates" checked> Expiring certificates </label> <label class="check"> <input type="checkbox" class="checkbox" name="notify" value="news"> Product news </label></fieldset><template> <fieldset class="fieldset"> <legend class="fieldset-legend">Notify me about</legend> <label class="check"> <input type="checkbox" class="checkbox" name="notify" value="failed" checked> Failed deployments </label> <label class="check"> <input type="checkbox" class="checkbox" name="notify" value="certificates" checked> Expiring certificates </label> <label class="check"> <input type="checkbox" class="checkbox" name="notify" value="news"> Product news </label> </fieldset></template>#长标签
复选框与标签的第一行垂直居中对齐,标签换行后仍保持这一位置,因此标签可以是一整句话。
长标签
<label class="check max-w(320)"> <input type="checkbox" class="checkbox" required> I have read the terms of service and understand that deployments on the free plan pause after 30 days without traffic.</label><label className="check max-w(320)"> <input type="checkbox" className="checkbox" required /> I have read the terms of service and understand that deployments on the free plan pause after 30 days without traffic.</label><label class="check max-w(320)"> <input type="checkbox" class="checkbox" required> I have read the terms of service and understand that deployments on the free plan pause after 30 days without traffic.</label><template> <label class="check max-w(320)"> <input type="checkbox" class="checkbox" required> I have read the terms of service and understand that deployments on the free plan pause after 30 days without traffic. </label></template>选项需要单独的标题和说明时,请使用选项组件。
#不确定状态
当子项只有一部分被选中时,父级复选框显示一条短横线。通过脚本设置 indeterminate,HTML 没有对应的属性。
不确定状态
<label class="check"><input type="checkbox" class="checkbox" id="select-all"> All regions</label><label class="check ml-lg"><input type="checkbox" class="checkbox" checked> Tokyo</label><label class="check ml-lg"><input type="checkbox" class="checkbox"> Frankfurt</label><script>document.getElementById('select-all').indeterminate = true</script>#禁用状态
禁用时,复选框变淡,标签改用禁用状态的墨色。
禁用状态
<label class="check"> <input type="checkbox" class="checkbox" checked disabled> Required by your plan</label><label class="check"> <input type="checkbox" class="checkbox" disabled> Not available in your region</label><> <label className="check"> <input type="checkbox" className="checkbox" defaultChecked disabled /> Required by your plan </label> <label className="check"> <input type="checkbox" className="checkbox" disabled /> Not available in your region </label></><label class="check"> <input type="checkbox" class="checkbox" checked disabled> Required by your plan</label><label class="check"> <input type="checkbox" class="checkbox" disabled> Not available in your region</label><template> <label class="check"> <input type="checkbox" class="checkbox" checked disabled> Required by your plan </label> <label class="check"> <input type="checkbox" class="checkbox" disabled> Not available in your region </label></template>#形状
复选框是一个采用胶囊标签圆角的方形,表示这些选项均可独立选择。浏览器支持绘制连续圆角时,方形会呈现应用图标的轮廓。勾号出现时有短暂的缓动,取消选中时立即消失。启用减少动态效果时,勾号只做淡入淡出。
#无障碍
- 使用原生输入控件,按 空格 键可切换选中状态,控件也会参与表单提交。
- 没有可见文字的复选框需要
aria-label,例如表格行中的复选框。 - 如果设置一经更改就立即生效,无需点击保存按钮,请使用开关。
#类名
| 类名 | 说明 |
|---|---|
check | 用一行标签包裹复选框、单选按钮或开关及其文字。控件禁用时,标签使用禁用状态的墨色 |
checkbox | 原生 type="checkbox" input,呈方形凹槽,采用与胶囊标签相同的圆角。 |