Bootstrap - Button Groups

Bootstrap

In this tutorial we will learn about button groups in Bootstrap.

Simple button group

To create a button group we have to wrap buttons .btn inside a .btn-group class.

<div class="btn-group" role="group" aria-label="sample buttons">
  <button type="button" class="btn btn-default">Default button</button>
  <button type="button" class="btn btn-info">Info button</button>
  <button type="button" class="btn btn-success">Success button</button>
</div>

Output

Button toolbar

To create button toolbar we use the .btn-toolbar class.

<div class="btn-toolbar" role="toolbar" aria-label="toolbar">
  <div class="btn-group" role="group" aria-label="button group">
    <div class="btn btn-default">Cut</div>
    <div class="btn btn-default">Copy</div>
    <div class="btn btn-default">Paste</div>
  </div>
  <div class="btn-group" role="group" aria-label="button group">
    <div class="btn btn-default">Print</div>
    <div class="btn btn-default">Scan</div>
    <div class="btn btn-default">Copy</div>
  </div>
</div>

Output

Button sizing

We can change the size of the button groups using the following classes .btn-group-lg for larger buttons, .btn-group-sm for small buttons and .btn-group-xs for extra small buttons.

<!-- large button group size -->
<div class="btn-group btn-group-lg" role="group" aria-label="button group">
  <div class="btn btn-default">Cut</div>
  <div class="btn btn-default">Copy</div>
  <div class="btn btn-default">Paste</div>
</div>

<!-- default button group size -->
<div class="btn-group" role="group" aria-label="button group">
  <div class="btn btn-default">Cut</div>
  <div class="btn btn-default">Copy</div>
  <div class="btn btn-default">Paste</div>
</div>

<!-- small button group size -->
<div class="btn-group btn-group-sm" role="group" aria-label="button group">
  <div class="btn btn-default">Cut</div>
  <div class="btn btn-default">Copy</div>
  <div class="btn btn-default">Paste</div>
</div>

<!-- extra small button group size -->
<div class="btn-group btn-group-xs" role="group" aria-label="button group">
  <div class="btn btn-default">Cut</div>
  <div class="btn btn-default">Copy</div>
  <div class="btn btn-default">Paste</div>
</div>

Output

Button nesting

We can nest button groups by planning .btn-group inside anther .btn-group.

<div class="btn-group" role="group" aria-label="button group nesting">
  <button type="button" class="btn btn-default">Item 1</button>
  <button type="button" class="btn btn-default">Item 2</button>

  <div class="btn-group" role="group">
    <button type="button"
            class="btn btn-default dropdown-toggle"
            data-toggle="dropdown"
            aria-haspopup="true"
            aria-expanded="false">
      Dropdown
      <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">
      <li><a href="#">Menu 1</a></li>
      <li><a href="#">Menu 2</a></li>
    </ul>
  </div>
</div>

Output

Vertical alignment

To create align button group vertically use the .btn-group-vertical class.

<div class="btn-group-vertical" role="group" aria-label="button group vertical">
  <button type="button" class="btn btn-default">Item 1</button>
  <button type="button" class="btn btn-default">Item 2</button>
  <button type="button" class="btn btn-default">Item 3</button>
  <button type="button" class="btn btn-default">Item 4</button>
</div>

Output

Justified button groups

We use the .btn-group-justified class to make the buttons stretch at equal size and span the entire width of its parent.

<div class="btn-group btn-group-justified" role="group" aria-label="button group justified">
  <div class="btn-group" role="group">
    <button type="button" class="btn btn-default">Menu 1</button>
  </div>
  <div class="btn-group" role="group">
    <button type="button" class="btn btn-default">Menu 2</button>
  </div>
  <div class="btn-group" role="group">
    <button type="button" class="btn btn-default">Menu 3</button>
  </div>
</div>

Output

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT