如何在Bootstrap响应表中设置列的大小

Bre*_*ogt 46 html css html5 twitter-bootstrap twitter-bootstrap-3

如何在Bootstrap响应表中设置列的大小?我不希望桌子失去它的反复功能.我也需要它在IE8中工作.我已经包含了HTML5SHIV和Respond.

我正在使用Bootstrap 3(3.2.0)

<div class="table-responsive">
    <table id="productSizes" class="table">
        <thead>
            <tr>
                <th>Size</th>
                <th>Bust</th>
                <th>Waist</th>
                <th>Hips</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>6</td>
                <td>79 - 81</td>
                <td>61 - 63</td>
                <td>89 - 91</td>
            </tr>
            <tr>
                <td>8</td>
                <td>84 - 86</td>
                <td>66 - 68</td>
                <td>94 - 96</td>
            </tr>
        </tbody>
    </table>
</div>
Run Code Online (Sandbox Code Playgroud)

Jor*_*J.D 101

Bootstrap 4.0

请注意从Bootstrap 3到4 的所有迁移更改.在表中,您现在需要通过添加类来启用弹性框d-flex,并放下xs以允许引导程序自动检测视口.

<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>
Run Code Online (Sandbox Code Playgroud)

bootply

Bootstrap 3.2

列宽度使用与网格相同的布局; 使用col-[viewport]-[size].请记住,列大小应为12; 1 + 3 + 3 + 5 = 12在这个例子中.

        <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>
Run Code Online (Sandbox Code Playgroud)

请记住设置<th>元素而不是<td>元素,以便设置整个列.这是一个有效的BOOTPLY.

感谢@Dan提醒我始终首先使用移动视图(col-xs-*).

  • 刚刚使用 Bootstrap 4 对此进行了测试,但它在 Chrome 和 Opera 中不起作用。 (3认同)
  • 毫无疑问,明白了,也许我错过了……我只是说,因为我相信默认值应该是“col-xs-..”,因为它总是响应式的,并被更大的屏幕继承。而“col-lg-..”仅适用于大屏幕。 (2认同)
  • 如果表中没有 `&lt;th&gt;` 元素怎么办? (2认同)

Dan*_*Dan 17

您可以使用内联样式并在<th>标记中定义宽度.使其宽度的总和= 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>
Run Code Online (Sandbox Code Playgroud)

Bootply演示

通常使用内联样式并不理想,但这确实提供了灵活性,因为您可以获得具有精确宽度的非常具体和粒度.

  • 如果您需要确保列的宽度相等或具有某种比例宽度,则效果非常好。似乎百分比宽度不需要加起来为 100%,因此也很容易用于动态表(至少对于 Chrome) (2认同)

小智 6

您可以使用以下 Bootstrap 类

<tr class="w-25">

</tr>
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请查看以下页面 https://getbootstrap.com/docs/4.1/utilities/sizing/