8fbbd55b6af5446f837c2b997b26f8c559662c26.svn-base 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ---
  2. layout: docs
  3. title: Buttons
  4. group: components
  5. redirect_from: "/components/"
  6. ---
  7. Use Bootstrap's custom button styles for actions in forms, dialogs, and more. Includes support for a handful of contextual variations, sizes, states, and more.
  8. ## Contents
  9. * Will be replaced with the ToC, excluding the "Contents" header
  10. {:toc}
  11. ## Examples
  12. Bootstrap includes six predefined button styles, each serving its own semantic purpose.
  13. {% example html %}
  14. <!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
  15. <button type="button" class="btn btn-primary">Primary</button>
  16. <!-- Secondary, outline button -->
  17. <button type="button" class="btn btn-secondary">Secondary</button>
  18. <!-- Indicates a successful or positive action -->
  19. <button type="button" class="btn btn-success">Success</button>
  20. <!-- Indicates caution should be taken with this action -->
  21. <button type="button" class="btn btn-warning">Warning</button>
  22. <!-- Indicates a dangerous or potentially negative action -->
  23. <button type="button" class="btn btn-danger">Danger</button>
  24. <!-- Deemphasize a button by making it look like a link while maintaining button behavior -->
  25. <button type="button" class="btn btn-link">Link</button>
  26. {% endexample %}
  27. {% callout warning %}
  28. #### Conveying meaning to assistive technologies
  29. 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 `.sr-only` class.
  30. {% endcallout %}
  31. ## Button tags
  32. 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).
  33. 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.
  34. {% example html %}
  35. <a class="btn btn-primary" href="javascript:;" role="button">Link</a>
  36. <button class="btn btn-primary" type="submit">Button</button>
  37. <input class="btn btn-primary" type="button" value="Input">
  38. <input class="btn btn-primary" type="submit" value="Submit">
  39. {% endexample %}
  40. ## Outline buttons
  41. 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.
  42. {% example html %}
  43. <button type="button" class="btn btn-primary-outline">Primary</button>
  44. <button type="button" class="btn btn-secondary-outline">Secondary</button>
  45. <button type="button" class="btn btn-success-outline">Success</button>
  46. <button type="button" class="btn btn-warning-outline">Warning</button>
  47. <button type="button" class="btn btn-danger-outline">Danger</button>
  48. {% endexample %}
  49. ## Sizes
  50. Fancy larger or smaller buttons? Add `.btn-lg`, `.btn-sm`, or `.btn-xs` for additional sizes.
  51. {% example html %}
  52. <button type="button" class="btn btn-primary btn-lg">Large button</button>
  53. <button type="button" class="btn btn-secondary btn-lg">Large button</button>
  54. {% endexample %}
  55. {% example html %}
  56. <button type="button" class="btn btn-primary btn-sm">Small button</button>
  57. <button type="button" class="btn btn-secondary btn-sm">Small button</button>
  58. {% endexample %}
  59. {% example html %}
  60. <button type="button" class="btn btn-primary btn-xs">Extra small button</button>
  61. <button type="button" class="btn btn-secondary btn-xs">Extra small button</button>
  62. {% endexample %}
  63. Create block level buttons—those that span the full width of a parent—by adding `.btn-block`.
  64. {% example html %}
  65. <button type="button" class="btn btn-primary btn-lg btn-block">Block level button</button>
  66. <button type="button" class="btn btn-secondary btn-lg btn-block">Block level button</button>
  67. {% endexample %}
  68. ## Active state
  69. 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.
  70. {% example html %}
  71. <a href="javascript:;" class="btn btn-primary btn-lg active" role="button">Primary link</a>
  72. <a href="javascript:;" class="btn btn-secondary btn-lg active" role="button">Link</a>
  73. {% endexample %}
  74. ## Disabled state
  75. Make buttons look inactive by adding the `disabled` boolean attribute to any `<button>` element.
  76. {% callout info %}
  77. **Heads up!** IE9 and below render disabled buttons with gray, shadowed text that we can't override.
  78. {% endcallout %}
  79. {% example html %}
  80. <button type="button" class="btn btn-lg btn-primary" disabled>Primary button</button>
  81. <button type="button" class="btn btn-secondary btn-lg" disabled>Button</button>
  82. {% endexample %}
  83. As `<a>` elements don't support the `disabled` attribute, you must add the `.disabled` class to fake it.
  84. {% example html %}
  85. <a href="javascript:;" class="btn btn-primary btn-lg disabled" role="button">Primary link</a>
  86. <a href="javascript:;" class="btn btn-secondary btn-lg disabled" role="button">Link</a>
  87. {% endexample %}
  88. {% callout warning %}
  89. #### Link functionality caveat
  90. This class uses `pointer-events: none` to try to disable the link functionality of `<a>`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 `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, use custom JavaScript to disable such links.
  91. {% endcallout %}
  92. ## Button plugin
  93. Do more with buttons. Control button states or create groups of buttons for more components like toolbars.
  94. ### Toggle states
  95. 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>`.
  96. {% example html %}
  97. <button type="button" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
  98. Single toggle
  99. </button>
  100. {% endexample %}
  101. ### Checkbox and radio buttons
  102. 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 toggling in their respective styles.
  103. 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.
  104. Note that pre-checked buttons require you to manually add the `.active` class to the input's `<label>`.
  105. {% example html %}
  106. <div class="btn-group" data-toggle="buttons">
  107. <label class="btn btn-primary active">
  108. <input type="checkbox" checked autocomplete="off"> Checkbox 1 (pre-checked)
  109. </label>
  110. <label class="btn btn-primary">
  111. <input type="checkbox" autocomplete="off"> Checkbox 2
  112. </label>
  113. <label class="btn btn-primary">
  114. <input type="checkbox" autocomplete="off"> Checkbox 3
  115. </label>
  116. </div>
  117. {% endexample %}
  118. {% example html %}
  119. <div class="btn-group" data-toggle="buttons">
  120. <label class="btn btn-primary active">
  121. <input type="radio" name="options" id="option1" autocomplete="off" checked> Radio 1 (preselected)
  122. </label>
  123. <label class="btn btn-primary">
  124. <input type="radio" name="options" id="option2" autocomplete="off"> Radio 2
  125. </label>
  126. <label class="btn btn-primary">
  127. <input type="radio" name="options" id="option3" autocomplete="off"> Radio 3
  128. </label>
  129. </div>
  130. {% endexample %}
  131. ### Methods
  132. | Method | Description |
  133. | --- | --- |
  134. | `$().button('toggle')` |Toggles push state. Gives the button the appearance that it has been activated. |
  135. | `$().button('reset')` | Resets button state—swaps text to original text. **This method is asynchronous and returns before the resetting has actually completed.** |
  136. | `$().button(string)` | Swaps text to any data defined text state. |
  137. ### Custom state example
  138. {% highlight html %}
  139. <button type="button" id="myStateButton" data-complete-text="finished!" class="btn btn-primary" autocomplete="off">
  140. ...
  141. </button>
  142. <script>
  143. $('#myStateButton').on('click', function () {
  144. $(this).button('complete') // button text will be "finished!"
  145. })
  146. </script>
  147. {% endhighlight %}