position: absolute 在 中不生效,因 table-cell 不生成定位上下文;须在 内嵌套 position: relative 的 作为定位容器,并设 height: 100% 防塌陷。

表格单元格(<td> 或 <code><th>)默认是 <code>display: table-cell,而 position: absolute 在 table-cell 上**不生效**——这是最常被忽略的根本限制。
为什么 position: absolute 在 <td> 里不起作用
<p>浏览器规范规定:<code>table-cell 元素不能作为绝对定位的“包含块”(containing block),即使你给 <td> 加了 <code>position: relative,它也不会创建定位上下文。实际渲染中,absolute 子元素会向上回溯,跳过整个表格结构,最终相对于最近的非 static 祖先(比如 或某个外层 <div>)定位。
<ul><li>现象:你在 <code><td> 里写 <code>position: relative + 内部 div 设为 position: absolute,结果该 div 跑到页面左上角或意外偏移
position: relative 对它无效(不触发包含块生成)<td> 的 computed <code>position,会显示为 static,哪怕你写了 relative
正确做法:用 div 包裹内容并设为定位容器
必须在 <td> 内部插入一个块级容器(如 <code><div>),再对这个 <code>div 应用 position: relative,才能让它成为绝对定位的参考父级。
- HTML 结构必须是:
<td><div class="pos-container"><div class="abs-el">...</div></div></td> - CSS 中:
.pos-container { position: relative; },.abs-el { position: absolute; top: 0; right: 0; } - 注意:该
<div> 需要显式设置 <code>height: 100%或min-height: 100%,否则高度塌陷,bottom/top计算可能失准 - 如果
<td> 有 padding,<code>absolute元素的top: 0是相对于div内容区起始,不是<td> 边框 <h3> <code>z-index和层级覆盖要注意什么表格行(
<tr>)和单元格(<code><td>)本身有隐式堆叠上下文,直接在 <code><td> 上设 <code>z-index无效;只有你创建的那个position: relative的<div> 才能承载 <code>z-index。- 若想让绝对元素盖住相邻单元格,需确保其父
<div> 的 <code>z-index高于其他同类容器 - 避免在
<tr> 或 <code><table> 上盲目加 <code>z-index—— 它们不是定位元素,不会创建新层叠上下文 - 常见错误:多个
absolute元素互相遮挡,其实是它们的共同父div没设position: relative,导致都相对于 body 定位,彼此无层级关系
真正起作用的永远是你亲手写的那个
position: relative的<div>,而不是 <code><td> 标签本身。表格结构天生排斥绝对定位,绕不过去,只能包裹。</td> - 若想让绝对元素盖住相邻单元格,需确保其父











