2014-07-12 07:34:47 +02:00
---
2015-08-15 07:45:55 +02:00
layout: docs
2014-07-12 07:34:47 +02:00
title: Buttons
2016-10-03 03:19:47 +02:00
description: Use Bootstrap's custom button styles for actions in forms, dialogs, and more with support for multiple sizes, states, and more.
2015-08-06 02:47:45 +02:00
group: components
2017-05-28 08:01:14 +02:00
toc: true
2014-07-12 07:34:47 +02:00
---
2014-07-10 06:17:10 +02:00
2015-01-20 05:03:08 +01:00
## Examples
2017-06-26 03:31:34 +02:00
Bootstrap includes several predefined button styles, each serving its own semantic purpose, with a few extras thrown in for more control.
2014-07-10 06:17:10 +02:00
2019-01-08 17:33:28 +01:00
{{< example > }}
{{< buttons.inline > }}
{{- range (index $.Site.Data "theme-colors") }}
< button type = "button" class = "btn btn-{{ .name }}" > {{ .name | title }}< / button >
{{- end -}}
{{< / buttons.inline > }}
2014-03-17 03:03:53 +01:00
< button type = "button" class = "btn btn-link" > Link< / button >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2014-07-10 06:17:10 +02:00
2019-01-08 17:33:28 +01:00
{{< callout info > }}
{{< partial " callout-warning-color-assistive-technologies . md " > }}
{{< / callout > }}
2014-12-01 05:17:45 +01:00
2019-10-25 11:37:52 +02:00
## 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.
2014-12-01 05:17:45 +01:00
## Button tags
2015-04-30 23:39:57 +02:00
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).
2015-04-29 20:44:19 +02:00
2015-04-30 23:39:57 +02:00
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.
2014-12-01 05:17:45 +01:00
2019-01-08 17:33:28 +01:00
{{< example > }}
2015-08-06 08:14:53 +02:00
< 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" >
2016-01-07 01:24:43 +01:00
< input class = "btn btn-primary" type = "reset" value = "Reset" >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2014-12-01 05:17:45 +01:00
2015-08-10 07:48:48 +02:00
## Outline buttons
2016-02-16 06:20:44 +01:00
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.
2015-08-10 07:48:48 +02:00
2019-01-08 17:33:28 +01:00
{{< example > }}
{{< buttons.inline > }}
{{- range (index $.Site.Data "theme-colors") }}
< button type = "button" class = "btn btn-outline-{{ .name }}" > {{ .name | title }}< / button >
{{- end -}}
{{< / buttons.inline > }}
{{< / example > }}
2015-08-10 07:48:48 +02:00
2014-07-10 06:17:10 +02:00
## Sizes
2015-08-19 19:38:22 +02:00
Fancy larger or smaller buttons? Add `.btn-lg` or `.btn-sm` for additional sizes.
2014-07-10 06:17:10 +02:00
2019-01-08 17:33:28 +01:00
{{< example > }}
2014-07-10 06:17:10 +02:00
< button type = "button" class = "btn btn-primary btn-lg" > Large button< / button >
< button type = "button" class = "btn btn-secondary btn-lg" > Large button< / button >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2014-07-10 06:17:10 +02:00
2019-01-08 17:33:28 +01:00
{{< example > }}
2014-07-10 06:17:10 +02:00
< button type = "button" class = "btn btn-primary btn-sm" > Small button< / button >
< button type = "button" class = "btn btn-secondary btn-sm" > Small button< / button >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2014-07-10 06:17:10 +02:00
Create block level buttons—those that span the full width of a parent—by adding `.btn-block` .
2019-01-08 17:33:28 +01:00
{{< example > }}
2014-03-17 03:03:53 +01:00
< button type = "button" class = "btn btn-primary btn-lg btn-block" > Block level button< / button >
2014-07-09 02:14:14 +02:00
< button type = "button" class = "btn btn-secondary btn-lg btn-block" > Block level button< / button >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2014-07-10 06:17:10 +02:00
## Disabled state
2019-12-27 17:40:06 +01:00
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.
2015-08-11 01:11:44 +02:00
2019-01-08 17:33:28 +01:00
{{< example > }}
2014-07-10 06:17:10 +02:00
< 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 >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2014-07-10 06:17:10 +02:00
2016-02-07 03:34:52 +01:00
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.
2016-02-08 12:05:03 +01:00
- Some future-friendly styles are included to disable all `pointer-events` on anchor buttons. In browsers which support that property, you won't see the disabled cursor at all.
2016-02-07 03:34:52 +01:00
- Disabled buttons should include the `aria-disabled="true"` attribute to indicate the state of the element to assistive technologies.
2014-07-10 06:17:10 +02:00
2019-01-08 17:33:28 +01:00
{{< example > }}
2018-02-25 00:15:19 +01:00
< 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 >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2014-07-10 06:17:10 +02:00
2019-01-08 17:33:28 +01:00
{{< callout warning > }}
2017-12-25 05:06:33 +01:00
##### Link functionality caveat
2015-04-17 01:56:40 +02:00
2015-12-27 00:51:27 +01:00
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, add a `tabindex="-1"` attribute on these links (to prevent them from receiving keyboard focus) and use custom JavaScript to disable their functionality.
2019-01-08 17:33:28 +01:00
{{< / callout > }}
2014-07-10 06:17:10 +02:00
2015-08-10 09:05:40 +02:00
## Button plugin
2015-04-17 23:11:08 +02:00
2020-06-04 15:59:24 +02:00
The button plugin allows you to create simple on/off toggle buttons.
2015-04-17 23:11:08 +02:00
2015-08-10 09:05:40 +02:00
### Toggle states
2015-04-17 23:11:08 +02:00
Add `data-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 the `<button>` .
2019-01-08 17:33:28 +01:00
{{< example > }}
2020-05-02 11:11:24 +02:00
< button type = "button" class = "btn btn-primary" data-toggle = "button" autocomplete = "off" > Toggle button< / button >
< button type = "button" class = "btn btn-primary active" data-toggle = "button" autocomplete = "off" aria-pressed = "true" > Active toggle button< / button >
< button type = "button" class = "btn btn-primary" disabled data-toggle = "button" autocomplete = "off" > Disabled toggle button< / button >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2015-04-17 23:11:08 +02:00
2019-01-08 17:33:28 +01:00
{{< example > }}
2020-05-02 11:11:24 +02:00
< a href = "#" class = "btn btn-primary" role = "button" data-toggle = "button" > Toggle link< / a >
< a href = "#" class = "btn btn-primary active" role = "button" data-toggle = "button" aria-pressed = "true" > Active toggle link< / a >
< a href = "#" class = "btn btn-primary disabled" role = "button" data-toggle = "button" > Disabled toggle link< / a >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2015-04-17 23:11:08 +02:00
### Methods
2018-09-15 13:44:23 +02:00
You can create a button instance with the button constructor, for example:
2019-01-08 17:33:28 +01:00
{{< highlight js > }}
2018-09-15 13:44:23 +02:00
var button = document.getElementById('myButton')
var bsButton = new bootstrap.Button(button)
2019-01-08 17:33:28 +01:00
{{< / highlight > }}
2018-09-15 13:44:23 +02:00
2019-07-15 18:59:55 +02:00
< table class = "table" >
v5: Forms update (#28450)
* Initial spike of consolidated form checks
* Stub out forms rearrangement
- Prepping to drop non-custom file and range inputs
- Prepping to merge custom and native checks and radios (with switches)
- Prepping to merge custom select with form select
- Moving docs arround so forms has it's own area given volume of CSS
* Move input group Sass file to forms subdir
* Start to split and move the docs around
* Simpler imports
* Copyediting
* delete overview file
* Remove commented out code
* remove the custom-forms import
* rewrite flex-check as form-check, replace all custom properties
* Remove old forms doc
* stub out new subpage link section
* update migration guide
* Update nav, forms overview in page nav, and descriptions
* fix check bg position
* fix margin-top calculation
* rename .custom-select to .form-select
* Update validation styles for new checks
* add some vertical margin, fix inline checks
* fix docs examples
* better way to do this contents stuff, redo the toc while i'm at it
* page restyle for docs while here
* un-callout that, edit text
* redo padding on toc
* fix toc
* start to cleanup checks docs
* Rewrite Markdown tables into HTML
* Redesign tables, redo their docs
* Replace Open Iconic icons with custom Bootstrap icons
* Redesign the docs navbar, add a subheader, redo the sidebar
* Redesign docs homepage a bit
* Simplify table style overrides for docs tables
* Simplify docs typography for page titles and reading line length
* Stub out icons page
* Part of sidebar update, remove migration from nav.yml
* Move toc CSS to separate partial
* Change appearance of overview page
* fix sidebar arrow direction
* Add footer to docs layout
* Update descriptions
* Drop the .form-group class for margin utilities
* Remove lingering form-group-margin-bottom var
* improve footer spacing
* add headings to range page
* uncomment form range css
* Rename .custom-range to .form-range
* Drop unused docs var
* Uncomment the comment
* Remove unused variable
* Fix radio image sizing
* Reboot update: reset horizontal ul and ol padding
* de-dupe IDs
* tweak toc styles
* nvm, fix dropdown versions stuff
* remove sidebar nav toggle for now
* broken html
* fix more broken html, move css
* scss linting
* comment out broken helper docs
* scope styles
* scope styles
* Fixes #25540 and fixes #26407 for v5 only
* Update sidebar once more
* Match new sidenav order
* fix syntax error
* Rename custom-file to form-file, update paths, update migration docs for previous changes in #28696
* rename back
* fix size and alignment
* rename that back too
2019-07-12 23:52:33 +02:00
< thead >
< tr >
< th > Method< / th >
< th > Description< / th >
< / tr >
< / thead >
< tbody >
< tr >
< td >
< code > toggle< / code >
< / td >
< td >
Toggles push state. Gives the button the appearance that it has been activated.
< / td >
< / tr >
< tr >
< td >
< code > dispose< / code >
< / td >
< td >
Destroys an element's button.
< / td >
< / tr >
< / tbody >
< / table >
2018-09-15 13:44:23 +02:00
For example, to toggle all buttons
2019-01-08 17:33:28 +01:00
{{< highlight js > }}
2018-09-15 13:44:23 +02:00
var buttons = document.querySelectorAll('.btn')
buttons.forEach(function (button) {
var button = new bootstrap.Button(button)
button.toggle()
})
2019-01-08 17:33:28 +01:00
{{< / highlight > }}