表單
核取方塊
勾選後會填上顏色的原生小方框。
本頁內容
核取方塊是個勾選後會填上顏色的小方框。將 .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>#群組
將相關的核取方塊放在同一個 fieldset 中,並用 legend 元素為群組命名。群組中可勾選任意數量的項目。
群組
<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>#形狀
核取方塊是採用膠囊標籤圓角的正方形,表示這些項目可任意勾選。瀏覽器支援連續圓角時,方框會呈現類似應用程式圖示的輪廓。勾號出現時會短暫過渡到定位,取消勾選時則立即消失。啟用減少動態效果偏好時,勾號只會以透明度變化呈現。
#無障礙
#類別
| 類別 | 說明 |
|---|---|
check | 將核取方塊、選項按鈕或開關與對應文字放在同一標籤列。控制項停用時,標籤列也會套用停用狀態的文字色 |
checkbox | 原生 type="checkbox" 輸入控制項,外觀為方形凹槽,使用小型元件的圓角 token。 |