Why do Twitter Bootstrap tables always have 100% width?
Bootstrap 3:
Why fight it? Why not simply control your table width using the bootstrap grid?
<div class="row">
<div class="col-sm-6">
<table></table>
</div>
</div>
This will create a table that is half (6 out of 12) of the width of the containing element.
I sometimes use inline styles as per the other answers, but it is discouraged.
Bootstrap 4 and 5:
Bootstrap 4 has some nice helper classes for width like w-25
, w-50
, w-75
, w-100
, and w-auto
. This will make the table 50% width:
<table class="w-50"></table>
Here's the doc: https://getbootstrap.com/docs/4.0/utilities/sizing/
<table style="width: auto;" ...
works fine. Tested in Chrome 38 , IE 11 and Firefox 34.
jsfiddle : http://jsfiddle.net/rpaul/taqodr8o/
All tables within the bootstrap stretch according to their container, which you can easily do by placing your table inside a .span*
grid element of your choice. If you wish to remove this property you can create your own table class and simply add it to the table you want to expand with the content within:
.table-nonfluid {
width: auto !important;
}
You can add this class inside your own stylesheet and simply add it to the container of your table like so:
<table class="table table-nonfluid"> ... </table>
This way your change won't affect the bootstrap stylesheet itself (you might want to have a fluid table somewhere else in your document).
If you're using Bootstrap 4, use .w-auto
.
See https://getbootstrap.com/docs/4.1/utilities/sizing/