How would you let bootstrap self-spread buttons to 100% width of their parent, without adjusting each of the button’s width separately ?
for example if i have 8 buttons, 100% containers width / 8 buttons = 12% width for each button.
i would like to:
-
avoid specifying each button’s width (today i got 8 tomorrow i’ll
have 20). -
adjusting the whole .btn-group’s child buttons to the center (if you pay close attention you’ll see the buttons inside the container are left-aligned for some reason).
.btn-group {
width: 100%;
}
.btn-group>input.btn {
width: 12%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container panel panel-default">
<div class="btn-group">
<input type="button" class="btn btn-primary btn-responsive" value="A" />
<input type="button" class="btn btn-primary btn-responsive" value="AB" />
<input type="button" class="btn btn-primary btn-responsive" value="ABC" />
<input type="button" class="btn btn-primary btn-responsive" value="ABCD" />
<input type="button" class="btn btn-primary btn-responsive" value="ABCDE" />
<input type="button" class="btn btn-primary btn-responsive" value="ABCD" />
<input type="button" class="btn btn-primary btn-responsive" value="ABC" />
<input type="button" class="btn btn-primary btn-responsive" value="AB" />
</div>
<br/>
<br/>
<br/>
<p>the buttons above don't take full width of the container</p>
</div>
Example CSS:
.btn-group {
width: 100%;
}
.btn-group>input.btn {
width: 12%;
}
3
Answers
This effect is easiest achieved with flexbox.
Depending on your what browers you target you might want to prefix
HTML added
.row
inside grid which will allow you to reach 100%.no I have added
.col-xs-12
inside.row
which might not be necessary but i have tried to follow what bootstrap gives ie container > row > column.additions .. make
.btn-group
to be.btn-block
too, it spans it 100%.CSS : i have removed padding from ‘column’ and removed margin from buttons insided button group
If using Flexbox isn’t the best option for your use case then you can use a
table
layout. Another advantage is the control you have with regard to responsive behavior since the defaultbtn-group
doesn’t offer much in this regard.Basic Example: Run with FullPage
Responsive Example: Run with FullPage and reduce the browser window