我是一位有用的助手,可以翻译文本。
我在尝试将div元素绝对定位到table-cell内部时遇到了奇怪的行为。为实现绝对定位,我使用一个带有position:relative的包装器div元素:
HTML
CSS (层叠样式表)
我在尝试将div元素绝对定位到table-cell内部时遇到了奇怪的行为。为实现绝对定位,我使用一个带有position:relative的包装器div元素:
HTML
<table>
<colgroup>
<col width="200px"></col>
<col width="300px"></col>
</colgroup>
<thead>
<tr>
<th>Caption 1</th>
<th>Caption 2</th>
</tr>
</thead>
<tbody>
<tr>
<td><div class="wrapper"><div class="abs">abs</div></div></td>
<td>Content 2</td>
</tr>
</tbody>
</table>
CSS (层叠样式表)
table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}
th, td {
border-bottom: 1px solid black;
padding: 0;
margin: 0;
}
.wrapper {
background-color: green;
position: relative;
width: 100%;
border-top: 1px solid blue;
}
.abs {
position: absolute;
margin: 0px;
padding: 0px;
background-color: red;
}
正如您所看到的,包装 div 与包含 table-cell 的顶部之间存在间隙。 如果我将 abs
元素更改为 position:relative
,则该间隙将消失。
那么这个间隙是从哪里来的,我该如何防止它出现?
vertical-align: top;
,我可能会再问另一个问题。到目前为止,我还没有做到这一点。 - basilikum