How to get button groups that span the full width

2020-05-17 02:32发布

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>

5条回答
Anthone
2楼-- · 2020-05-17 02:45

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')                                                                     
查看更多
家丑人穷心不美
3楼-- · 2020-05-17 02:48

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>

查看更多
叼着烟拽天下
4楼-- · 2020-05-17 02:51

Correct solution for Bootstrap 4 (from its 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

查看更多
我命由我不由天
5楼-- · 2020-05-17 02:53

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>

查看更多
在下西门庆
6楼-- · 2020-05-17 03:09

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/

查看更多
登录 后发表回答