Bootstrap 3中,我可以将col-sm-xx应用于thead列中的th个标记,并随意调整表列的大小.但是,这在Bootstrap 4中不起作用,我如何在Bootstrap 4中实现这样的功能呢?

<thead>
<th class="col-sm-3">3 columns wide</th>
<th class="col-sm-5">5 columns wide</th>
<th class="col-sm-4">4 columns wide</th>
</thead>

如果代码的大小不正确,请查看代码,特别是在向表中添加一些数据的情况下.请查看此命令的运行方式:

<div class="container" style="border: 1px solid black;">
    <table class="table table-bordered">
    <thead>
        <tr class="row">
            <th class="col-sm-3">3 columns wide</th>
            <th class="col-sm-5">5 columns wide</th>
            <th class="col-sm-4">4 columns wide</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>123</td>
            <td>456</td>
            <td>789</td>
        </tr>
    </tbody>
</table>
</div>

推荐答案

Updated 2018

请确保您的餐桌包括table级.这是因为Bootstrap 4 tables are "opt-in",所以必须故意将table类添加到表中.

http://codeply.com/go/zJLXypKZxL

Bootstrap3.x还提供了一些CSS来重置表格单元格,以便它们不会浮动.

table td[class*=col-], table th[class*=col-] {
    position: static;
    display: table-cell;
    float: none;
}

我不知道为什么这不是Bootstrap 4alpha,但它可能会在最终版本中添加回来.添加此CSS将帮助所有列使用thead.中设置的宽度.

Bootstrap 4 Alpha 2 Demo


UPDATE (as of Bootstrap 4.0.0)

现在Bootstrap 4是Flexbox,表格单元格在添加col-*时将不会采用正确的宽度.一种解决方法是在表格单元格上使用d-inline-block类来阻止默认显示:列的伸缩.

BS4中的另一个选项是使用调整大小实用程序类来表示宽度……

<thead>
     <tr>
           <th class="w-25">25</th>
           <th class="w-50">50</th>
           <th class="w-25">25</th>
     </tr>
</thead>

Bootstrap 4 Alpha 6 Demo

最后,您可以对表行(Tr)使用d-flex,对列(th,td)使用col-*个网格类……

<table class="table table-bordered">
        <thead>
            <tr class="d-flex">
                <th class="col-3">25%</th>
                <th class="col-3">25%</th>
                <th class="col-6">50%</th>
            </tr>
        </thead>
        <tbody>
            <tr class="d-flex">
                <td class="col-sm-3">..</td>
                <td class="col-sm-3">..</td>
                <td class="col-sm-6">..</td>
            </tr>
        </tbody>
    </table>

Bootstrap 4.0.0 (stable) Demo

注:将TR更改为显示:FLEX can alter the borders

Css相关问答推荐

将CSS更改为Mat-Form-Field,其中包含搜索框

如何将下拉面板放置在MAT-SELECT文本框的正下方

如何在Angular material 选项卡中设置自定义圆角下划线的样式

如何在容器查询体中重新定义:Root的CSS自定义属性值?

在AND设计令牌上使用不同的主题

如何解决 Spotify 嵌入代码中的白色背景错误?

使用 Flex 容器,如何在右对齐所有 Flex 子项的同时赋予子图像完整宽度?

根据子索引计算变换 CSS 参数

如何在启动时滚动 div 的底部?

图像占用尽可能多的空间但保持宽高比并包含在窗口高度中

Angular 导航栏问题

CSS Stepper - LI After Overlaying LI Before

在嵌套的弹性框中创建弹性项目之间的空间

flex-start 和基线有什么区别?

导航栏中的 Bootstrap 3.0 按钮

使用 float:left 后如何获得新行?

什么是 DOM 回流?

HTML5 和边框

CSS中伪元素前的&是什么意思?

如何使用纯 CSS 创建工具提示尾部?