基本复选框
所有复选框样式都通过直接在 <input> 上添加 .check 类来实现。它会执行以下操作:
- 使用主题颜色覆盖默认的
<input>外观。 - 通过输入框
::before伪元素上的 CSSmask渲染:checked和不确定状态的图标,因此标记会继承对比色,无需内联 SVG。会根据输入框的状态显示相应的图标。
如果想要替换我们提供的图标,请用你自己的 mask-image URL 覆盖 --bs-check-icon-checked 和 --bs-check-icon-indeterminate CSS 变量(或 --check-icon-* Sass tokens)。
标签和布局由额外的 HTML 和 CSS 处理。没有可见标签的复选框仍然需要一个可供辅助技术使用的无障碍名称,例如使用 aria-label。该名称应说明复选框的作用,例如 aria-label="Select all rows",并替换下面示例中的 ... 占位符。详情请参阅表单概览无障碍部分。
<input type="checkbox" id="check" class="check" checked aria-label="..." />不确定状态
复选框可以通过 JavaScript 手动设置来使用不确定伪类。没有可用的 HTML 属性来指定它。
<input type="checkbox" id="checkIndeterminate" class="check" aria-label="..." />配合表单字段
将 .check 包裹在 .form-field 布局包装器中,并添加你的标签。网格布局会将复选框和标签并排显示。
<div class="form-field">
<input type="checkbox" id="checkLabel" class="check" />
<label for="checkLabel">Example new checkbox</label>
</div>描述
在标签后添加描述或其他内容。我们建议将标签和描述包裹在 .form-field-content 中,以确保适当的间距和对齐。
Supporting description for the above label.
<div class="form-field">
<input type="checkbox" id="checkDescription" class="check" />
<div class="form-field-content">
<label for="checkDescription">Example new checkbox</label>
<small class="form-text">Supporting description for the above label.</small>
</div>
</div>主题颜色
通过向 .check 元素添加 .theme-{color} 类来修改选中复选框的外观。这会将选中的背景和边框颜色设置为对应的主题颜色。
<div class="form-field">
<input type="checkbox" id="checkprimary" class="check theme-primary" checked />
<label for="checkprimary">Example primary checkbox</label>
</div>
<div class="form-field">
<input type="checkbox" id="checkaccent" class="check theme-accent" checked />
<label for="checkaccent">Example accent checkbox</label>
</div>
<div class="form-field">
<input type="checkbox" id="checksuccess" class="check theme-success" checked />
<label for="checksuccess">Example success checkbox</label>
</div>
<div class="form-field">
<input type="checkbox" id="checkdanger" class="check theme-danger" checked />
<label for="checkdanger">Example danger checkbox</label>
</div>
<div class="form-field">
<input type="checkbox" id="checkwarning" class="check theme-warning" checked />
<label for="checkwarning">Example warning checkbox</label>
</div>
<div class="form-field">
<input type="checkbox" id="checkinfo" class="check theme-info" checked />
<label for="checkinfo">Example info checkbox</label>
</div>
<div class="form-field">
<input type="checkbox" id="checkinverse" class="check theme-inverse" checked />
<label for="checkinverse">Example inverse checkbox</label>
</div>
<div class="form-field">
<input type="checkbox" id="checksecondary" class="check theme-secondary" checked />
<label for="checksecondary">Example secondary checkbox</label>
</div>禁用
添加 disabled 属性,关联的 <label> 会自动应用较浅的颜色样式以匹配,帮助指示输入框的状态。
<div class="form-field">
<input type="checkbox" id="checkDisabled" class="check" disabled />
<label for="checkDisabled">Example new checkbox</label>
</div>
<div class="form-field">
<input type="checkbox" id="checkDisabledChecked" class="check" checked disabled />
<label for="checkDisabledChecked">Example new checkbox</label>
</div>
<div class="form-field">
<input type="checkbox" id="checkDisabledIndeterminate" class="check" disabled />
<label for="checkDisabledIndeterminate">Example new checkbox</label>
</div>尺寸
添加 .check-sm 或 .check-lg 使复选框显示得更小或更大。
<div class="form-field">
<input type="checkbox" id="checkSizeSm" class="check check-sm" checked />
<label for="checkSizeSm">Small checkbox</label>
</div>
<div class="form-field">
<input type="checkbox" id="checkSizeMd" class="check" checked />
<label for="checkSizeMd">Default checkbox</label>
</div>
<div class="form-field">
<input type="checkbox" id="checkSizeLg" class="check check-lg" checked />
<label for="checkSizeLg">Large checkbox</label>
</div>CSS
变量
复选框组件的 CSS 变量基于 Sass 变量构建。
// stylelint-disable-next-line scss/dollar-variable-default
$check-tokens: defaults(
(
--check-size: 1.25rem,
--check-margin-block: .125rem,
--check-bg: var(--bg-body),
--check-border-color: var(--border-color),
--check-border-radius: 33%,
--check-icon-checked: #{escape-svg(url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'><path fill='none' stroke='black' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m5.5 10 3 3 6-6'/></svg>"))},
--check-icon-indeterminate: #{escape-svg(url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'><path fill='none' stroke='black' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M6 10h8'/></svg>"))},
--check-checked-bg: var(--control-checked-bg),
--check-checked-border-color: var(--control-checked-border-color),
--check-indeterminate-bg: var(--check-checked-bg),
--check-indeterminate-border-color: var(--check-checked-border-color),
--check-active-bg: var(--control-active-bg),
--check-active-border-color: var(--control-active-border-color),
--check-disabled-bg: var(--control-disabled-bg),
--check-disabled-opacity: var(--control-disabled-opacity),
),
$check-tokens
);