【问题标题】:Table column sizing表列大小
【发布时间】:2016-10-21 19:15:38
【问题描述】:

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>

查看 codeply 提供的大小不正确,尤其是当您向表中添加一些数据时。看看它是如何运行的:

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

【问题讨论】:

  • 作为这个问题的附录:如果您有很长的内容(例如非常长的 URL)来显示引导程序 4 表,即使使用下面的大小调整答案也不是一个很好的解决方案。使用 flex-row 或 row/col 解决方案往往会更好地使用溢出和文本换行

标签: css twitter-bootstrap html-table bootstrap-4


【解决方案1】:

2018 年更新

确保您的表包含table 类。这是因为Bootstrap 4 tables are "opt-in" 所以必须有意将table 类添加到表中。

http://codeply.com/go/zJLXypKZxL

Bootstrap 3.x 也有一些 CSS 来重置表格单元格,这样它们就不会浮动..

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

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

Bootstrap 4 Alpha 2 Demo


更新(自 Bootstrap 4.0.0 起)

现在 Bootstrap 4 是 flexbox,添加 col-* 时表格单元格将不会假定正确的宽度。一种解决方法是在表格单元格上使用 d-inline-block 类来阻止默认的 display:flex 列。

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 更改为 display:flex can alter the borders

【讨论】:

  • 实际上大小不合适。如果您添加一些边框以查看大小如何以及何时将一行添加到正文,请检查它。我把代码放在问题中
  • 实际上,如果您的列中的内容溢出,则您的 BS4-A6 示例不起作用,因为其他列没有扩展。尝试在一列中粘贴一些 lorem ipsum。
  • 您的解决方案有效。就我而言,我需要在 tr 中添加 no-gutters 以避免负填充 jsfiddle.net/Vimalan/xhvdcasn
  • 是的,w-25w-50w-75 类与引导程序 4 一起使用。谢谢!
【解决方案2】:

另一种选择是在表格行应用 flex 样式,并将col-classes 添加到表格标题/表格数据元素:

<table>
  <thead>
    <tr class="d-flex">
      <th class="col-3">3 columns wide header</th>
      <th class="col-sm-5">5 columns wide header</th>
      <th class="col-sm-4">4 columns wide header</th>
    </tr>
  </thead>
  <tbody>
    <tr class="d-flex">
      <td class="col-3">3 columns wide content</th>
      <td class="col-sm-5">5 columns wide content</th>
      <td class="col-sm-4">4 columns wide content</th>
    </tr>
  </tbody>
</table>

【讨论】:

  • 有效,但 vertical-align: middle 不再适用于 d-flex 类。
  • 垂直对齐确实不适用于 flexbox 布局。使用align-items: baseline。要了解有关 flexbox 的更多信息,请阅读此guide
  • 这就是解决方案。谢谢。
【解决方案3】:

使用d-flex 类效果很好,但其他一些属性不再起作用,例如vertical-align: middle 属性。

我发现最容易调整列大小的方法是仅在thead 单元格中使用带有百分比的width 属性。

<table class="table">
    <thead>
        <tr>
            <th width="25%">25%</th>
            <th width="25%">25%</th>
            <th width="50%">50%</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>25%</td>
            <td>25%</td>
            <td>50%</td>
        </tr>
    </tbody>
</table>

【讨论】:

  • 注意HTML5 中现在不支持为 使用宽度属性
【解决方案4】:

在看到@florian_korner 的帖子之前,我根据自己的需要将其破解为发布 Bootstrap 4.1.1。看起来非常相似。

如果您使用 sass,您可以将这个 sn-p 粘贴到引导程序包含的末尾。它似乎解决了 chrome、IE 和 edge 的问题。似乎没有破坏 Firefox 中的任何内容。

@mixin make-td-col($size, $columns: $grid-columns) {
    width: percentage($size / $columns);
}

@each $breakpoint in map-keys($grid-breakpoints) {
    $infix: breakpoint-infix($breakpoint, $grid-breakpoints);

    @for $i from 1 through $grid-columns {
        td.col#{$infix}-#{$i}, th.col#{$infix}-#{$i} {
            @include make-td-col($i, $grid-columns);
        }
    }
}

或者如果您只想要编译后的 css 实用程序:

td.col-1, th.col-1 {
  width: 8.33333%; }

td.col-2, th.col-2 {
  width: 16.66667%; }

td.col-3, th.col-3 {
  width: 25%; }

td.col-4, th.col-4 {
  width: 33.33333%; }

td.col-5, th.col-5 {
  width: 41.66667%; }

td.col-6, th.col-6 {
  width: 50%; }

td.col-7, th.col-7 {
  width: 58.33333%; }

td.col-8, th.col-8 {
  width: 66.66667%; }

td.col-9, th.col-9 {
  width: 75%; }

td.col-10, th.col-10 {
  width: 83.33333%; }

td.col-11, th.col-11 {
  width: 91.66667%; }

td.col-12, th.col-12 {
  width: 100%; }

td.col-sm-1, th.col-sm-1 {
  width: 8.33333%; }

td.col-sm-2, th.col-sm-2 {
  width: 16.66667%; }

td.col-sm-3, th.col-sm-3 {
  width: 25%; }

td.col-sm-4, th.col-sm-4 {
  width: 33.33333%; }

td.col-sm-5, th.col-sm-5 {
  width: 41.66667%; }

td.col-sm-6, th.col-sm-6 {
  width: 50%; }

td.col-sm-7, th.col-sm-7 {
  width: 58.33333%; }

td.col-sm-8, th.col-sm-8 {
  width: 66.66667%; }

td.col-sm-9, th.col-sm-9 {
  width: 75%; }

td.col-sm-10, th.col-sm-10 {
  width: 83.33333%; }

td.col-sm-11, th.col-sm-11 {
  width: 91.66667%; }

td.col-sm-12, th.col-sm-12 {
  width: 100%; }

td.col-md-1, th.col-md-1 {
  width: 8.33333%; }

td.col-md-2, th.col-md-2 {
  width: 16.66667%; }

td.col-md-3, th.col-md-3 {
  width: 25%; }

td.col-md-4, th.col-md-4 {
  width: 33.33333%; }

td.col-md-5, th.col-md-5 {
  width: 41.66667%; }

td.col-md-6, th.col-md-6 {
  width: 50%; }

td.col-md-7, th.col-md-7 {
  width: 58.33333%; }

td.col-md-8, th.col-md-8 {
  width: 66.66667%; }

td.col-md-9, th.col-md-9 {
  width: 75%; }

td.col-md-10, th.col-md-10 {
  width: 83.33333%; }

td.col-md-11, th.col-md-11 {
  width: 91.66667%; }

td.col-md-12, th.col-md-12 {
  width: 100%; }

td.col-lg-1, th.col-lg-1 {
  width: 8.33333%; }

td.col-lg-2, th.col-lg-2 {
  width: 16.66667%; }

td.col-lg-3, th.col-lg-3 {
  width: 25%; }

td.col-lg-4, th.col-lg-4 {
  width: 33.33333%; }

td.col-lg-5, th.col-lg-5 {
  width: 41.66667%; }

td.col-lg-6, th.col-lg-6 {
  width: 50%; }

td.col-lg-7, th.col-lg-7 {
  width: 58.33333%; }

td.col-lg-8, th.col-lg-8 {
  width: 66.66667%; }

td.col-lg-9, th.col-lg-9 {
  width: 75%; }

td.col-lg-10, th.col-lg-10 {
  width: 83.33333%; }

td.col-lg-11, th.col-lg-11 {
  width: 91.66667%; }

td.col-lg-12, th.col-lg-12 {
  width: 100%; }

td.col-xl-1, th.col-xl-1 {
  width: 8.33333%; }

td.col-xl-2, th.col-xl-2 {
  width: 16.66667%; }

td.col-xl-3, th.col-xl-3 {
  width: 25%; }

td.col-xl-4, th.col-xl-4 {
  width: 33.33333%; }

td.col-xl-5, th.col-xl-5 {
  width: 41.66667%; }

td.col-xl-6, th.col-xl-6 {
  width: 50%; }

td.col-xl-7, th.col-xl-7 {
  width: 58.33333%; }

td.col-xl-8, th.col-xl-8 {
  width: 66.66667%; }

td.col-xl-9, th.col-xl-9 {
  width: 75%; }

td.col-xl-10, th.col-xl-10 {
  width: 83.33333%; }

td.col-xl-11, th.col-xl-11 {
  width: 91.66667%; }

td.col-xl-12, th.col-xl-12 {
  width: 100%; }

【讨论】:

    【解决方案5】:

    免责声明:这个答案可能有点老了。自引导程序 4 测试版以来。自那以后,Bootstrap 发生了变化。

    表格列大小类已从此更改

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

    <th class="col-3">3 columns wide</th>
    

    【讨论】:

    【解决方案6】:

    我可以使用 Bootstrap 4 使用以下代码解决此问题:

    <table class="table">
      <tbody>
        <tr class="d-flex">
          <th class="col-3" scope="row">Indicador:</th>
          <td class="col-9">this is my indicator</td>
        </tr>
    
        <tr class="d-flex">
          <th class="col-3" scope="row">Definición:</th>
          <td class="col-9">This is my definition</td>
        </tr>
    
      </tbody>
    </table>
    

    【讨论】:

      【解决方案7】:

      从 Alpha 6 开始,您可以创建以下 sass 文件:

      @each $breakpoint in map-keys($grid-breakpoints) {
        $infix: breakpoint-infix($breakpoint, $grid-breakpoints);
      
        col, td, th {
          @for $i from 1 through $grid-columns {
              &.col#{$infix}-#{$i} {
                flex: none;
                position: initial;
              }
          }
      
          @include media-breakpoint-up($breakpoint, $grid-breakpoints) {
            @for $i from 1 through $grid-columns {
              &.col#{$infix}-#{$i} {
                width: 100% / $grid-columns * $i;
              }
            }
          }
        }
      }
      

      【讨论】:

      • 这段代码根本不起作用 - 尝试调试了 1.5 小时,但我对 sass 的了解太弱,无法做到这一点。请下次发布工作示例。
      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 2017-11-03
      • 2011-08-16
      • 1970-01-01
      相关资源
      最近更新 更多