具有相同大小列的100% 宽度的表

我必须动态地创建一个具有可变数量的列的表,这些列是在运行时确定的。

有没有人能告诉我,是否有可能有一个具有相同大小的柱子的 html 表,这些柱子是完全拉伸的?

221313 次浏览
<table width="400px">
<tr>
<td width="100px"></td>
<td width="100px"></td>
<td width="100px"></td>
<td width="100px"></td>
</tr>
</table>

For variable number of columns use %

<table width="100%">
<tr>
<td width="(100/x)%"></td>
</tr>
</table>

where 'x' is number of columns

ALL YOU HAVE TO DO:

HTML:

<table id="my-table"><tr>
<td> CELL 1 With a lot of text in it</td>
<td> CELL 2 </td>
<td> CELL 3 </td>
<td> CELL 4 With a lot of text in it </td>
<td> CELL 5 </td>
</tr></table>

CSS:

#my-table{width:100%;} /*or whatever width you want*/
#my-table td{width:2000px;} /*something big*/

if you have th you need to set it too like this:

#my-table th{width:2000px;}

If you don't know how many columns you are going to have, the declaration

table-layout: fixed

along with not setting any column widths, would imply that browsers divide the total width evenly - no matter what.

That can also be the problem with this approach, if you use this, you should also consider how overflow is to be handled.

table {
width: 100%;


th, td {
width: 1%;
}
}

SCSS syntax

Just add style="table-layout: fixed ; width: 100%;" inside <table> tag and also if you do not specify any styles and add just style=" width: 100%;" inside <table> You will be able to resolve it.