Skip to content

Z-索引不 工作

快速摘要

你的 z-索引值 是不 工作因为元素需要位置值 other than 静态 (默认).

为什么发生

  • 元素有 位置: 静态 (默认), 哪个 ignores z-索引

  • parent 元素创建 stacking 上下文那个限制 child's z-索引范围

  • Competing z-索引值 on sibling 元素是 不设置正确地

最小示例

✗ 错误代码
.modal {
  z-index: 999;
  /* position is static by default */
}
✓ 修复代码
.modal {
  position: fixed;
  z-index: 999;
}

z-索引仅 工作 on positioned 元素. 添加位置: fixed (or 相对/绝对/sticky) 制作 z-索引 take 副作用.

如何诊断

  • 检查元素's 位置 in DevTools — 是它 静态?

  • Inspect parent 元素 for stacking 上下文 (transform, opacity, 将-更改)

  • Compare z-索引值 的 sibling 元素

如何修复

  • 设置位置: 相对, 绝对, fixed, or sticky on 元素

  • Increase z-索引值 above competing 元素

  • 移除 or adjust parent stacking 上下文如果它们限制 child

如何预防

  • 总是设置位置值 当 using z-索引

  • Use consistent z-索引 scale (e.g., 10, 20, 30, 100)

相关资源

相关术语

相关课程

  • CSS

    学习 CSS positioning.

← 返回语言