159

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?

4
  • 3
    because it efficiently uses its parents width; that is how grid system should work!
    – Vishal
    May 21, 2012 at 14:40
  • What width do you want your table to be? May 21, 2012 at 14:49
  • 1
    I would like it to be depent on number of cells, just like ordinary table May 21, 2012 at 15:03
  • 1
    For those who searching how to set cell width like div width via span check this answer
    – alexche8
    Oct 23, 2014 at 7:35

6 Answers 6

244

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

5
  • 1
    If you place the table inside a span* tag, in order to center the table, would you just include an offset*, too?
    – HPWD
    Mar 12, 2013 at 17:46
  • @dlackey Yes, you can offset the table with a class in order to center, you can even add a .span* class to your table to give it some width. Mar 12, 2013 at 20:51
  • 2
    At least with bootstrap 3 it only worked for me after adding !important to the width: auto
    – geekQ
    Mar 6, 2014 at 16:43
  • we have to use bootstarp's span class for a table , or we have to manually give a style for .span class, Jul 28, 2014 at 14:55
  • 1
    Warning. Be careful if used in combination with .table-responsive and .table-bordered. The border is applied to the .table-responsive container when below screen width 767px. (bootstrap 3.x)
    – Stuart
    Dec 2, 2015 at 10:53
36

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

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

1
  • This is the correct answer if you want to keep it to bootstrap and make the table auto-size but not use the column size class within a row.
    – Harlin
    Feb 16, 2022 at 2:42
33

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

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

17

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/

2
  • 5
    this is by far the simplest solution and should be the accepted answer
    – Jeff Brown
    Apr 19, 2018 at 19:22
  • Personally I think the one above it is just as good but you're right, you can't go wrong with this one either way. Still works in BS5.
    – Harlin
    Feb 16, 2022 at 2:43
12

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.

2
3

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

2
  • 1
    If you ensure that your custom CSS appears after the Bootstrap CSS (e.g. your custom styles <link> tag appears below the Bootstrap <link> tag), then the rules will cascade as designed, and you shouldn't need to use your !important exception at all. This is true because width: 100%; is defined for table in the Bootstrap CSS and so is superseding your width: auto; rule. Oct 15, 2014 at 9:01
  • Not really an answer, better to edit the current accepted answer which most of the people read (I edited it now). Feb 11, 2015 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.