Buttons

Use Bootstrap’s custom button styles for actions in forms, dialogs, and more with support for multiple sizes, states, and more.

Base class

Bootstrap has a base .btn class that sets up basic styles such as padding and content alignment. By default, .btn controls have a transparent border and background color, and lack any explicit focus and hover styles.

Buttons - 图1

html

  1. <button type="button" class="btn">Base class</button>

The .btn class is intended to be used in conjunction with our button variants, or to serve as a basis for your own custom styles.

If you are using the .btn class on its own, remember to at least define some explicit :focus and/or :focus-visible styles.

Variants

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

Buttons - 图2

html

  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>

Accessibility tip: Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies like screen readers. Please ensure the meaning is obvious from the content itself (e.g., the visible text with a sufficient color contrast) 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 - 图3

html

  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 - 图4

html

  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 - 图5

html

  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 - 图6

html

  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>

You can even roll your own custom sizing with CSS variables:

Buttons - 图7

html

  1. <button type="button" class="btn btn-primary"
  2. style="--bs-btn-padding-y: .25rem; --bs-btn-padding-x: .5rem; --bs-btn-font-size: .75rem;">
  3. Custom button
  4. </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 - 图8

html

  1. <button type="button" class="btn btn-primary" disabled>Primary button</button>
  2. <button type="button" class="btn btn-secondary" disabled>Button</button>
  3. <button type="button" class="btn btn-outline-primary" disabled>Primary button</button>
  4. <button type="button" class="btn btn-outline-secondary" 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 using <a> should include the aria-disabled="true" attribute to indicate the state of the element to assistive technologies.
  • Disabled buttons using <a> should not include the href attribute.

Buttons - 图9

html

  1. <a class="btn btn-primary disabled" role="button" aria-disabled="true">Primary link</a>
  2. <a class="btn btn-secondary disabled" role="button" aria-disabled="true">Link</a>

To cover cases where you have to keep the href attribute on a disabled link, the .disabled class uses pointer-events: none to try to disable the link functionality of <a>s. Note that this CSS property is not yet standardized for HTML, but all modern browsers support it. 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.

Buttons - 图10

html

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

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 - 图11

html

  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 - 图12

html

  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 - 图13

html

  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 - 图14

html

  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 - 图15

html

  1. <p class="d-inline-flex gap-1">
  2. <button type="button" class="btn" data-bs-toggle="button">Toggle button</button>
  3. <button type="button" class="btn active" data-bs-toggle="button" aria-pressed="true">Active toggle button</button>
  4. <button type="button" class="btn" disabled data-bs-toggle="button">Disabled toggle button</button>
  5. </p>
  6. <p class="d-inline-flex gap-1">
  7. <button type="button" class="btn btn-primary" data-bs-toggle="button">Toggle button</button>
  8. <button type="button" class="btn btn-primary active" data-bs-toggle="button" aria-pressed="true">Active toggle button</button>
  9. <button type="button" class="btn btn-primary" disabled data-bs-toggle="button">Disabled toggle button</button>
  10. </p>

Buttons - 图16

html

  1. <p class="d-inline-flex gap-1">
  2. <a href="#" class="btn" role="button" data-bs-toggle="button">Toggle link</a>
  3. <a href="#" class="btn active" role="button" data-bs-toggle="button" aria-pressed="true">Active toggle link</a>
  4. <a class="btn disabled" aria-disabled="true" role="button" data-bs-toggle="button">Disabled toggle link</a>
  5. </p>
  6. <p class="d-inline-flex gap-1">
  7. <a href="#" class="btn btn-primary" role="button" data-bs-toggle="button">Toggle link</a>
  8. <a href="#" class="btn btn-primary active" role="button" data-bs-toggle="button" aria-pressed="true">Active toggle link</a>
  9. <a class="btn btn-primary disabled" aria-disabled="true" role="button" data-bs-toggle="button">Disabled toggle link</a>
  10. </p>

Methods

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

  1. const bsButton = new bootstrap.Button('#myButton')
MethodDescription
disposeDestroys an element’s button. (Removes stored data on the DOM element)
getInstanceStatic method which allows you to get the button instance associated with a DOM element, you can use it like this: bootstrap.Button.getInstance(element).
getOrCreateInstanceStatic method which returns a button instance associated with a DOM element or creates a new one in case it wasn’t initialized. You can use it like this: bootstrap.Button.getOrCreateInstance(element).
toggleToggles push state. Gives the button the appearance that it has been activated.

For example, to toggle all buttons

  1. document.querySelectorAll('.btn').forEach(buttonElement => {
  2. const button = bootstrap.Button.getOrCreateInstance(buttonElement)
  3. button.toggle()
  4. })

CSS

Variables

Added in v5.2.0

As part of Bootstrap’s evolving CSS variables approach, buttons now use local CSS variables on .btn for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

scss/_buttons.scss

  1. --#{$prefix}btn-padding-x: #{$btn-padding-x};
  2. --#{$prefix}btn-padding-y: #{$btn-padding-y};
  3. --#{$prefix}btn-font-family: #{$btn-font-family};
  4. @include rfs($btn-font-size, --#{$prefix}btn-font-size);
  5. --#{$prefix}btn-font-weight: #{$btn-font-weight};
  6. --#{$prefix}btn-line-height: #{$btn-line-height};
  7. --#{$prefix}btn-color: #{$btn-color};
  8. --#{$prefix}btn-bg: transparent;
  9. --#{$prefix}btn-border-width: #{$btn-border-width};
  10. --#{$prefix}btn-border-color: transparent;
  11. --#{$prefix}btn-border-radius: #{$btn-border-radius};
  12. --#{$prefix}btn-hover-border-color: transparent;
  13. --#{$prefix}btn-box-shadow: #{$btn-box-shadow};
  14. --#{$prefix}btn-disabled-opacity: #{$btn-disabled-opacity};
  15. --#{$prefix}btn-focus-box-shadow: 0 0 0 #{$btn-focus-width} rgba(var(--#{$prefix}btn-focus-shadow-rgb), .5);

Each .btn-* modifier class updates the appropriate CSS variables to minimize additional CSS rules with our button-variant(), button-outline-variant(), and button-size() mixins.

Here’s an example of building a custom .btn-* modifier class as we do for the buttons unique to our docs by reassigning Bootstrap’s CSS variables with a mixture of our own CSS and Sass variables.

Buttons - 图17

site/assets/scss/_buttons.scss

  1. .btn-bd-primary {
  2. --bs-btn-font-weight: 600;
  3. --bs-btn-color: var(--bs-white);
  4. --bs-btn-bg: var(--bd-violet-bg);
  5. --bs-btn-border-color: var(--bd-violet-bg);
  6. --bs-btn-hover-color: var(--bs-white);
  7. --bs-btn-hover-bg: #{shade-color($bd-violet, 10%)};
  8. --bs-btn-hover-border-color: #{shade-color($bd-violet, 10%)};
  9. --bs-btn-focus-shadow-rgb: var(--bd-violet-rgb);
  10. --bs-btn-active-color: var(--bs-btn-hover-color);
  11. --bs-btn-active-bg: #{shade-color($bd-violet, 20%)};
  12. --bs-btn-active-border-color: #{shade-color($bd-violet, 20%)};
  13. }

Sass variables

scss/_variables.scss

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

Sass mixins

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

scss/mixins/_buttons.scss

  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. --#{$prefix}btn-color: #{$color};
  16. --#{$prefix}btn-bg: #{$background};
  17. --#{$prefix}btn-border-color: #{$border};
  18. --#{$prefix}btn-hover-color: #{$hover-color};
  19. --#{$prefix}btn-hover-bg: #{$hover-background};
  20. --#{$prefix}btn-hover-border-color: #{$hover-border};
  21. --#{$prefix}btn-focus-shadow-rgb: #{to-rgb(mix($color, $border, 15%))};
  22. --#{$prefix}btn-active-color: #{$active-color};
  23. --#{$prefix}btn-active-bg: #{$active-background};
  24. --#{$prefix}btn-active-border-color: #{$active-border};
  25. --#{$prefix}btn-active-shadow: #{$btn-active-box-shadow};
  26. --#{$prefix}btn-disabled-color: #{$disabled-color};
  27. --#{$prefix}btn-disabled-bg: #{$disabled-background};
  28. --#{$prefix}btn-disabled-border-color: #{$disabled-border};
  29. }

scss/mixins/_buttons.scss

  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. --#{$prefix}btn-color: #{$color};
  9. --#{$prefix}btn-border-color: #{$color};
  10. --#{$prefix}btn-hover-color: #{$color-hover};
  11. --#{$prefix}btn-hover-bg: #{$active-background};
  12. --#{$prefix}btn-hover-border-color: #{$active-border};
  13. --#{$prefix}btn-focus-shadow-rgb: #{to-rgb($color)};
  14. --#{$prefix}btn-active-color: #{$active-color};
  15. --#{$prefix}btn-active-bg: #{$active-background};
  16. --#{$prefix}btn-active-border-color: #{$active-border};
  17. --#{$prefix}btn-active-shadow: #{$btn-active-box-shadow};
  18. --#{$prefix}btn-disabled-color: #{$color};
  19. --#{$prefix}btn-disabled-bg: transparent;
  20. --#{$prefix}btn-disabled-border-color: #{$color};
  21. --#{$prefix}gradient: none;
  22. }

scss/mixins/_buttons.scss

  1. @mixin button-size($padding-y, $padding-x, $font-size, $border-radius) {
  2. --#{$prefix}btn-padding-y: #{$padding-y};
  3. --#{$prefix}btn-padding-x: #{$padding-x};
  4. @include rfs($font-size, --#{$prefix}btn-font-size);
  5. --#{$prefix}btn-border-radius: #{$border-radius};
  6. }

Sass 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.

scss/_buttons.scss

  1. @each $color, $value in $theme-colors {
  2. .btn-#{$color} {
  3. @if $color == "light" {
  4. @include button-variant(
  5. $value,
  6. $value,
  7. $hover-background: shade-color($value, $btn-hover-bg-shade-amount),
  8. $hover-border: shade-color($value, $btn-hover-border-shade-amount),
  9. $active-background: shade-color($value, $btn-active-bg-shade-amount),
  10. $active-border: shade-color($value, $btn-active-border-shade-amount)
  11. );
  12. } @else if $color == "dark" {
  13. @include button-variant(
  14. $value,
  15. $value,
  16. $hover-background: tint-color($value, $btn-hover-bg-tint-amount),
  17. $hover-border: tint-color($value, $btn-hover-border-tint-amount),
  18. $active-background: tint-color($value, $btn-active-bg-tint-amount),
  19. $active-border: tint-color($value, $btn-active-border-tint-amount)
  20. );
  21. } @else {
  22. @include button-variant($value, $value);
  23. }
  24. }
  25. }
  26. @each $color, $value in $theme-colors {
  27. .btn-outline-#{$color} {
  28. @include button-outline-variant($value);
  29. }
  30. }