简体   繁体   English

固定宽度表格单元格

[英]Fixed width table cell

I'm trying to make cells have a fixed (in px) width within a table. 我正在尝试使单元格在表格中具有固定(以px为单位)的宽度。

This is what I have now: 这就是我现在拥有的:

        <table style="border: 1px solid black; width: 1128px; table-layout: fixed;">
            <tbody>
              <tr>
                <td style="background: none repeat scroll 0 0 red; width: 4px;">Row 1 Col 1</td>
                <td style="background: none repeat scroll 0 0 red; width: 4px;">Row 1 Col 2</td>
                <td style="background: none repeat scroll 0 0 red; width: 4px;">Row 1 Col 3</td>
              </tr>
            </tbody>
        </table>

However, none of the table-cells are 4px wide. 但是,没有一个表格单元是4px宽。 What am I missing? 我错过了什么?

You can add a 4th <td> tag, with no width specified: 您可以添加第4个<td>标记,但不指定宽度:

<table style="border: 1px solid black; width: 1128px; table-layout: fixed;">
    <tbody>
        <tr>
            <td style="background: none repeat scroll 0 0 red; width: 4px;">Row 1 Col 1</td>
            <td style="background: none repeat scroll 0 0 red; width: 4px;">Row 1 Col 2</td>
            <td style="background: none repeat scroll 0 0 red; width: 4px;">Row 1 Col 3</td>
            <td></td>
        </tr>
    </tbody>
</table>

See the result here . 这里查看结果。 The width of the first three cells is 4px, and the 4th, empty cell fills up the remaining width of the table. 前三个单元格的宽度为4px,第四个空单元格的宽度填满了表格的剩余宽度。

Well, you can add buffer cells... 好吧,你可以添加缓冲细胞......

Example: http://jsfiddle.net/YGRGG/ 示例: http//jsfiddle.net/YGRGG/

HTML HTML

<table>
    <tbody>
        <tr>
            <td></td>
            <td>Row 1 Col 1</td>
            <td></td>
            <td>Row 1 Col 2</td>
            <td></td>
            <td>Row 1 Col 3</td>
            <td></td>
        </tr>
    </tbody>
</table>

CSS CSS

table {
    border: 1px solid black;
    width: 1128px;
}
table td:nth-of-type(2),
table td:nth-of-type(4),
table td:nth-of-type(6) {
    width:4px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM