页面顶部表格设margin-top:0仍有间隙,无style标签如何修复?
解决表格顶部间隙问题(无需style标签)
问题根源
你碰到的顶部间隙主要来自三个点:
- 浏览器默认给
<body>设置了8px左右的margin - 使用
align属性的表格默认是基线对齐,会和顶部产生微小空隙 - 表格里的
<br>标签会额外占据行高,撑开顶部空间
具体修复步骤
- 消除body默认上边距
给<body>标签添加topmargin="0"属性,直接覆盖浏览器默认样式:
<body topmargin="0">
- 设置表格顶部对齐
给两个表格都加上valign="top"属性,解决基线对齐带来的间隙:
- 左侧表格修改后:
<table width="80%" align="left" cellspacing="0" valign="top"> <tr> <th bgcolor="purple" height="20"> </th> <th></th> <th></th> </tr> <tr></tr> <tr></tr> </table>
- 右侧表格修改后:
<table width="20%" cellspacing="0" align="right" valign="top"> <tr> <th bgcolor="#f3e12b" colspan="3" height="20"> </th> </tr> <tr></tr> <tr></tr> </table>
- 替换
<br>为height属性
把表头里的<br>换成height属性加 ,既保留表头高度,又不会产生换行带来的额外空间。
为什么之前的方法无效?
margin-top="0"不是表格的合法HTML属性,直接写在table标签里浏览器不会识别- style标签设置的样式可能被浏览器默认的表格对齐规则覆盖
cellspacing只控制单元格之间的间距,和顶部间隙无关
内容的提问来源于stack exchange,提问作者user25009771
相关产品推荐
相关产品推荐

