Why do Twitter Bootstrap tables always have 100% width?

前端 未结 6 489
南方客
南方客 2021-01-29 23:47

Suppose this markup:

No mather how many cells I have, the table is always 100% w

相关标签:
6条回答
  • 2021-01-30 00:14

    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:

    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/

    0 讨论(0)
  • 2021-01-30 00:15

    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.

    0 讨论(0)
  • 2021-01-30 00:18

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

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

    0 讨论(0)
  • 2021-01-30 00:26

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

    0 讨论(0)
  • 2021-01-30 00:26

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

    0 讨论(0)
  • 2021-01-30 00:37

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

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

    0 讨论(0)
提交回复
热议问题