Html HTML5 - 如何为特定单元格指定固定宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15284606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML5 - How can I specify a fixed width for a particular cell?
提问by Hosea146
In HTML5, how can I specify a fixed width for a table cell? Most of the <col>
properties which were available in HTML4 are no longer valid in HTML5.
在 HTML5 中,如何为表格单元格指定固定宽度?<col>
HTML4 中可用的大多数属性在 HTML5 中不再有效。
Just to be clear, I know I can do <td style="width:150px">
, but this does not keep the column width fixed. No matter what the contents of the cell are, I want the width fixed at 150px.
为了清楚起见,我知道我可以做到<td style="width:150px">
,但这并不能保持列宽固定。无论单元格的内容是什么,我都希望宽度固定为 150px。
采纳答案by Explosion Pills
You can use width
on col
. Depending upon what you are doing it may also be helpful to use a fixed width child of the <td>
to enforce its width as this does not work by setting the width of <td>
alone due to its table-cell
display type.
您可以width
在col
. 根据您在做什么,使用 的固定宽度子项<td>
来强制其宽度也可能会有所帮助,<td>
因为由于其table-cell
显示类型,这不能通过单独设置宽度来工作。
回答by Blair Anderson
Use colgroup
用 colgroup
<table>
<colgroup>
<col span="1" style="width: 15%;">
<col span="1" style="width: 70%;">
<col span="1" style="width: 15%;">
</colgroup>
<thead></thead>
<tbody></tbody>
</table>
回答by FelipeAls
Use the othertable algorithm by adding table-layout: fixed
:
通过添加使用其他表算法table-layout: fixed
:
- How to set the table column width constant regardless of the amount of text in its cells?
- previous answer of mine with demo(that was for
div
displayed as table but that's the same astable
element displayed as default)
- 无论单元格中的文本数量如何,如何将表格列宽设置为常量?
- 我之前用演示的答案(用于
div
显示为表格,但table
与默认显示的元素相同)