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


当前回答

我在单元格中使用::after元素,我想设置一个最小宽度,而不管当前的文本,如下所示:

.cell::after {
    content: "";
    width: 20px;
    display: block;
}

我不需要在父表上设置宽度,也不需要使用表布局。

其他回答

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

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/

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

eg.

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

您需要在相应的CSS中编写这些内容

table-layout:fixed;

设置:

style="min-width:100px;" 

为我工作。

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

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