buttons.md 5.7 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
2
3
4
---
layout: page
title: Buttons
---
Mark Otto's avatar
Mark Otto committed
5
6
7
8

Use any of the available button classes to quickly create a styled button.

{% example html %}
Mark Otto's avatar
Mark Otto committed
9
10
11
<!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
<button type="button" class="btn btn-primary">Primary</button>

12
13
14
<!-- Secondary, outline button -->
<button type="button" class="btn btn-secondary">Secondary</button>

Mark Otto's avatar
Mark Otto committed
15
16
17
18
19
20
21
22
23
24
25
<!-- Indicates a successful or positive action -->
<button type="button" class="btn btn-success">Success</button>

<!-- Indicates caution should be taken with this action -->
<button type="button" class="btn btn-warning">Warning</button>

<!-- Indicates a dangerous or potentially negative action -->
<button type="button" class="btn btn-danger">Danger</button>

<!-- Deemphasize a button by making it look like a link while maintaining button behavior -->
<button type="button" class="btn btn-link">Link</button>
Mark Otto's avatar
Mark Otto committed
26
27
{% endexample %}

Mark Otto's avatar
Mark Otto committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<div class="bs-callout bs-callout-warning">
  <h4>Conveying meaning to assistive technologies</h4>
  <p>Using color to add meaning to a button 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 (the visible text of the button), or is included through alternative means, such as additional text hidden with the <code>.sr-only</code> class.</p>
</div>

## Button tags

Use the button classes on an `<a>`, `<button>`, or `<input>` element.

{% example html %}
<a class="btn btn-secondary" href="#" role="button">Link</a>
<button class="btn btn-secondary" type="submit">Button</button>
<input class="btn btn-secondary" type="button" value="Input">
<input class="btn btn-secondary" type="submit" value="Submit">
{% endexample %}

<div class="bs-callout bs-callout-warning">
  <h4>Links acting as buttons</h4>
  <p>If the <code>&lt;a&gt;</code> elements are used to act as buttons – triggering in-page functionality, rather than navigating to another document or section within the current page – they should also be given an appropriate <code>role="button"</code>.</p>
</div>

<div class="bs-callout bs-callout-warning">
  <h4>Cross-browser rendering</h4>
  <p>As a best practice, <strong>we highly recommend using the <code>&lt;button&gt;</code> element whenever possible</strong> to ensure matching cross-browser rendering.</p>
</div>

Mark Otto's avatar
Mark Otto committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
## Sizes

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

{% example html %}
<button type="button" class="btn btn-primary btn-lg">Large button</button>
<button type="button" class="btn btn-secondary btn-lg">Large button</button>
{% endexample %}

{% example html %}
<button type="button" class="btn btn-primary btn-sm">Small button</button>
<button type="button" class="btn btn-secondary btn-sm">Small button</button>
{% endexample %}

{% example html %}
<button type="button" class="btn btn-primary btn-xs">Extra small button</button>
<button type="button" class="btn btn-secondary btn-xs">Extra small button</button>
{% endexample %}

Create block level buttons—those that span the full width of a parent—by adding `.btn-block`.

{% example html %}
Mark Otto's avatar
Mark Otto committed
76
<button type="button" class="btn btn-primary btn-lg btn-block">Block level button</button>
Mark Otto's avatar
Mark Otto committed
77
<button type="button" class="btn btn-secondary btn-lg btn-block">Block level button</button>
Mark Otto's avatar
Mark Otto committed
78
79
80
81
{% endexample %}

## Active state

Mark Otto's avatar
Mark Otto committed
82
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.
Mark Otto's avatar
Mark Otto committed
83
84

{% example html %}
Mark Otto's avatar
Mark Otto committed
85
<a href="#" class="btn btn-primary btn-lg active" role="button">Primary link</a>
Mark Otto's avatar
Mark Otto committed
86
<a href="#" class="btn btn-secondary btn-lg active" role="button">Link</a>
Mark Otto's avatar
Mark Otto committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{% endexample %}

## Disabled state

Make buttons look unclickable by adding the `disabled` boolean attribute to any `<button>` element.

{% example html %}
<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>
{% endexample %}

As `<a>` elements don't support the `disabled` attribute, you must add the `.disabled` class to fake it.

{% example html %}
Mark Otto's avatar
Mark Otto committed
101
<a href="#" class="btn btn-primary btn-lg disabled" role="button">Primary link</a>
Mark Otto's avatar
Mark Otto committed
102
<a href="#" class="btn btn-secondary btn-lg disabled" role="button">Link</a>
Mark Otto's avatar
Mark Otto committed
103
104
105
106
107
108
109
110
111
{% endexample %}

<div class="bs-callout bs-callout-warning">
  <h4>Cross-browser compatibility</h4>
  <p>If you add the <code>disabled</code> attribute to a <code>&lt;button&gt;</code>, Internet Explorer 9 and below will render text gray with a nasty text-shadow that we cannot fix.</p>
</div>

<div class="bs-callout bs-callout-warning">
  <h4>Link functionality caveat</h4>
Mark Otto's avatar
Mark Otto committed
112
  <p>This class uses <code>pointer-events: none</code> to try to disable the link functionality of <code>&lt;a&gt;</code>s, but that CSS property is not yet standardized and isn't fully supported in Opera 18 and below, or in Internet Explorer 11. In addition, even in browsers that do support <code>pointer-events: none</code>, 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, use custom JavaScript to disable such links.</p>
Mark Otto's avatar
Mark Otto committed
113
114
115
116
117
118
</div>

<div class="bs-callout bs-callout-warning">
  <h4>Context-specific usage</h4>
  <p>While button classes can be used on <code>&lt;a&gt;</code> and <code>&lt;button&gt;</code> elements, only <code>&lt;button&gt;</code> elements are supported within our nav and navbar components.</p>
</div>