How to set the size of a column in a Bootstrap responsive table

后端 未结 3 699
说谎
说谎 2020-12-22 22:19

How do you set the size of a column in a Bootstrap responsive table? I don\'t want the table to loose its reponsive features. I need it to work in IE8 as well. I have includ

相关标签:
3条回答
  • 2020-12-22 22:50

    you can use the following Bootstrap class with

    <tr class="w-25">
    
    </tr>
    

    for more details check the following page https://getbootstrap.com/docs/4.1/utilities/sizing/

    0 讨论(0)
  • 2020-12-22 22:56

    Bootstrap 4.0

    Be aware of all migration changes from Bootstrap 3 to 4. On the table you now need to enable flex box by adding the class d-flex, and drop the xs to allow bootstrap to automatically detect the viewport.

    <div class="container-fluid">
        <table id="productSizes" class="table">
            <thead>
                <tr class="d-flex">
                    <th class="col-1">Size</th>
                    <th class="col-3">Bust</th>
                    <th class="col-3">Waist</th>
                    <th class="col-5">Hips</th>
                </tr>
            </thead>
            <tbody>
                <tr class="d-flex">
                    <td class="col-1">6</td>
                    <td class="col-3">79 - 81</td>
                    <td class="col-3">61 - 63</td>
                    <td class="col-5">89 - 91</td>
                </tr>
                <tr class="d-flex">
                    <td class="col-1">8</td>
                    <td class="col-3">84 - 86</td>
                    <td class="col-3">66 - 68</td>
                    <td class="col-5">94 - 96</td>
                </tr>
            </tbody>
        </table>
    

    bootply

    Bootstrap 3.2

    Table column width use the same layout as grids do; using col-[viewport]-[size]. Remember the column sizes should total 12; 1 + 3 + 3 + 5 = 12 in this example.

            <thead>
                <tr>
                    <th class="col-xs-1">Size</th>
                    <th class="col-xs-3">Bust</th>
                    <th class="col-xs-3">Waist</th>
                    <th class="col-xs-5">Hips</th>
                </tr>
            </thead>
    

    Remember to set the <th> elements rather than the <td> elements so it sets the whole column. Here is a working BOOTPLY.

    Thanks to @Dan for reminding me to always work mobile view (col-xs-*) first.

    0 讨论(0)
  • 2020-12-22 23:03

    You could use inline styles and define the width in the <th> tag. Make it so that the sum of the widths = 100%.

        <tr>
            <th style="width:10%">Size</th>
            <th style="width:30%">Bust</th>
            <th style="width:50%">Waist</th>
            <th style="width:10%">Hips</th>
        </tr>
    

    Bootply demo

    Typically using inline styles is not ideal, however this does provide flexibility because you can get very specific and granular with exact widths.

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