HTML表格跨列设置异常:无法将RAL、Ore单元格置于Media下方
问题根因
你的表格单元格错位由属性拼写错误+冗余单元格共同导致:
- HTML中实现单元格跨多行的标准属性是
rowspan,你错写为不存在的colsrow,第一行的黄色空表头、「Distribuzione」表头本应跨2行高度,属性失效后浏览器默认两个单元格仅占1行高度 - 第二行表头多写了两个无内容的空
<th>标签,还加了无效的colspan="0"属性,直接把后续的「RAL」「Ore」单元格挤到了错误位置 - 补充:普通单个单元格默认
colspan="1",不需要额外写该属性,写了也不会产生实际效果
修正后可直接运行的代码
<table border="1" cellspacing="5" cellpadding="5" width="30%"> <caption> Tabelle dipendenti </caption> <thead> <tr> <th rowspan="2" style="background-color:yellow;"></th> <th rowspan="2">Distribuzione</th> <th colspan="2">Media</th> </tr> <tr> <th>RAL</th> <th style="background-color:red;">Ore</th> </tr> <tr> <th style="background-color:yellow;">Manager</th> <th>2%</th> <td>100.000 €</td> <td style="background-color:red;">-</td> </tr> <tr> <th style="background-color:yellow">Impiegati</th> <th>20%</th> <td>40.000€</td> <td rowspan="2" style="background-color:red;">40</td> </tr> <tr> <th>Operai</th> <th>78%</th> <td>35.000€</td> </tr> </thead> </table>
效果说明
- 第一行前两个表头正确设置
rowspan="2"后,会自动占据第一、第二行的前两列位置,第二行不需要再为这两列添加单元格 - 删除第二行冗余的空单元格后,「RAL」「Ore」会自动对齐到上一行跨2列的「Media」单元格正下方,和预期效果完全一致
- 清理了所有无效冗余属性,代码逻辑更清晰
内容的提问来源于stack exchange,提问作者andreabianchi8
相关产品推荐
相关产品推荐

