Why do Twitter Bootstrap tables always have 100% width?

Twitter Bootstrap

Twitter Bootstrap Problem Overview


Suppose this markup:

<table class="table table-bordered" align="center"> 

No mather how many cells I have, the table is always 100% width. Why's that?

Twitter Bootstrap Solutions


Solution 1 - Twitter Bootstrap

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).

Solution 2 - Twitter Bootstrap

<table style="width: auto;" ... works fine. Tested in Chrome 38 , IE 11 and Firefox 34.

jsfiddle : http://jsfiddle.net/rpaul/taqodr8o/

Solution 3 - Twitter Bootstrap

If you're using Bootstrap 4, use .w-auto.

See https://getbootstrap.com/docs/4.1/utilities/sizing/

Solution 4 - Twitter Bootstrap

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/

Solution 5 - Twitter Bootstrap

I was having the same issue, I made the table fixed and then specified my td width. If you have th you can do those as well.

<style>
table {
table-layout: fixed;
word-wrap: break-word;
}
</style>

<td width="10%" /td>

I didn't have any luck with .table-nonfluid.

Solution 6 - Twitter Bootstrap

I've tried to add style="width: auto !important" and works great for me!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkskaradzinskiView Question on Stackoverflow
Solution 1 - Twitter BootstrapAndres IlichView Answer on Stackoverflow
Solution 2 - Twitter BootstrapRazan PaulView Answer on Stackoverflow
Solution 3 - Twitter BootstrapDanko DurbićView Answer on Stackoverflow
Solution 4 - Twitter BootstrapJessView Answer on Stackoverflow
Solution 5 - Twitter BootstrapDDDDView Answer on Stackoverflow
Solution 6 - Twitter BootstrapSergey DolgopolovView Answer on Stackoverflow