html - 无法让 z-index 在我的表中工作

标签 html css

我有一个 HTML 表格,其中有一个对象元素和一个 td 父标签内的 span 标签,如下所示:

HTML:

<table class="tableClass">
<tr>
<td class="tdClass"><object data="http://mydata.com"></object><span>My Text</span></td>
</tr>
</table>

现在表格的背景颜色为#999。基本上,使用 z-indexing,我希望元素隐藏在元素后面,但 span 元素应该位于 #999 表格背景之上。我如何实现这一目标?

我的 CSS:

.tableClass{
  background: #999;
}
.tdClass object{
  z-index:999;
}

.tdClass span{
  position: absolute;
  z-index: 1; (if I make it -1, it disappears behind the table background...)
}

目前带有文本的 span 元素位于对象元素的顶部...我希望它位于对象元素的后面和表格背景的前面。

谢谢

最佳答案

.tdClass object 至少需要 position:relative,因为 z-index 仅适用于 block 级元素,而 block 级元素不是位置:静态。尝试以下操作:

.tableClass{
  background: #999;
}
.tdClass object{
  position:relative;
  z-index:999;
}

.tdClass span{
  position: absolute;
  z-index: 1;
}

另见 CSS2.1: 9.9.1 Specifying the stack level: the 'z-index' property :

For a positioned box, the 'z-index' property specifies:

  1. The stack level of the box in the current stacking context.
  2. Whether the box establishes a stacking context.

关于html - 无法让 z-index 在我的表中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11742832/

相关文章:

html - 正则表达式匹配 HTML 中的属性?

html - 如何使两个跨度在 td 表内的同一行开始

jQuery div点击通过div并调用两个函数

html - 为图像从左到右运行直到消失制作动画

javascript - 更改 html 字符串宽度

jquery - 在圆圈内居中加/减切换元素

html - float :left insive li in Chrome

javascript - 获取所选文本相对于主 div 的范围(包含所有文本)

javascript - ASP.NET - 覆盖浏览器缓存的 Javascript 和样式表

javascript - 图像适合屏幕。如何在添加页眉和页脚时删除滚动条