当您尝试将元素绝对定位到
顺便说一下:当您将
我的问题是: 为什么在
tbody
、tr
甚至 td
上时,您会发现在大多数浏览器中这是不起作用的。 它在 Firefox 中按预期工作,但在 IE、Edge 和 Chrome 中不起作用。
tbody
、tr
甚至 td
上的 position: relative
被忽略。 然后,第一个具有 position: relative
的父级被用作绝对定位的“锚点”。顺便说一下:当您将
tbody
设置为 display: block
时,position: relative
确实起作用。但是那时您可能会在表行的宽度上遇到麻烦。一般来说,子元素不再像表格元素一样精确地行为。列会消失……但这不是问题的一部分。我的问题是: 为什么在
tbody
、tr
和 td
上忽略了 position: relative
?
是否有任何原因造成这种行为?
这应该是需要修复的错误吗?.example {
border: 1px solid #ccc;
position: relative;
background: #eee;
margin: 2em;
padding: 2em;
width: 50%;
}
.abs {
display: block;
position: absolute;
left: 100%;
top: 0;
}
table {
//border: 5px solid rgba(255,200,0,0.2);
border-collapse: collapse;
}
tbody {
border: 2px solid red;
position: relative;
}
td {
border: 1px solid lime;
padding: 1em;
}
.text--red {
color: red;
}
.text--gray {
color: gray;
}
<ul>
<li class="text--gray">Gray background is table wrapper with position relative.</li>
<li class="text--red">Redline is tbody with position relative.</li>
</ul>
<div class="example">
<table>
<tbody>
<tr>
<td>tbody1>tr1>td</td>
</tr>
<tr>
<td>tbody1>tr2>td</td>
</tr>
<tr class="abs abs--1">
<td>tbody1>tr3>td absolute position to tbody</td>
</tr>
</tbody>
<tbody>
<tr>
<td>tbody2>tr1>td</td>
</tr>
</tbody>
<tbody>
<tr>
<td>tbody3>tr1>td</td>
</tr>
<tr class="abs abs--2">
<td>tbody3>tr2>td absolute position to tbody</td>
</tr>
</tbody>
</table>
</div>
来源:
https://www.w3.org/TR/css-position-3/#valdef-position-relative
https://www.w3.org/TR/css-position-3/#property-index
属性名:
position
适用于:除了table-column-group
和table-column
以外的所有元素。
https://developer.mozilla.org/en-US/docs/Web/CSS/position#relative 关于“堆叠上下文”的内容,但这不是本问题的主题。
当 z-index 的值不为 auto 时,此值 (
position: relative
) 会创建一个新的堆叠上下文。它对于table-*-group
、table-row
、table-column
、table-cell
和table-caption
元素的影响未定义。
tr
和td
也是一样的。因此,例如您无法将绝对定位的子DIV放置在其父TD中。因此,我编辑了我的问题,并将其扩展到tr
和td
。 - Matěj Křížtable-column-group
和table-column
。请参见:https://www.w3.org/TR/css-position-3/#property-index - Matěj Křížstreteched-link
中也无法工作:https://codesandbox.io/s/xenodochial-bas-t1zoi - pom421