弗雷德会面。他是一张桌子:

<table border="1" style="width: 100%;">
    <tr>
        <td>This cells has more content</td>
        <td>Less content here</td>
    </tr>
</table>

弗雷德的公寓有改变大小的奇怪习惯,所以他学会了隐藏一些他的内容,这样就不会把其他所有的单元都挤过去,把惠特福德夫人的客厅挤到被遗忘的地方:

<table border="1" style="width: 100%; white-space: nowrap; table-layout: fixed;">
    <tr>
        <td style="overflow: hidden; text-overflow: ellipsis">This cells has more content</td>
        <td style="overflow: hidden; text-overflow: ellipsis">Less content here</td>
    </tr>
</table>

这种方法很有效,但弗雷德有一种挥之不去的感觉,如果他的右细胞(他昵称为Celldito)让出一点空间,他的左细胞就不会经常被截断。你能挽救他的理智吗?


总之:表的单元格如何才能均匀溢出,并且只有在它们都放弃了所有空白的情况下才能溢出?


当前回答

<table border="1" style="width: 100%;">
    <colgroup>
        <col width="100%" />
        <col width="0%" />
    </colgroup>
    <tr>
        <td style="white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:1px;">This cell has more content.This cell has more content.This cell has more content.This cell has more content.This cell has more content.This cell has more content.</td>
        <td style="white-space: nowrap;">Less content here.</td>
    </tr>
</table>

http://jsfiddle.net/7CURQ/

其他回答

使用一些css hack,似乎显示:table-column;可以来拯救:

.myTable { display: table; width: 100%; } .myTable:before { display: table-column; width: 100%; content: ''; } .flexibleCell { display: table-cell; max-width: 1px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; } .staticCell { white-space: nowrap; } <div class="myTable"> <div class="flexibleCell">A very long piece of content in first cell, long enough that it would normally wrap into multiple lines.</div> <div class="staticCell">Less content</div> </div>

JSFiddle: http://jsfiddle.net/blai/7u59asyp/

不知道这是否对任何人都有帮助,但我通过为每列指定特定的宽度大小来解决类似的问题。显然,如果每个列的内容宽度变化不太大,那么效果最好。

几天前我也遇到了同样的挑战。看来路西法·萨姆找到了最好的解决办法。

但我注意到你应该在spacer元素复制内容。认为它不是那么糟糕,但我也想应用标题弹出剪切文本。这意味着长文本将在我的代码中出现第三次。

在这里,我建议在伪元素之后访问title属性以生成间隔并保持HTML干净。

适用于IE8+, FF, Chrome, Safari, Opera

<table border="1">
  <tr>
    <td class="ellipsis_cell">
      <div title="This cells has more content">
        <span>This cells has more content</span>
      </div>
    </td>
    <td class="nowrap">Less content here</td>
  </tr>
</table>
.ellipsis_cell > div {
    position: relative;
    overflow: hidden;
    height: 1em;
}

/* visible content */
.ellipsis_cell > div > span {
    display: block;
    position: absolute; 
    max-width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1em;
}

/* spacer content */
.ellipsis_cell > div:after {
    content: attr(title);
    overflow: hidden;
    height: 0;
    display: block;
}

http://jsfiddle.net/feesler/HQU5J/

问题在于“table-layout:fixed”,它会创建间距均匀、宽度固定的列。但是禁用这个css属性将杀死文本溢出,因为表将变得尽可能大(并且没有任何东西可以溢出)。

我很抱歉,但在这种情况下,弗雷德不能鱼与熊掌兼得。除非房东一开始就给塞尔迪托更少的工作空间,否则弗雷德就不能使用他的…

<table border="1" style="width: 100%;">
    <colgroup>
        <col width="100%" />
        <col width="0%" />
    </colgroup>
    <tr>
        <td style="white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:1px;">This cell has more content.This cell has more content.This cell has more content.This cell has more content.This cell has more content.This cell has more content.</td>
        <td style="white-space: nowrap;">Less content here.</td>
    </tr>
</table>

http://jsfiddle.net/7CURQ/