Bootstrap - Advance


How can we make a toggle button?

Add data-toggle="button" to activate toggling on a single button. Pre-toggled buttons need .active and aria-pressed="true". For pre-toggled buttons, you must add the .active class and the aria-pressed="true" attribute to the button yourself.

<button type="button" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
  Single toggle

How can we enable toggling on a button group?

Add data-toggle="buttons" to a .btn-group containing checkbox or radio inputs to enable toggling in their respective styles. For preselected options, you must add the .active class to the input's label yourself.

Visual checked state only updated on click. If the checked state of a checkbox button is updated without firing a click event on the button (e.g. via <input type="reset"> or via setting the checked property of the input), you will need to toggle the .active class on the input's label yourself.

<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-primary active">
    <input type="checkbox" autocomplete="off" checked> Checkbox 1 (pre-checked)
  <label class="btn btn-primary">
    <input type="checkbox" autocomplete="off"> Checkbox 2
  <label class="btn btn-primary">
    <input type="checkbox" autocomplete="off"> Checkbox 3

What is the purpose of a button group?

Group a series of buttons together on a single line with the button group. Add on optional JavaScript radio and checkbox style behavior with the buttons plugin.

Tooltips & popovers in button groups require special setting: When using tooltips or popovers on elements within a .btn-group, you'll have to specify the option container: 'body' to avoid unwanted side effects (such as the element growing wider and/or losing its rounded corners when the tooltip or popover is triggered).

Ensure correct role and provide a label: In order for assistive technologies – such as screen readers – to convey that a series of buttons is grouped, an appropriate role attribute needs to be provided. For button groups, this would be role="group", while toolbars should have a role="toolbar". One exception are groups which only contain a single control (for instance the justified button groups with <button> elements) or a dropdown. In addition, groups and toolbars should be given an explicit label, as most assistive technologies will otherwise not announce them, despite the presence of the correct role attribute. In the examples provided here, we use aria-label, but alternatives such as aria-labelledby can also be used.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License