在我的表格中,我设置列中第一个单元格的宽度为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/

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

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

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

我发现凯旋的答案使用vw而不是px更好,就像这样:

< td > < div =“宽度:10大众风格 " >...............</ 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>

我用了这个

.app_downloads_table tr td:first-child {
    width: 75%;
}

.app_downloads_table tr td:last-child {
    text-align: center;
}