【问题标题】:Styling a table layout fixed & fixed width columns + colspan样式化表格布局固定和固定宽度的列+ colspan
【发布时间】:2016-10-26 06:30:23
【问题描述】:

我有以下布局。

<style>
   table { width: 200px; white-space: nowrap; table-layout: fixed; }
   .a { width:10px; overflow: hidden; text-overflow: ellipsis }
   .b { width:190px; overflow: hidden; text-overflow: ellipsis }
</style>

<table border="1" style="">
    <tr>         
        <td colspan="2">Some content header goes in here</td>
    </tr>
    <tr>
        <td class="a">This cells has more content</td>
        <td class="b">Less content here</td>
    </tr>
</table>

我正在尝试设置固定表格布局并在每列上具有固定宽度,但是,由于我在固定列上方有colspan,因此我无法设置宽度。我怎样才能做到这一点?

【问题讨论】:

    标签: css html-table


    【解决方案1】:

    您可以在打开的&lt;table&gt; 标记之后添加几个col 标记并设置它们的宽度:

    <style>
       table { width: 200px; white-space: nowrap; table-layout: fixed; }
       .a { overflow: hidden; text-overflow: ellipsis }
       .b { overflow: hidden; text-overflow: ellipsis }
       .cola { width: 10px; }
       .colb { width: 190px; }
    </style>
    
    <table border="1" style="">
        <col class="cola" />
        <col class="colb" />
        <tr>         
            <td colspan="2">Some content header goes in here</td>
        </tr>
        <tr>
            <td class="a">This cells has more content</td>
            <td class="b">Less content here</td>
        </tr>
    </table>
    

    【讨论】:

    • col 真的应该关闭
    • 取决于您的 DOCTYPE。它是完全有效的 HTML5,这就是我这些天要做的。
    • hmm... 出于某种原因,我认为它们应该在 XHTML 中自封闭,而在 HTML 中 可选 自封闭(即不要用 &lt;/col&gt; 封闭) - 但是stackoverflow.com/questions/3008593/… 不一样。 &lt;/tr&gt;&lt;/td&gt; 是可选的!
    • 无论如何要使用更少的 HTML 标记来做到这一点?我正在尝试在现有站点中实现此功能,因此必须返回数百页并插入 &lt;col&gt; 标签会是有问题的
    • &lt;col&gt;&lt;colgroup&gt; 在 HTML4 和 HTML5 中仍然有效,但 &lt;col&gt; 上的许多属性(例如,&lt;col align=""&gt;)在 HTML4 中已弃用,在 HTML5 中已过时。不过,上面的语法在 HTML4 和 HTML5 中是有效的。见MDNHTML5 specHTML 4.01 spec
    猜你喜欢
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2014-05-22
    • 2014-12-22
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 2012-03-26
    相关资源
    最近更新 更多