How to get button groups that span the full width of a parent in Bootstrap?
Asked Answered
K

6

54

In Bootstrap, how can I get a button group like the following that span the full width of a parent element? (like with the ".btn-block" class, but applied to a group http://getbootstrap.com/css/#buttons-sizes )

<div class="btn-group" role="group" aria-label="...">
  <button type="button" class="btn btn-default">Left</button>
  <button type="button" class="btn btn-default">Middle</button>
  <button type="button" class="btn btn-default">Right</button>
</div>
Karren answered 12/5, 2016 at 14:41 Comment(1)
Possibly this Justified button groupsInvolutional
C
92

Flexbox can do that.

.btn-group.special {
  display: flex;
}

.special .btn {
  flex: 1
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="btn-group special" role="group" aria-label="...">
  <button type="button" class="btn btn-default">Left</button>
  <button type="button" class="btn btn-default">Middle</button>
  <button type="button" class="btn btn-default">Right</button>
</div>
Cumbrous answered 12/5, 2016 at 14:54 Comment(3)
best method to useProverb
I would upvote this twice if I could; this works like a charmDrill
For those using bootstrap 4.5 just use .d-flex along .btn-groupCodeine
F
111

Correct solution for Bootstrap 4 and Bootstrap 5 (from Bootstrap 4 migration documentation):

Removed .btn-group-justified. As a replacement you can use <div class="btn-group d-flex" role="group"></div> as a wrapper around elements with .w-100.

Example:

<div class="btn-group d-flex" role="group" aria-label="...">
  <button type="button" class="btn btn-default w-100">Left</button>
  <button type="button" class="btn btn-default w-100">Middle</button>
  <button type="button" class="btn btn-default w-100">Right</button>
</div>

Source: https://getbootstrap.com/docs/4.0/migration/#button-group

Edit: verified solution with Bootstrap 5

Farinose answered 8/2, 2018 at 20:15 Comment(3)
Works perfectly, simple, standard. Thanks.Sardou
Add class w-100 to the .btn-group element, too.Greenberg
for me it also works fine without adding w-100 to each button class in bootstrap 4.5.2Warfarin
C
92

Flexbox can do that.

.btn-group.special {
  display: flex;
}

.special .btn {
  flex: 1
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="btn-group special" role="group" aria-label="...">
  <button type="button" class="btn btn-default">Left</button>
  <button type="button" class="btn btn-default">Middle</button>
  <button type="button" class="btn btn-default">Right</button>
</div>
Cumbrous answered 12/5, 2016 at 14:54 Comment(3)
best method to useProverb
I would upvote this twice if I could; this works like a charmDrill
For those using bootstrap 4.5 just use .d-flex along .btn-groupCodeine
P
7

You can also use bootstraps btn-block will span across parent.

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

<button type="button" class="btn btn-primary btn-lg btn-block">Block level button</button>
<button type="button" class="btn btn-secondary btn-lg btn-block">Block level button</button>

https://getbootstrap.com/docs/4.0/components/buttons/

Plenum answered 25/9, 2019 at 6:22 Comment(0)
G
6

In Bootstrap 4, make use of .d-flex on your .btn-group and .w-100 on individual buttons:

.btn-group.d-flex(role='group')
  button.w-100.btn.btn-lg.btn-success(type='button') 👍
  button.w-100.btn.btn-lg.btn-danger(type='button') 👎
  .btn-group(role='group')
    button#btnGroupDrop1.btn.btn-lg.btn-light.dropdown-toggle(type='button', data-toggle='dropdown')
      | &#8226;&#8226;&#8226;
    .dropdown-menu
      a.dropdown-item(href='#') An option
      a.dropdown-item(href='#') Another option
Gradygrae answered 2/3, 2018 at 23:7 Comment(0)
M
3

You can also use .btn-group-justified.

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <div class="btn-group btn-group-justified">
    <a href="button" class="btn btn-default">Left</a>
    <a href="button" class="btn btn-default">Middle</a>
    <a href="button" class="btn btn-default">Right</a>
  </div>
</div>
Maisey answered 12/5, 2016 at 15:10 Comment(1)
Outdated. Removed .btn-group-justified. As a replacement you can use <div class="btn-group d-flex" role="group"></div> as a wrapper around elements with .w-100. getbootstrap.com/docs/4.3/migration/#button-groupBullwhip
J
2
<div class="col-sm-12">
<div class="btn-group btn-block" role="group">
    <button class="btn btn-success col-sm-10" type="submit">Button 1</button>
    <button class="btn btn-danger col-sm-2" type="submit">Button 2</button>
</div>
Jobbery answered 7/8, 2020 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.