跳到主要内容

复选框与单选框

使用我们完全重写的复选框与单选按钮,创建跨浏览器和跨设备一致的复选框和单选按钮。

方法

浏览器默认的复选框和单选框在...的帮助下被替换 .form-check一系列针对两种输入类型的类,用于改进其 HTML 元素的布局和行为,提供更大的自定义性和跨浏览器一致性。复选框用于在列表中选择一个或多个选项,而单选按钮用于从多个选项中选择一个。

在结构上,我们的 <input>和 <label>s 是兄弟元素,而不是一个 <input> 在……之内 <label>. 这稍微更啰嗦,因为你必须指定 id 和 for 与……相关的属性 <input> 和 <label>. 我们使用兄弟选择器(~) 对我们所有的 <input> 州,比如 :checked 或 :disabled. 当与...结合时 .form-check-label 类,我们可以轻松地根据每个项目来设置文本样式 <input>的状态。

我们的复选框使用自定义的 Bootstrap 图标来表示选中或不确定状态。

检查

HTML
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="checkDefault">
  <label class="form-check-label" for="checkDefault">
    Default checkbox
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="checkChecked" checked>
  <label class="form-check-label" for="checkChecked">
    Checked checkbox
  </label>
</div>

不确定

复选框在通过 JavaScript 手动设置时可以使用 :indeterminate 伪类(没有可用的 HTML 属性来指定它)。

HTML
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="checkIndeterminate">
  <label class="form-check-label" for="checkIndeterminate">
    Indeterminate checkbox
  </label>
</div>

禁用

添加 disabled 属性后,相关的 <label> 会自动应用更浅的颜色样式,以帮助指示输入的状态。

HTML
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="checkIndeterminateDisabled" disabled>
  <label class="form-check-label" for="checkIndeterminateDisabled">
    Disabled indeterminate checkbox
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="checkDisabled" disabled>
  <label class="form-check-label" for="checkDisabled">
    Disabled checkbox
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="checkCheckedDisabled" checked disabled>
  <label class="form-check-label" for="checkCheckedDisabled">
    Disabled checked checkbox
  </label>
</div>

单选框

HTML
<div class="form-check">
  <input class="form-check-input" type="radio" name="radioDefault" id="radioDefault1">
  <label class="form-check-label" for="radioDefault1">
    Default radio
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="radio" name="radioDefault" id="radioDefault2" checked>
  <label class="form-check-label" for="radioDefault2">
    Default checked radio
  </label>
</div>

禁用

添加 disabled 属性后,相关的 <label> 会自动应用更浅的颜色样式,以帮助指示输入的状态。

HTML
<div class="form-check">
  <input class="form-check-input" type="radio" name="radioDisabled" id="radioDisabled" disabled>
  <label class="form-check-label" for="radioDisabled">
    Disabled radio
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="radio" name="radioDisabled" id="radioCheckedDisabled" checked disabled>
  <label class="form-check-label" for="radioCheckedDisabled">
    Disabled checked radio
  </label>
</div>

开关

开关具有自定义复选框的标记,但使用 .form-switch 类来呈现切换开关。考虑使用 role="switch" 以更准确地向支持该角色的辅助技术传达控件的性质。在较旧的辅助技术中,它将作为常规复选框进行简单宣布作为回退。开关还支持 disabled 属性。

HTML
<div class="form-check form-switch">
  <input class="form-check-input" type="checkbox" role="switch" id="switchCheckDefault">
  <label class="form-check-label" for="switchCheckDefault">Default switch checkbox input</label>
</div>
<div class="form-check form-switch">
  <input class="form-check-input" type="checkbox" role="switch" id="switchCheckChecked" checked>
  <label class="form-check-label" for="switchCheckChecked">Checked switch checkbox input</label>
</div>
<div class="form-check form-switch">
  <input class="form-check-input" type="checkbox" role="switch" id="switchCheckDisabled" disabled>
  <label class="form-check-label" for="switchCheckDisabled">Disabled switch checkbox input</label>
</div>
<div class="form-check form-switch">
  <input class="form-check-input" type="checkbox" role="switch" id="switchCheckCheckedDisabled" checked disabled>
  <label class="form-check-label" for="switchCheckCheckedDisabled">Disabled checked switch checkbox input</label>
</div>

原生开关

通过在输入中添加switch属性,逐步增强你在移动 Safari(iOS 17.4)上的开关,以在切换开关时启用触觉反馈,就像原生 iOS 开关一样。在 Bootstrap 中使用此属性不会带来样式更改,因为我们所有的开关都使用自定义样式。

HTML
<div class="form-check form-switch">
  <input class="form-check-input" type="checkbox" value="" id="checkNativeSwitch" switch>
  <label class="form-check-label" for="checkNativeSwitch">
    Native switch haptics
  </label>
</div>

一定要在 WebKit 博客上阅读更多关于开关属性的信息。macOS 和 iOS 上的 Safari 17.4 都在 HTML 中提供原生风格的开关,而其他浏览器则仅回退到标准复选框外观。在更新版本的 Safari 中,将此属性应用于非 Bootstrap 复选框将呈现原生开关。

默认(堆叠)

默认情况下,任何数量的复选框和单选框,如果是直接兄弟元素,将会垂直堆叠并使用 .form-check 适当间隔。

HTML
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
  <label class="form-check-label" for="defaultCheck1">
    Default checkbox
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="defaultCheck2" disabled>
  <label class="form-check-label" for="defaultCheck2">
    Disabled checkbox
  </label>
</div>
HTML
<div class="form-check">
  <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
  <label class="form-check-label" for="exampleRadios1">
    Default radio
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="option2">
  <label class="form-check-label" for="exampleRadios2">
    Second default radio
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios3" value="option3" disabled>
  <label class="form-check-label" for="exampleRadios3">
    Disabled radio
  </label>
</div>

内联

通过添加在同一水平行上的组复选框或单选按钮 .form-check-inline 到任何 .form-check.

HTML
<div class="form-check form-check-inline">
  <input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1">
  <label class="form-check-label" for="inlineCheckbox1">1</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="checkbox" id="inlineCheckbox2" value="option2">
  <label class="form-check-label" for="inlineCheckbox2">2</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="checkbox" id="inlineCheckbox3" value="option3" disabled>
  <label class="form-check-label" for="inlineCheckbox3">3 (disabled)</label>
</div>
HTML
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1">
  <label class="form-check-label" for="inlineRadio1">1</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">
  <label class="form-check-label" for="inlineRadio2">2</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3" disabled>
  <label class="form-check-label" for="inlineRadio3">3 (disabled)</label>
</div>

反转

使用 .form-check-reverse 修饰类将你的复选框、单选按钮和开关放在相反的一侧。

HTML
<div class="form-check form-check-reverse">
  <input class="form-check-input" type="checkbox" value="" id="reverseCheck1">
  <label class="form-check-label" for="reverseCheck1">
    Reverse checkbox
  </label>
</div>
<div class="form-check form-check-reverse">
  <input class="form-check-input" type="checkbox" value="" id="reverseCheck2" disabled>
  <label class="form-check-label" for="reverseCheck2">
    Disabled reverse checkbox
  </label>
</div>

<div class="form-check form-switch form-check-reverse"> <input class="form-check-input" type="checkbox" id="switchCheckReverse"> <label class="form-check-label" for="switchCheckReverse">Reverse switch checkbox input</label> </div>

无标签

对于没有标签文本的复选框和单选按钮,请省略包裹的 .form-check。请记住仍然需要为辅助技术提供某种可访问名称(例如,使用 aria-label)。详细信息请参见 表单概述可访问性 部分。

HTML
<div>
  <input class="form-check-input" type="checkbox" id="checkboxNoLabel" value="" aria-label="...">
</div>

<div> <input class="form-check-input" type="radio" name="radioNoLabel" id="radioNoLabel1" value="" aria-label="..."> </div>

切换按钮

通过使用 .btn 样式而不是 .form-check-label 样式在 <label> 元素上创建类似按钮的复选框和单选按钮。如果需要,这些切换按钮可以进一步分组在 按钮组 中。

复选框切换按钮

HTML
<input type="checkbox" class="btn-check" id="btn-check" autocomplete="off">
<label class="btn btn-primary" for="btn-check">Single toggle</label>

<input type="checkbox" class="btn-check" id="btn-check-2" checked autocomplete="off"> <label class="btn btn-primary" for="btn-check-2">Checked</label>

<input type="checkbox" class="btn-check" id="btn-check-3" autocomplete="off" disabled> <label class="btn btn-primary" for="btn-check-3">Disabled</label>

HTML
<input type="checkbox" class="btn-check" id="btn-check-4" autocomplete="off">
<label class="btn" for="btn-check-4">Single toggle</label>

<input type="checkbox" class="btn-check" id="btn-check-5" checked autocomplete="off"> <label class="btn" for="btn-check-5">Checked</label>

<input type="checkbox" class="btn-check" id="btn-check-6" autocomplete="off" disabled> <label class="btn" for="btn-check-6">Disabled</label>

从视觉上看,这些复选框切换按钮与按钮插件切换按钮完全相同。然而,它们在辅助技术中的表现方式不同:屏幕阅读器将以“已选中/未选中”来朗读复选框切换按钮(因为尽管外观如此,它们本质上仍然是复选框),而按钮插件切换按钮将被朗读为“按钮/按钮已按下”。在这两种方法之间的选择取决于你所创建的切换类型,以及当切换被朗读为复选框或实际按钮时是否对用户有意义。

单选按钮切换

HTML
<input type="radio" class="btn-check" name="options" id="option1" autocomplete="off" checked>
<label class="btn btn-secondary" for="option1">Checked</label>

<input type="radio" class="btn-check" name="options" id="option2" autocomplete="off"> <label class="btn btn-secondary" for="option2">Radio</label>

<input type="radio" class="btn-check" name="options" id="option3" autocomplete="off" disabled> <label class="btn btn-secondary" for="option3">Disabled</label>

<input type="radio" class="btn-check" name="options" id="option4" autocomplete="off"> <label class="btn btn-secondary" for="option4">Radio</label>

HTML
<input type="radio" class="btn-check" name="options-base" id="option5" autocomplete="off" checked>
<label class="btn" for="option5">Checked</label>

<input type="radio" class="btn-check" name="options-base" id="option6" autocomplete="off"> <label class="btn" for="option6">Radio</label>

<input type="radio" class="btn-check" name="options-base" id="option7" autocomplete="off" disabled> <label class="btn" for="option7">Disabled</label>

<input type="radio" class="btn-check" name="options-base" id="option8" autocomplete="off"> <label class="btn" for="option8">Radio</label>

轮廓样式

支持.btn的不同变体,例如各种轮廓样式。



HTML
<input type="checkbox" class="btn-check" id="btn-check-outlined" autocomplete="off">
<label class="btn btn-outline-primary" for="btn-check-outlined">Single toggle</label><br>

<input type="checkbox" class="btn-check" id="btn-check-2-outlined" checked autocomplete="off"> <label class="btn btn-outline-secondary" for="btn-check-2-outlined">Checked</label><br>

<input type="radio" class="btn-check" name="options-outlined" id="success-outlined" autocomplete="off" checked> <label class="btn btn-outline-success" for="success-outlined">Checked success radio</label>

<input type="radio" class="btn-check" name="options-outlined" id="danger-outlined" autocomplete="off"> <label class="btn btn-outline-danger" for="danger-outlined">Danger radio</label>

CSS

Sass 变量

检查变量:

$form-check-input-width:                  1em;
$form-check-min-height:                   $font-size-base * $line-height-base;
$form-check-padding-start:                $form-check-input-width + .5em;
$form-check-margin-bottom:                .125rem;
$form-check-label-color:                  null;
$form-check-label-cursor:                 null;
$form-check-transition:                   null;

$form-check-input-active-filter: brightness(90%);

$form-check-input-bg: $input-bg; $form-check-input-border: var(--#{$prefix}border-width) solid var(--#{$prefix}border-color); $form-check-input-border-radius: .25em; $form-check-radio-border-radius: 50%; $form-check-input-focus-border: $input-focus-border-color; $form-check-input-focus-box-shadow: $focus-ring-box-shadow;

$form-check-input-checked-color: $component-active-color; $form-check-input-checked-bg-color: $component-active-bg; $form-check-input-checked-border-color: $form-check-input-checked-bg-color; $form-check-input-checked-bg-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'><path fill='none' stroke='#{$form-check-input-checked-color}' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='m6 10 3 3 6-6'/></svg>"); $form-check-radio-checked-bg-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'><circle r='2' fill='#{$form-check-input-checked-color}'/></svg>");

$form-check-input-indeterminate-color: $component-active-color; $form-check-input-indeterminate-bg-color: $component-active-bg; $form-check-input-indeterminate-border-color: $form-check-input-indeterminate-bg-color; $form-check-input-indeterminate-bg-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'><path fill='none' stroke='#{$form-check-input-indeterminate-color}' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='M6 10h8'/></svg>");

$form-check-input-disabled-opacity: .5; $form-check-label-disabled-opacity: $form-check-input-disabled-opacity; $form-check-btn-check-disabled-opacity: $btn-disabled-opacity;

$form-check-inline-margin-end: 1rem;

开关的变量:

$form-switch-color:               rgba($black, .25);
$form-switch-width:               2em;
$form-switch-padding-start:       $form-switch-width + .5em;
$form-switch-bg-image:            url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'><circle r='3' fill='#{$form-switch-color}'/></svg>");
$form-switch-border-radius:       $form-switch-width;
$form-switch-transition:          background-position .15s ease-in-out;

$form-switch-focus-color: $input-focus-border-color; $form-switch-focus-bg-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'><circle r='3' fill='#{$form-switch-focus-color}'/></svg>");

$form-switch-checked-color: $component-active-color; $form-switch-checked-bg-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'><circle r='3' fill='#{$form-switch-checked-color}'/></svg>"); $form-switch-checked-bg-position: right center;