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
## 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
## Active state
2014-12-01 05:17:45 +01:00
Buttons will appear pressed (with a darker background, darker border, and inset shadow) when active. **There's no need to add a class to `<button>`s as they use a pseudo-class** . However, you can still force the same active appearance with `.active` (and include the < code > aria-pressed="true"</ code > attribute) should you need to replicate the state programmatically.
2014-07-10 06:17:10 +02:00
2019-01-08 17:33:28 +01:00
{{< example > }}
2015-12-25 22:09:59 +01:00
< a href = "#" class = "btn btn-primary btn-lg active" role = "button" aria-pressed = "true" > Primary link< / a >
< a href = "#" class = "btn btn-secondary btn-lg active" role = "button" aria-pressed = "true" > Link< / a >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2014-07-10 06:17:10 +02:00
## Disabled state
2015-08-11 01:11:44 +02:00
Make buttons look inactive by adding the `disabled` boolean attribute to any `<button>` element.
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
Do more with buttons. Control button states or create groups of buttons for more components like toolbars.
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 > }}
2015-04-17 23:11:08 +02:00
< button type = "button" class = "btn btn-primary" data-toggle = "button" aria-pressed = "false" autocomplete = "off" >
Single toggle
< / button >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2015-04-17 23:11:08 +02:00
2015-08-10 09:05:40 +02:00
### Checkbox and radio buttons
2015-04-17 23:11:08 +02:00
2017-12-24 23:26:52 +01:00
Bootstrap's `.button` styles can be applied to other elements, such as `<label>` s, to provide checkbox or radio style button toggling. Add `data-toggle="buttons"` to a `.btn-group` containing those modified buttons to enable their toggling behavior via JavaScript and add `.btn-group-toggle` to style the `<input>` s within your buttons. **Note that you can create single input-powered buttons or groups of them.**
2015-04-17 23:11:08 +02:00
The checked state for these buttons is **only updated via `click` event** on the button. If you use another method to update the input—e.g., with `<input type="reset">` or by manually applying the input's `checked` property—you'll need to toggle `.active` on the `<label>` manually.
Note that pre-checked buttons require you to manually add the `.active` class to the input's `<label>` .
2019-01-08 17:33:28 +01:00
{{< example > }}
2017-12-24 23:25:18 +01:00
< div class = "btn-group-toggle" data-toggle = "buttons" >
2017-02-13 02:15:16 +01:00
< label class = "btn btn-secondary active" >
2017-12-24 23:25:18 +01:00
< input type = "checkbox" checked autocomplete = "off" > Checked
2015-04-17 23:24:11 +02:00
< / label >
2015-04-17 23:11:08 +02:00
< / div >
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 > }}
2017-12-23 01:49:57 +01:00
< div class = "btn-group btn-group-toggle" data-toggle = "buttons" >
2017-02-13 02:15:16 +01:00
< label class = "btn btn-secondary active" >
2017-12-23 01:49:57 +01:00
< input type = "radio" name = "options" id = "option1" autocomplete = "off" checked > Active
2015-04-17 23:11:08 +02:00
< / label >
2017-02-13 02:15:16 +01:00
< label class = "btn btn-secondary" >
2017-12-23 01:49:57 +01:00
< input type = "radio" name = "options" id = "option2" autocomplete = "off" > Radio
2015-04-17 23:11:08 +02:00
< / label >
2017-02-13 02:15:16 +01:00
< label class = "btn btn-secondary" >
2017-12-23 01:49:57 +01:00
< input type = "radio" name = "options" id = "option3" autocomplete = "off" > Radio
2015-04-17 23:11:08 +02:00
< / label >
< / div >
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 > }}