问题 你如何使表格单元显示尽可能多的文本,如GMail和谷歌阅读器?


制作表格的最佳方法是什么(不一定使用表格标签),其中每行具有固定高度并填充整个可用水平空间,其中一列具有动态宽度,尽可能多地显示文本而不会断行?像Gmail和Google Reader一样。

我非常喜欢这种呈现信息的方式。可扩展的固定高度行是扫描数据列表的好方法,恕我直言。


11277
2018-05-13 21:03


起源



答案:


看到:  http://jsfiddle.net/gtRnn/

<p>What is the best way *snip*</p>

p {
    border: 1px dashed #666;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis
}

每个属性都在做有用的事情:

  • white-space: nowrap 强制文本保持在一行。
  • overflow: hidden 停止 这个
  • text-overflow: ellipsis 适用于“所有浏览器”,  Firefox(支持计划
  • border 是没有理由的。

9
2018-05-13 21:09



+1为 text-overflow,虽然不是100%浏览器兼容。 - Dustin Laine
谢谢你的简单回答! - Gurgeh


答案:


看到:  http://jsfiddle.net/gtRnn/

<p>What is the best way *snip*</p>

p {
    border: 1px dashed #666;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis
}

每个属性都在做有用的事情:

  • white-space: nowrap 强制文本保持在一行。
  • overflow: hidden 停止 这个
  • text-overflow: ellipsis 适用于“所有浏览器”,  Firefox(支持计划
  • border 是没有理由的。

9
2018-05-13 21:09



+1为 text-overflow,虽然不是100%浏览器兼容。 - Dustin Laine
谢谢你的简单回答! - Gurgeh


其他答案在表格中不起作用,如下所示: http://jsfiddle.net/gtRnn/8/  - 链接还包含正确的方法。

解决方案是设置 表格的布局 桌子上的风格 固定 并为每列提供百分比或绝对宽度。 table-layout:fixed; 告诉浏览器不要根据内容计算表的宽度,而是根据给每个单元格的显式宽度计算表的宽度。

工作实例: http://jsfiddle.net/gtRnn/8/

码:

<style type="text/css">
    .my-table {
        /* This is important for ellipsis to work in tables.
           Don't forget to explicitly set the widths on all columns. */
        table-layout: fixed;
        width: 100%;              /* The table must be given a width. */
    }

    .overflow-ellipsis {
        overflow: hidden;        /* Ellipsis won't work without this. */
        text-overflow: ellipsis; /* The most important part */
    }
</style>

<table border="1" class="my-table">
    <tr>
        <td width="10%">1.</td>
        <td width="90%" class="overflow-ellipsis">
            In this TD, wrapping still occurs if there is white space.
            ButIfThereIsAReallyLongWordOrStringOfCharactersThenThisWillNotWrapButUseEllipsis
        </td>
    </tr>
    <tr>
        <td width="10%">2.</td>
        <td width="90%" class="overflow-ellipsis" style="white-space:nowrap;">
            In this TD, wrapping will not occur, even if there is white space.
        </td>
    </tr>
</table>

混合百分比宽度和绝对宽度不能正常工作(至少在谷歌浏览器中)。


4
2017-12-09 21:55



天才!谢谢! - Michael


关键是 overflow: hidden; 和 white-space: nowrap;

<div style="width: 200px; overflow: hidden; white-space: nowrap;">some long text.............asdfasdf........</div>

2
2018-05-13 21:06