1 |
efrain |
1 |
---
|
|
|
2 |
layout: docs
|
|
|
3 |
title: Buttons
|
|
|
4 |
description: Use Bootstrap's custom button styles for actions in forms, dialogs, and more with support for multiple sizes, states, and more.
|
|
|
5 |
group: components
|
|
|
6 |
toc: true
|
|
|
7 |
---
|
|
|
8 |
|
|
|
9 |
## Examples
|
|
|
10 |
|
|
|
11 |
Bootstrap includes several predefined button styles, each serving its own semantic purpose, with a few extras thrown in for more control.
|
|
|
12 |
|
|
|
13 |
{{< example >}}
|
|
|
14 |
{{< buttons.inline >}}
|
|
|
15 |
{{- range (index $.Site.Data "theme-colors") }}
|
|
|
16 |
<button type="button" class="btn btn-{{ .name }}">{{ .name | title }}</button>
|
|
|
17 |
{{- end -}}
|
|
|
18 |
{{< /buttons.inline >}}
|
|
|
19 |
|
|
|
20 |
<button type="button" class="btn btn-link">Link</button>
|
|
|
21 |
{{< /example >}}
|
|
|
22 |
|
|
|
23 |
{{< callout warning >}}
|
|
|
24 |
{{< partial "callout-warning-color-assistive-technologies.md" >}}
|
|
|
25 |
{{< /callout >}}
|
|
|
26 |
|
|
|
27 |
## Disable text wrapping
|
|
|
28 |
|
|
|
29 |
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.
|
|
|
30 |
|
|
|
31 |
## Button tags
|
|
|
32 |
|
|
|
33 |
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).
|
|
|
34 |
|
|
|
35 |
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.
|
|
|
36 |
|
|
|
37 |
{{< example >}}
|
|
|
38 |
<a class="btn btn-primary" href="#" role="button">Link</a>
|
|
|
39 |
<button class="btn btn-primary" type="submit">Button</button>
|
|
|
40 |
<input class="btn btn-primary" type="button" value="Input">
|
|
|
41 |
<input class="btn btn-primary" type="submit" value="Submit">
|
|
|
42 |
<input class="btn btn-primary" type="reset" value="Reset">
|
|
|
43 |
{{< /example >}}
|
|
|
44 |
|
|
|
45 |
## Outline buttons
|
|
|
46 |
|
|
|
47 |
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.
|
|
|
48 |
|
|
|
49 |
{{< example >}}
|
|
|
50 |
{{< buttons.inline >}}
|
|
|
51 |
{{- range (index $.Site.Data "theme-colors") }}
|
|
|
52 |
<button type="button" class="btn btn-outline-{{ .name }}">{{ .name | title }}</button>
|
|
|
53 |
{{- end -}}
|
|
|
54 |
{{< /buttons.inline >}}
|
|
|
55 |
{{< /example >}}
|
|
|
56 |
|
|
|
57 |
{{< callout info >}}
|
|
|
58 |
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.
|
|
|
59 |
{{< /callout >}}
|
|
|
60 |
|
|
|
61 |
## Sizes
|
|
|
62 |
|
|
|
63 |
Fancy larger or smaller buttons? Add `.btn-lg` or `.btn-sm` for additional sizes.
|
|
|
64 |
|
|
|
65 |
{{< example >}}
|
|
|
66 |
<button type="button" class="btn btn-primary btn-lg">Large button</button>
|
|
|
67 |
<button type="button" class="btn btn-secondary btn-lg">Large button</button>
|
|
|
68 |
{{< /example >}}
|
|
|
69 |
|
|
|
70 |
{{< example >}}
|
|
|
71 |
<button type="button" class="btn btn-primary btn-sm">Small button</button>
|
|
|
72 |
<button type="button" class="btn btn-secondary btn-sm">Small button</button>
|
|
|
73 |
{{< /example >}}
|
|
|
74 |
|
|
|
75 |
Create block level buttons—those that span the full width of a parent—by adding `.btn-block`.
|
|
|
76 |
|
|
|
77 |
{{< example >}}
|
|
|
78 |
<button type="button" class="btn btn-primary btn-lg btn-block">Block level button</button>
|
|
|
79 |
<button type="button" class="btn btn-secondary btn-lg btn-block">Block level button</button>
|
|
|
80 |
{{< /example >}}
|
|
|
81 |
|
|
|
82 |
## Active state
|
|
|
83 |
|
|
|
84 |
Buttons will appear pressed when active with a darker background, darker border, and, when shadows are enabled, an inset shadow. **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.
|
|
|
85 |
|
|
|
86 |
{{< example >}}
|
|
|
87 |
<a href="#" class="btn btn-primary btn-lg active" role="button" aria-pressed="true">Primary link</a>
|
|
|
88 |
<a href="#" class="btn btn-secondary btn-lg active" role="button" aria-pressed="true">Link</a>
|
|
|
89 |
{{< /example >}}
|
|
|
90 |
|
|
|
91 |
## Disabled state
|
|
|
92 |
|
|
|
93 |
Make buttons look inactive by adding the `disabled` boolean attribute to any `<button>` element.
|
|
|
94 |
|
|
|
95 |
{{< example >}}
|
|
|
96 |
<button type="button" class="btn btn-lg btn-primary" disabled>Primary button</button>
|
|
|
97 |
<button type="button" class="btn btn-secondary btn-lg" disabled>Button</button>
|
|
|
98 |
{{< /example >}}
|
|
|
99 |
|
|
|
100 |
Disabled buttons using the `<a>` element behave a bit different:
|
|
|
101 |
|
|
|
102 |
- `<a>`s don't support the `disabled` attribute, so you must add the `.disabled` class to make it visually appear disabled.
|
|
|
103 |
- 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.
|
|
|
104 |
- Disabled buttons using `<a>` should include the `aria-disabled="true"` attribute to indicate the state of the element to assistive technologies.
|
|
|
105 |
- Disabled buttons using `<a>` *should not* include the `href` attribute.
|
|
|
106 |
|
|
|
107 |
{{< example >}}
|
|
|
108 |
<a class="btn btn-primary btn-lg disabled" role="button" aria-disabled="true">Primary link</a>
|
|
|
109 |
<a class="btn btn-secondary btn-lg disabled" role="button" aria-disabled="true">Link</a>
|
|
|
110 |
{{< /example >}}
|
|
|
111 |
|
|
|
112 |
### Link functionality caveat
|
|
|
113 |
|
|
|
114 |
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.
|
|
|
115 |
|
|
|
116 |
{{< example >}}
|
|
|
117 |
<a href="#" class="btn btn-primary btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">Primary link</a>
|
|
|
118 |
<a href="#" class="btn btn-secondary btn-lg disabled" tabindex="-1" role="button" aria-disabled="true">Link</a>
|
|
|
119 |
{{< /example >}}
|
|
|
120 |
|
|
|
121 |
## Button plugin
|
|
|
122 |
|
|
|
123 |
Do more with buttons. Control button states or create groups of buttons for more components like toolbars.
|
|
|
124 |
|
|
|
125 |
### Toggle states
|
|
|
126 |
|
|
|
127 |
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>`.
|
|
|
128 |
|
|
|
129 |
{{< example >}}
|
|
|
130 |
<button type="button" class="btn btn-primary" data-toggle="button" aria-pressed="false">
|
|
|
131 |
Single toggle
|
|
|
132 |
</button>
|
|
|
133 |
{{< /example >}}
|
|
|
134 |
|
|
|
135 |
### Checkbox and radio buttons
|
|
|
136 |
|
|
|
137 |
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.**
|
|
|
138 |
|
|
|
139 |
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.
|
|
|
140 |
|
|
|
141 |
Note that pre-checked buttons require you to manually add the `.active` class to the input's `<label>`.
|
|
|
142 |
|
|
|
143 |
{{< example >}}
|
|
|
144 |
<div class="btn-group-toggle" data-toggle="buttons">
|
|
|
145 |
<label class="btn btn-secondary active">
|
|
|
146 |
<input type="checkbox" checked> Checked
|
|
|
147 |
</label>
|
|
|
148 |
</div>
|
|
|
149 |
{{< /example >}}
|
|
|
150 |
|
|
|
151 |
{{< example >}}
|
|
|
152 |
<div class="btn-group btn-group-toggle" data-toggle="buttons">
|
|
|
153 |
<label class="btn btn-secondary active">
|
|
|
154 |
<input type="radio" name="options" id="option1" checked> Active
|
|
|
155 |
</label>
|
|
|
156 |
<label class="btn btn-secondary">
|
|
|
157 |
<input type="radio" name="options" id="option2"> Radio
|
|
|
158 |
</label>
|
|
|
159 |
<label class="btn btn-secondary">
|
|
|
160 |
<input type="radio" name="options" id="option3"> Radio
|
|
|
161 |
</label>
|
|
|
162 |
</div>
|
|
|
163 |
{{< /example >}}
|
|
|
164 |
|
|
|
165 |
### Methods
|
|
|
166 |
|
|
|
167 |
| Method | Description |
|
|
|
168 |
| --- | --- |
|
|
|
169 |
| `$().button('toggle')` | Toggles push state. Gives the button the appearance that it has been activated. |
|
|
|
170 |
| `$().button('dispose')` | Destroys an element's button. |
|