跳到主题内容 跳到文档导航栏

按钮

将Bootstrap的自定义按钮样式用于表单,对话框等中的操作,并支持多种尺寸,状态等

示例

Bootstrap包含了几个预定义的按钮样式,每个样式都有自己的语义目的,并加入了一些额外的按钮以获得更多的控制。

<button type="button" class="btn btn-primary">Primary</button>
<button type="button" class="btn btn-secondary">Secondary</button>
<button type="button" class="btn btn-success">Success</button>
<button type="button" class="btn btn-danger">Danger</button>
<button type="button" class="btn btn-warning">Warning</button>
<button type="button" class="btn btn-info">Info</button>
<button type="button" class="btn btn-light">Light</button>
<button type="button" class="btn btn-dark">Dark</button>

<button type="button" class="btn btn-link">Link</button>
向辅助技术传达意义

使用颜色来增加意义只会提供一种视觉指示,而不会传达给辅助技术(如屏幕阅读器)的用户。确保由颜色表示的信息在内容本身(例如可见文本)中是明显的,或者通过其他方式包含,例如使用 .visually-hidden类隐藏的其他文本。

禁用文字包装

如果您不希望包装按钮文本,则可以将 .text-nowrap类添加到按钮。在Sass中,可以将 $btn-white-space: nowrap设置为禁用每个按钮的文本包装。

按钮标签

这些.btn类设计用于<button>元素。但是,您也可以在<a><input>元素上使用这些类(尽管某些浏览器可能应用稍微不同的呈现)。

<a>元素上使用按钮类来触发页内功能(如折叠内容),而不是链接到当前页面中的新页面或新部分时,应为这些链接赋予role="button"以适当地将其目的传达给屏幕阅读器等辅助技术。

Link
<a class="btn btn-primary" href="#" role="button">Link</a>
<button class="btn btn-primary" type="submit">Button</button>
<input class="btn btn-primary" type="button" value="Input">
<input class="btn btn-primary" type="submit" value="Submit">
<input class="btn btn-primary" type="reset" value="Reset">

轮廓按钮

需要一个按钮,却不需要它们带来的厚重背景色?将默认修饰符类替换为.btn-outline-*类,以删除任何按钮上的所有背景图像和颜色。

<button type="button" class="btn btn-outline-primary">Primary</button>
<button type="button" class="btn btn-outline-secondary">Secondary</button>
<button type="button" class="btn btn-outline-success">Success</button>
<button type="button" class="btn btn-outline-danger">Danger</button>
<button type="button" class="btn btn-outline-warning">Warning</button>
<button type="button" class="btn btn-outline-info">Info</button>
<button type="button" class="btn btn-outline-light">Light</button>
<button type="button" class="btn btn-outline-dark">Dark</button>
有些按钮样式使用相对较浅的前景色,为了有足够的对比度,只能在深色背景上使用。

尺寸

喜欢大的还是小的按钮?添加.btn-lg.btn-sm以获得其他尺寸。

<button type="button" class="btn btn-primary btn-lg">Large button</button>
<button type="button" class="btn btn-secondary btn-lg">Large button</button>
<button type="button" class="btn btn-primary btn-sm">Small button</button>
<button type="button" class="btn btn-secondary btn-sm">Small button</button>

禁用状态

通过向任何<button>元素添加disabled的布尔属性,使按钮看起来不活动。 禁用的按钮具有pointer-events: none,防止触发悬停和活动状态。

<button type="button" class="btn btn-lg btn-primary" disabled>Primary button</button>
<button type="button" class="btn btn-secondary btn-lg" disabled>Button</button>

使用<a>元素禁用的按钮的行为有些不同:

  • <a>不支持disabled属性,因此必须添加.disabled类以使其在视觉上显示为禁用。
  • 包括一些未来友好的样式来禁用锚按钮上的所有pointer-events
  • 禁用的按钮应该包含aria-disabled="true"属性,以指示辅助技术的元素状态。
<a href="#" class="btn btn-primary btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">Primary link</a>
<a href="#" class="btn btn-secondary btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">Link</a>

这些.disabled类使用pointer-events: none来尝试禁用<a>的链接功能,但是CSS属性还没有标准化。 此外,即使在支持pointer-events: none的浏览器中,键盘导航也不会受到影响,这意味着有视力的键盘用户和辅助技术用户仍然能够激活这些链接。因此,为了安全起见,除了aria-disabled="true"之外,还要在这些链接上包含tabindex="-1"属性,以防止它们接收键盘焦点,并使用自定义JavaScript完全禁用它们的功能。

块按钮

混合使用我们的显示和间隙实用程序,创建与Bootstrap 4中类似的全宽度“块按钮”响应式堆栈。通过使用实用程序而不是按钮特定的类,我们可以更好地控制间距,对齐方式和响应行为。

<div class="d-grid gap-2">
  <button class="btn btn-primary" type="button">Button</button>
  <button class="btn btn-primary" type="button">Button</button>
</div>

在这里,我们创建一个响应变体,从垂直堆叠的按钮开始,直到md断点,其中.d-md-block替换 .d-grid类,从而使gap-2实用程序无效。调整浏览器大小以查看更改。

<div class="d-grid gap-2 d-md-block">
  <button class="btn btn-primary" type="button">Button</button>
  <button class="btn btn-primary" type="button">Button</button>
</div>

可以使用网格列宽类调整块按钮的宽度。例如,对于半宽度的“块按钮”,使用.col-6。也可以用.mx-auto将其水平居中。

<div class="d-grid gap-2 col-6 mx-auto">
  <button class="btn btn-primary" type="button">Button</button>
  <button class="btn btn-primary" type="button">Button</button>
</div>

其他实用程序可用于调整按钮水平时的对齐方式。在这里,我们以前面的响应示例为例,在按钮上添加了一些flex实用程序和margin实用程序,以便在按钮不再堆叠时将其右对齐。

<div class="d-grid gap-2 d-md-flex justify-content-md-end">
  <button class="btn btn-primary me-md-2" type="button">Button</button>
  <button class="btn btn-primary" type="button">Button</button>
</div>

按钮插件

按钮插件允许您创建简单的开/关切换按钮。

从视觉上看,这些切换按钮与复选框切换按钮相同。然而,辅助技术传达的方式不同:屏幕阅读器会将复选框切换宣布为“选中”/“未选中”(因为尽管它们看起来很像复选框,但它们基本上仍然是复选框),而这些切换按钮将宣布为“按钮”/“按下按钮”。这两种方法之间的选择将取决于您正在创建的切换类型,以及当作为复选框或实际按钮发布时,切换对用户是否有意义。

切换状态

添加 data-bs-toggle="button"切换按钮的 active状态。如果要预先切换按钮,则必须手动添加.active aria-pressed="true",以确保将其适当地传递给辅助技术。

<button type="button" class="btn btn-primary" data-bs-toggle="button" autocomplete="off">Toggle button</button>
<button type="button" class="btn btn-primary active" data-bs-toggle="button" autocomplete="off" aria-pressed="true">Active toggle button</button>
<button type="button" class="btn btn-primary" disabled data-bs-toggle="button" autocomplete="off">Disabled toggle button</button>
<a href="#" class="btn btn-primary" role="button" data-bs-toggle="button">Toggle link</a>
<a href="#" class="btn btn-primary active" role="button" data-bs-toggle="button" aria-pressed="true">Active toggle link</a>
<a href="#" class="btn btn-primary disabled" tabindex="-1" aria-disabled="true" role="button" data-bs-toggle="button">Disabled toggle link</a>

方法

可以使用按钮构造函数创建按钮实例,例如:

var button = document.getElementById('myButton')
var bsButton = new bootstrap.Button(button)
方法 描述
toggle 切换推送状态。使按钮看起来已被激活。
dispose 销毁元素的按钮。(删除DOM元素上存储的数据)

例如,切换所有按钮

var buttons = document.querySelectorAll('.btn')
buttons.forEach(function (button) {
  var button = new bootstrap.Button(button)
  button.toggle()
})