在我的表格中,我设置列中第一个单元格的宽度为100px。 但是,当此列中的某个单元格中的文本太长时,列的宽度就会超过100px。如何禁用此扩展?


当前回答

只需在<td>或<th>定义宽度<div>内添加<div>标签。 这对你有帮助。别的都不管用。

eg.

<td><div style="width: 50px" >...............</div></td>

其他回答

凯旋的想法是对的。这是正确的代码…

<style type="text/css">
  th.first-col > div, 
  td.first-col > div {
    overflow:hidden;
    white-space:nowrap;
    width:100px
  }
</style>

<table>
  <thead><tr><th class="first-col"><div>really long header</div></th></tr></thead>
  <tbody><tr><td class="first-col"><div>really long text</div></td></tr></tbody>
</table>

你不需要设置“fixed”-你只需要设置overflow:隐藏,因为列宽度已经设置好了。

这也有助于,在最后一个“填充单元格”,宽度:auto。这将占用剩余空间,并保留指定的所有其他维度。

只需在<td>或<th>定义宽度<div>内添加<div>标签。 这对你有帮助。别的都不管用。

eg.

<td><div style="width: 50px" >...............</div></td>

使接受的答案响应小屏幕时,小于固定宽度。

HTML:

<table>
  <tr>
    <th>header 1</th>
    <th>header 234567895678657</th>
  </tr>
  <tr>
    <td>data asdfasdfasdfasdfasdf</td>
    <td>data 2</td>
  </tr>
</table>

CSS

table{
    border: 1px solid black;
    table-layout: fixed;
    max-width: 600px;
    width: 100%;
}

th, td {
    border: 1px solid black;
    overflow: hidden;
    max-width: 300px;
    width: 100%;
}

JS小提琴

https://jsfiddle.net/w9s3ebzt/