弗雷德会面。他是一张桌子:
<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 table-layout: fixed的容器div。这个容器获取父表单元格的全部宽度,因此它甚至可以响应。
确保对表中的元素应用截断。
工作在IE8+
<table>
<tr>
<td>
<div class="truncate">
<h1 class="truncated">I'm getting truncated because I'm way too long to fit</h1>
</div>
</td>
<td class="some-width">
I'm just text
</td>
</tr>
</table>
和css:
.truncate {
display: table;
table-layout: fixed;
width: 100%;
}
h1.truncated {
overflow-x: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
这是一个工作提琴
https://jsfiddle.net/d0xhz8tb/
如果Javascript是可以接受的,我把一个快速例程放在一起,你可以作为一个起点。它动态地尝试使用跨度的内部宽度来适应单元格宽度,以响应窗口调整大小事件。
目前,它假设每个单元格通常获得行宽的50%,并且它将折叠右侧单元格以保持左侧单元格的最大宽度,以避免溢出。您可以实现更复杂的宽度平衡逻辑,这取决于您的用例。希望这能有所帮助:
我用于测试的行标记:
<tr class="row">
<td style="overflow: hidden; text-overflow: ellipsis">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
</td>
<td style="overflow: hidden; text-overflow: ellipsis">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
</td>
</tr>
JQuery,它连接了resize事件:
$(window).resize(function() {
$('.row').each(function() {
var row_width = $(this).width();
var cols = $(this).find('td');
var left = cols[0];
var lcell_width = $(left).width();
var lspan_width = $(left).find('span').width();
var right = cols[1];
var rcell_width = $(right).width();
var rspan_width = $(right).find('span').width();
if (lcell_width < lspan_width) {
$(left).width(row_width - rcell_width);
} else if (rcell_width > rspan_width) {
$(left).width(row_width / 2);
}
});
});
我相信我有一个非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> </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;
}