【问题标题】:How to make a column auto width in fixed layout table in 2018如何在 2018 年的固定布局表中使列自动宽度
【发布时间】:2018-07-07 17:28:32
【问题描述】:

我相信width: 1px; white-space: nowrap; 的伎俩以前有效,但现在似乎不再有效了?参考。 CSS table column autowidth(那里的表格也是固定布局的,但那是在 2011 年)

这里是 HTML 和 CSS:

table {
  table-layout: fixed;
  width: 100%;
}

td,
th {
  border: 1px solid #D5D5D5;
  padding: 15px;
}

.auto {
  width: 1px;
  white-space: nowrap;
}
<table cellspacing="0" cellpadding="0" border="0">
  <thead>
    <tr>
      <th>Column 1 even width</th>
      <th>Column 2 even width</th>
      <th>Column 3 even width</th>
      <th class="auto">Auto</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
      <td>Data 3</td>
      <td class="auto">Data4</td>
    </tr>
  </tbody>
</table>

我在下面的 jsfiddle 中也有一个示例设置:

http://jsfiddle.net/hCkch/21/

如何根据内容使最后一列自动宽度,而其他列尊重table-layout: fixed

注意:上面给出的三列只是一个例子。因此,请不要硬编码答案以使每列宽度为 100/3%。这是一个一般性问题,答案应该适合宽度均匀的 n 列,但最后一列会根据内容自动宽度。

【问题讨论】:

  • 删除table-layout: fixed?根据您的小提琴,我不确定这对您有什么好处...
  • @billynoah 你是什么意思?需要使列均匀分布。
  • 也许我遗漏了一些东西,但我不明白为什么你需要一个固定的布局才能到达那里。并且 afaik width: 1px 技巧只适用于自动布局。这不是你所追求的吗? jsfiddle.net/hCkch/22
  • 看到这个:jsfiddle.net/hCkch/23,你会看到其他三列有不同的宽度。然后尝试加回固定布局看看。

标签: html css html-table


【解决方案1】:

正如 cmets 中所提到的,我知道要跨浏览器工作的唯一方法是摆脱 table-layout: fixed; 并设置剩余列的宽度。固定布局对很多事情都有好处,但根据内容自动计算似乎不是其中之一。

table {
  width: 100%;
}

td, th {
  border: 1px solid #D5D5D5;
  padding: 15px;
  width: 33.33%
}

.auto {
  white-space: nowrap;
}
<table cellspacing="0" cellpadding="0" border="0">
  <thead>
    <tr>
      <th>Column 1 even width</th>
      <th>Column 2</th>
      <th>Column 3</th>
      <th class="auto">Auto</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
      <td>Data 3</td>
      <td>Data4</td>
    </tr>
  </tbody>
</table>

我知道您已澄清您不想要标记特定的规则,但实际上它确实实现了您的目标,即您事先知道要处理多少列。一般来说,这不应该是令人望而却步的。

另一种可能的解决方法是事后使用 js 为客户端分配宽度:

$(document).ready(function() {
  var width = 100 / $('table tr:first th:not(.auto)').length;
  var cols = $('table th:not(.auto)');
  cols.css('width', width + '%');
});
table {
  width: 100%;
}

td, th {
  border: 1px solid #D5D5D5;
  padding: 15px;
}

.auto {
  white-space: nowrap;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table cellspacing="0" cellpadding="0" border="0">
  <thead>
    <tr>
      <th>Column 1 even width</th>
      <th>Column 2</th>
      <th>Column 3</th>
      <th class="auto">Auto</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
      <td>Data 3</td>
      <td>Data4</td>
    </tr>
  </tbody>
</table>

最后,我要提一下 min-content 的 Firefox 特定实现,它实际上完全符合您的要求。不幸的是,它只适用于 Firefox:

table {
  width: 100%;
  table-layout: fixed;
}

td, th {
  border: 1px solid #D5D5D5;
  padding: 15px;
}

.auto {
  width: -moz-min-content;
  white-space: nowrap;
}
<table cellspacing="0" cellpadding="0" border="0">
  <thead>
    <tr>
      <th>Column 1 even width</th>
      <th>Column 2</th>
      <th>Column 3</th>
      <th class="auto">Auto</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
      <td>Data 3</td>
      <td>Data4</td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 感谢您提供详细示例。我仍然希望有一个非编程的解决方案,比如你在这里拥有的 firefox,而不需要求助于 js。我将等待另一个答案,看看是否有其他知道的人可以分享这个技巧。
【解决方案2】:

使用表格布局,您需要设置表格第一行中单元格的宽度 (https://developer.mozilla.org/en-US/docs/Web/CSS/table-layout)。您还需要为旨在自动扩展的最后一列添加 100% 的宽度。这是您的 jsfiddle 的编辑: http://jsfiddle.net/jessbodie/hCkch/31/

<table cellspacing="0" cellpadding="0" border="0">
  <thead>
    <tr>
      <th class="col1st">Column 1 even width</th>
      <th class="col2nd">Column 2 even width</th>
      <th class="col3rd">Column 3 even width</th>
      <th class="auto">Auto</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
      <td>Data 3</td>
      <td class="auto">Data4</td>
    </tr>
  </tbody>
</table>

table {
  table-layout: fixed;
  width: 100%;
}

td, th {
  border: 1px solid red;
  padding: 15px;
}

.col1st {
  width: 100px;
}

.col2nd {
  width: 100px;
}

.col3rd {
  width: 100px;
}

.auto {
  width: 100%;
  white-space: nowrap;
}

为了使前 n 列具有相同的宽度,在 SASS 中,您可以使用 calc 函数来计算宽度。

【讨论】:

  • 谢谢,但如果我已经硬编码(或 SASS 计算)其他列的宽度,那么使用 table-layout: fixed 有什么意义?
  • 性能。根据 MDN:“在“固定”布局方法下,一旦下载并分析了第一行表格,就可以渲染整个表格。与“自动”布局方法相比,这可以加快渲染时间”MDN on Table Layout
猜你喜欢
  • 1970-01-01
  • 2017-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
相关资源
最近更新 更多