样式表格式固定和固定宽度列+ colspan

Kay*_*yla 9 css html-table

我有以下布局.

<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>
Run Code Online (Sandbox Code Playgroud)

我试图设置一个固定的表格布局,并在每列上有一个固定的宽度,但是,因为我已经colspan在固定列之上,所以我无法设置宽度.我怎样才能做到这一点?

Jon*_* S. 19

您可以col在开始<table>标记之后添加几个标记,并在其上设置宽度:

<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>
Run Code Online (Sandbox Code Playgroud)

  • `<col>`和`<colgroup>`在HTML4和HTML5中仍然有效,但`<col>`上的许多属性(例如,`<col align ="">`)在HTML4中被弃用并制作在HTML5中过时了.但是,上面的语法在HTML4和HTML5中是有效的.见[MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/col),[HTML5规范](http://www.w3.org/TR/html5/tabular -data.html#the-col-element),[HTML 4.01 spec](http://www.w3.org/TR/html401/struct/tables.html#edef-COL). (2认同)