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

<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)让出一点空间,他的左细胞就不会经常被截断。你能挽救他的理智吗?


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


当前回答

使用一些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/

其他回答

我相信我有一个非javascript的解决方案!我不想满足于JavaScript的修复,因为我发现页面加载后东西移动的轻微抖动是不可接受的。

特点:

没有JavaScript 没有固定布局 没有加权或百分比宽度技巧 适用于任意数量的列 简单的服务器端生成和客户端更新(不需要计算) 跨浏览器兼容的

How it works: Inside the table cell place two copies of the content in two different elements within a relatively-positioned container element. The spacer element is statically-positioned and as such will affect the width of the table cells. By allowing the contents of the spacer cell to wrap we can get the "best-fit" width of the table cells that we are looking for. This also allows us to use the absolutely-positioned element to restrict the width of the visible content to that of the relatively-positioned parent.

测试和工作在:IE8, IE9, IE10, Chrome, Firefox, Safari, Opera

结果图片:

JSFiddle: http://jsfiddle.net/zAeA2/

示例HTML / CSS:

<td>
    <!--Relative-positioned container-->
    <div class="container">
        <!--Visible-->
        <div class="content"><!--Content here--></div>
        <!--Hidden spacer-->
        <div class="spacer"><!--Content here--></div>
        <!--Keeps the container from collapsing without
            having to specify a height-->
        <span>&nbsp;</span>
     </div>
</td>

.container {
    position: relative;
}
.content {
    position: absolute;
    max-width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.spacer {
    height: 0;
    overflow: hidden;
}

使用一些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/

假定“表布局:固定”是基本的布局要求,这将创建均匀间隔的不可调整列,但您需要使单元格的百分比宽度不同,也许设置单元格的“colspan”为倍数?

例如,为了方便百分比计算,使用总宽度为100,并且假设您需要一个单元格为80%,另一个单元格为20%,考虑:

<TABLE width=100% style="table-layout:fixed;white-space:nowrap;overflow:hidden;">
     <tr>
          <td colspan=100>
               text across entire width of table
          </td>
     <tr>
          <td colspan=80>
               text in lefthand bigger cell
          </td>
          <td colspan=20>
               text in righthand smaller cell
          </td>
</TABLE>

当然,对于80%和20%的列,您可以将100%宽度的单元格colspan设置为5,80%宽度的单元格colspan设置为4,20%宽度的单元格colspan设置为1。

Yep I would say thirtydot has it, there is no way to do it unless you use a js method. You are talking about a complex set of rendering conditions that you will have to define. e.g. what happens when both cells are getting too big for their apartments you will have to decide who has priority or simply just give them a percentage of the area and if they are overfull they will both take up that area and only if one has whitespace will you stretch your legs in the other cell, either way there is no way to do it with css. Although there are some pretty funky things people do with css that I have not thought of. I really doubt you can do this though.

这个问题在谷歌的顶部弹出,所以在我的例子中,我使用了css代码片段 从https://css-tricks.com/snippets/css/truncate-string-with-ellipsis/,但应用到td没有给出预期的结果。

我不得不在td的文本周围添加一个div标签,省略号终于起作用了。

简写HTML代码;

<table style="width:100%">
 <tr>
    <td><div class='truncate'>Some Long Text Here</div></td>
 </tr>
</table>

缩写CSS;

.truncate { width: 300px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }