Examples

Bootstrap includes several predefined button styles, each serving its own semantic purpose, with a few extras thrown in for more control.

Buttons - 图1

  1. <button type="button" class="btn btn-primary">Primary</button>
  2. <button type="button" class="btn btn-secondary">Secondary</button>
  3. <button type="button" class="btn btn-success">Success</button>
  4. <button type="button" class="btn btn-danger">Danger</button>
  5. <button type="button" class="btn btn-warning">Warning</button>
  6. <button type="button" class="btn btn-info">Info</button>
  7. <button type="button" class="btn btn-light">Light</button>
  8. <button type="button" class="btn btn-dark">Dark</button>
  9. <button type="button" class="btn btn-link">Link</button>
Conveying meaning to assistive technologies

Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies – such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (e.g. the visible text), or is included through alternative means, such as additional text hidden with the .visually-hidden class.

Disable text wrapping

If you don’t want the button text to wrap, you can add the .text-nowrap class to the button. In Sass, you can set $btn-white-space: nowrap to disable text wrapping for each button.

Button tags

The .btn classes are designed to be used with the <button> element. However, you can also use these classes on <a> or <input> elements (though some browsers may apply a slightly different rendering).

When using button classes on <a> elements that are used to trigger in-page functionality (like collapsing content), rather than linking to new pages or sections within the current page, these links should be given a role="button" to appropriately convey their purpose to assistive technologies such as screen readers.

Buttons - 图2

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

Outline buttons

In need of a button, but not the hefty background colors they bring? Replace the default modifier classes with the .btn-outline-* ones to remove all background images and colors on any button.

Buttons - 图3

  1. <button type="button" class="btn btn-outline-primary">Primary</button>
  2. <button type="button" class="btn btn-outline-secondary">Secondary</button>
  3. <button type="button" class="btn btn-outline-success">Success</button>
  4. <button type="button" class="btn btn-outline-danger">Danger</button>
  5. <button type="button" class="btn btn-outline-warning">Warning</button>
  6. <button type="button" class="btn btn-outline-info">Info</button>
  7. <button type="button" class="btn btn-outline-light">Light</button>
  8. <button type="button" class="btn btn-outline-dark">Dark</button>

Some of the button styles use a relatively light foreground color, and should only be used on a dark background in order to have sufficient contrast.

Sizes

Fancy larger or smaller buttons? Add .btn-lg or .btn-sm for additional sizes.

Buttons - 图4

  1. <button type="button" class="btn btn-primary btn-lg">Large button</button>
  2. <button type="button" class="btn btn-secondary btn-lg">Large button</button>

Buttons - 图5

  1. <button type="button" class="btn btn-primary btn-sm">Small button</button>
  2. <button type="button" class="btn btn-secondary btn-sm">Small button</button>

Disabled state

Make buttons look inactive by adding the disabled boolean attribute to any <button> element. Disabled buttons have pointer-events: none applied to, preventing hover and active states from triggering.

Buttons - 图6

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

Disabled buttons using the <a> element behave a bit different:

  • <a>s don’t support the disabled attribute, so you must add the .disabled class to make it visually appear disabled.
  • Some future-friendly styles are included to disable all pointer-events on anchor buttons.
  • Disabled buttons should include the aria-disabled="true" attribute to indicate the state of the element to assistive technologies.

Buttons - 图7

  1. <a href="#" class="btn btn-primary btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">Primary link</a>
  2. <a href="#" class="btn btn-secondary btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">Link</a>
Link functionality caveat

The .disabled class uses pointer-events: none to try to disable the link functionality of <a>s, but that CSS property is not yet standardized. In addition, even in browsers that do support pointer-events: none, keyboard navigation remains unaffected, meaning that sighted keyboard users and users of assistive technologies will still be able to activate these links. So to be safe, in addition to aria-disabled="true", also include a tabindex="-1" attribute on these links to prevent them from receiving keyboard focus, and use custom JavaScript to disable their functionality altogether.

Block buttons

Create responsive stacks of full-width, “block buttons” like those in Bootstrap 4 with a mix of our display and gap utilities. By using utilities instead of button specific classes, we have much greater control over spacing, alignment, and responsive behaviors.

Buttons - 图8

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

Here we create a responsive variation, starting with vertically stacked buttons until the md breakpoint, where .d-md-block replaces the .d-grid class, thus nullifying the gap-2 utility. Resize your browser to see them change.

Buttons - 图9

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

You can adjust the width of your block buttons with grid column width classes. For example, for a half-width “block button”, use .col-6. Center it horizontally with .mx-auto, too.

Buttons - 图10

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

Additional utilities can be used to adjust the alignment of buttons when horizontal. Here we’ve taken our previous responsive example and added some flex utilities and a margin utility on the button to right align the buttons when they’re no longer stacked.

Buttons - 图11

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

Button plugin

The button plugin allows you to create simple on/off toggle buttons.

Visually, these toggle buttons are identical to the checkbox toggle buttons. However, they are conveyed differently by assistive technologies: the checkbox toggles will be announced by screen readers as “checked”/“not checked” (since, despite their appearance, they are fundamentally still checkboxes), whereas these toggle buttons will be announced as “button”/“button pressed”. The choice between these two approaches will depend on the type of toggle you are creating, and whether or not the toggle will make sense to users when announced as a checkbox or as an actual button.

Toggle states

Add data-bs-toggle="button" to toggle a button’s active state. If you’re pre-toggling a button, you must manually add the .active class and aria-pressed="true" to ensure that it is conveyed appropriately to assistive technologies.

Buttons - 图12

  1. <button type="button" class="btn btn-primary" data-bs-toggle="button" autocomplete="off">Toggle button</button>
  2. <button type="button" class="btn btn-primary active" data-bs-toggle="button" autocomplete="off" aria-pressed="true">Active toggle button</button>
  3. <button type="button" class="btn btn-primary" disabled data-bs-toggle="button" autocomplete="off">Disabled toggle button</button>

Buttons - 图13

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

Methods

You can create a button instance with the button constructor, for example:

  1. var button = document.getElementById('myButton')
  2. var bsButton = new bootstrap.Button(button)
MethodDescription
toggleToggles push state. Gives the button the appearance that it has been activated.
disposeDestroys an element’s button. (Removes stored data on the DOM element)

For example, to toggle all buttons

  1. var buttons = document.querySelectorAll('.btn')
  2. buttons.forEach(function (button) {
  3. var button = new bootstrap.Button(button)
  4. button.toggle()
  5. })

Sass

Variables

  1. $btn-padding-y: $input-btn-padding-y;
  2. $btn-padding-x: $input-btn-padding-x;
  3. $btn-font-family: $input-btn-font-family;
  4. $btn-font-size: $input-btn-font-size;
  5. $btn-line-height: $input-btn-line-height;
  6. $btn-white-space: null; // Set to `nowrap` to prevent text wrapping
  7. $btn-padding-y-sm: $input-btn-padding-y-sm;
  8. $btn-padding-x-sm: $input-btn-padding-x-sm;
  9. $btn-font-size-sm: $input-btn-font-size-sm;
  10. $btn-padding-y-lg: $input-btn-padding-y-lg;
  11. $btn-padding-x-lg: $input-btn-padding-x-lg;
  12. $btn-font-size-lg: $input-btn-font-size-lg;
  13. $btn-border-width: $input-btn-border-width;
  14. $btn-font-weight: $font-weight-normal;
  15. $btn-box-shadow: inset 0 1px 0 rgba($white, .15), 0 1px 1px rgba($black, .075);
  16. $btn-focus-width: $input-btn-focus-width;
  17. $btn-focus-box-shadow: $input-btn-focus-box-shadow;
  18. $btn-disabled-opacity: .65;
  19. $btn-active-box-shadow: inset 0 3px 5px rgba($black, .125);
  20. $btn-link-color: $link-color;
  21. $btn-link-hover-color: $link-hover-color;
  22. $btn-link-disabled-color: $gray-600;
  23. // Allows for customizing button radius independently from global border radius
  24. $btn-border-radius: $border-radius;
  25. $btn-border-radius-sm: $border-radius-sm;
  26. $btn-border-radius-lg: $border-radius-lg;
  27. $btn-transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
  28. $btn-hover-bg-shade-amount: 15%;
  29. $btn-hover-bg-tint-amount: 15%;
  30. $btn-hover-border-shade-amount: 20%;
  31. $btn-hover-border-tint-amount: 10%;
  32. $btn-active-bg-shade-amount: 20%;
  33. $btn-active-bg-tint-amount: 20%;
  34. $btn-active-border-shade-amount: 25%;
  35. $btn-active-border-tint-amount: 10%;

Mixins

There are three mixins for buttons: button and button outline variant mixins (both based on $theme-colors), plus a button size mixin.

  1. @mixin button-variant(
  2. $background,
  3. $border,
  4. $color: color-contrast($background),
  5. $hover-background: if($color == $color-contrast-light, shade-color($background, $btn-hover-bg-shade-amount), tint-color($background, $btn-hover-bg-tint-amount)),
  6. $hover-border: if($color == $color-contrast-light, shade-color($border, $btn-hover-border-shade-amount), tint-color($border, $btn-hover-border-tint-amount)),
  7. $hover-color: color-contrast($hover-background),
  8. $active-background: if($color == $color-contrast-light, shade-color($background,$btn-active-bg-shade-amount), tint-color($background, $btn-active-bg-tint-amount)),
  9. $active-border: if($color == $color-contrast-light, shade-color($border, $btn-active-border-shade-amount), tint-color($border, $btn-active-border-tint-amount)),
  10. $active-color: color-contrast($active-background),
  11. $disabled-background: $background,
  12. $disabled-border: $border,
  13. $disabled-color: color-contrast($disabled-background)
  14. ) {
  15. color: $color;
  16. @include gradient-bg($background);
  17. border-color: $border;
  18. @include box-shadow($btn-box-shadow);
  19. &:hover {
  20. color: $hover-color;
  21. @include gradient-bg($hover-background);
  22. border-color: $hover-border;
  23. }
  24. .btn-check:focus + &,
  25. &:focus {
  26. color: $hover-color;
  27. @include gradient-bg($hover-background);
  28. border-color: $hover-border;
  29. @if $enable-shadows {
  30. @include box-shadow($btn-box-shadow, 0 0 0 $btn-focus-width rgba(mix($color, $border, 15%), .5));
  31. } @else {
  32. // Avoid using mixin so we can pass custom focus shadow properly
  33. box-shadow: 0 0 0 $btn-focus-width rgba(mix($color, $border, 15%), .5);
  34. }
  35. }
  36. .btn-check:checked + &,
  37. .btn-check:active + &,
  38. &:active,
  39. &.active,
  40. .show > &.dropdown-toggle {
  41. color: $active-color;
  42. background-color: $active-background;
  43. // Remove CSS gradients if they're enabled
  44. background-image: if($enable-gradients, none, null);
  45. border-color: $active-border;
  46. &:focus {
  47. @if $enable-shadows {
  48. @include box-shadow($btn-active-box-shadow, 0 0 0 $btn-focus-width rgba(mix($color, $border, 15%), .5));
  49. } @else {
  50. // Avoid using mixin so we can pass custom focus shadow properly
  51. box-shadow: 0 0 0 $btn-focus-width rgba(mix($color, $border, 15%), .5);
  52. }
  53. }
  54. }
  55. &:disabled,
  56. &.disabled {
  57. color: $disabled-color;
  58. background-color: $disabled-background;
  59. // Remove CSS gradients if they're enabled
  60. background-image: if($enable-gradients, none, null);
  61. border-color: $disabled-border;
  62. }
  63. }
  1. @mixin button-outline-variant(
  2. $color,
  3. $color-hover: color-contrast($color),
  4. $active-background: $color,
  5. $active-border: $color,
  6. $active-color: color-contrast($active-background)
  7. ) {
  8. color: $color;
  9. border-color: $color;
  10. &:hover {
  11. color: $color-hover;
  12. background-color: $active-background;
  13. border-color: $active-border;
  14. }
  15. .btn-check:focus + &,
  16. &:focus {
  17. box-shadow: 0 0 0 $btn-focus-width rgba($color, .5);
  18. }
  19. .btn-check:checked + &,
  20. .btn-check:active + &,
  21. &:active,
  22. &.active,
  23. &.dropdown-toggle.show {
  24. color: $active-color;
  25. background-color: $active-background;
  26. border-color: $active-border;
  27. &:focus {
  28. @if $enable-shadows {
  29. @include box-shadow($btn-active-box-shadow, 0 0 0 $btn-focus-width rgba($color, .5));
  30. } @else {
  31. // Avoid using mixin so we can pass custom focus shadow properly
  32. box-shadow: 0 0 0 $btn-focus-width rgba($color, .5);
  33. }
  34. }
  35. }
  36. &:disabled,
  37. &.disabled {
  38. color: $color;
  39. background-color: transparent;
  40. }
  41. }
  1. @mixin button-size($padding-y, $padding-x, $font-size, $border-radius) {
  2. padding: $padding-y $padding-x;
  3. @include font-size($font-size);
  4. // Manually declare to provide an override to the browser default
  5. @include border-radius($border-radius, 0);
  6. }

Loops

Button variants (for regular and outline buttons) use their respective mixins with our $theme-colors map to generate the modifier classes in scss/_buttons.scss.

  1. @each $color, $value in $theme-colors {
  2. .btn-#{$color} {
  3. @include button-variant($value, $value);
  4. }
  5. }
  6. @each $color, $value in $theme-colors {
  7. .btn-outline-#{$color} {
  8. @include button-outline-variant($value);
  9. }
  10. }