I'm using bootstrap, and want to create a simple HTML table
, where in the last column I have a 2-button group and a single separate button. I want this last cell to always keep this 3 buttons in one line, so I added the text-nowrap
bootstrap class (which is a simple white-space: nowrap
).
If the content of the other cell(s) is long enough, the content of the last cell will overflow horizontally, and I don't understand why. I'd expect that the rest of the table will shrink to make space for the non-wrapping cell.
Here is a demo to reproduce the issue: bootply
Here is the markup.
<div style="width: 500px">
<table class="table table-striped">
<tbody><tr>
<td>Test text</td>
<td class="text-nowrap">
<div class="btn-group">
<button class="btn btn-default"><span>A</span></button>
<button class="btn btn-warning"><span>B</span></button>
</div>
<button class="btn btn-success"><span>C</span></button>
</td>
</tr>
</tbody></table>
</div>
The button group div
and the separate button
are all inline-block
elements, so the wrapping should work as expected IMO. What am I doing wrong?
Maybe this is not strictly related to bootstrap.
UPDATE
The cause and the workaround
btn-group
will result a correct layout, so I started to examine what actually the btn-group
does.btn
s in the btn-group
are floated, and this floating causes the bad layout. clearfix
class to the btn-group
solves the issue.float: left
is needed for bootstrap, it seems like the buttons in the group could be aligned by using purely inline-block
without margin. I will check this in the v4-alpha
code, and will also report this issue to bootstrap v3
.The <table class="table">
will fill 100% width of it's container. Column width are divided proportionally to the width of it's content.
When the left hand side claim bigger portion, the button column will fall "short".
When the left hand side has smaller portion, the buttons column will grow larger, thus making the buttons "move toward the center".
To solve: Set the width of the right hand colum with a fixed value. A total value of combined width of all three buttons: width:125px
. As shown below:
<div style="width: 500px">
<table class="table table-striped">
<tbody><tr>
<td>Test text</td>
<td class="text-nowrap" style="width:125px;">
<div class="btn-group">
<button class="btn btn-default"><span>A</span></button>
<button class="btn btn-warning"><span>B</span></button>
</div>
<button class="btn btn-success"><span>C</span></button>
</td>
</tr>
</tbody></table>
</div>
See: http://www.bootply.com/PGL6xCDgMz
This will set the width of the button column to a fixed value of 125px. So the left hand column will get the rest of any portion left.
Update 1: (according to questioner comments)
- technique 1:
<div style="width: 500px">
<table class="table table-striped">
<tbody><tr>
<td>Test text</td>
<td class="text-nowrap">
<table cellpadding="0" cellspacing="0">
<tr>
<td><button class="btn btn-default"><span>A</span></button></td>
<td><button class="btn btn-warning"><span>B</span></button></td>
<td><button class="btn btn-success"><span>C</span></button></td>
</tr>
</table>
</td>
</tr>
</tbody></table>
</div>
- technique 2:
<div style="width: 500px">
<table class="table table-striped">
<tbody><tr>
<td>Test text</td>
<td><button class="btn btn-default"><span>A</span></button></td>
<td><button class="btn btn-warning"><span>B</span></button></td>
<td><button class="btn btn-success"><span>C</span></button></td>
</tr>
</tbody></table>
</div>
You Can Use Another One Also
td {
max-width: 100px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With