You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决CSS容器查询容器中z-index元素被覆盖问题?

CSS容器查询导致绝对定位元素被后续容器覆盖的问题

我一直很期待CSS容器查询功能,如今该功能已在所有浏览器中落地。但当我创建多个连续的container-type: inline-size容器时,其中一个容器内带z-index的绝对定位元素会被后续的同类型容器覆盖。

问题场景

  • 菜单(position: absolute,z-index: 9)属于设置了container-type: inline-size的深蓝色容器
  • 但被同样设置了container-type: inline-size的浅蓝色容器覆盖

隔离复现代码

CSS代码

.block {
  display: inline-block;
  background-color: darkblue;
  color: lightblue;
  margin: 2px;
  width: 100%;
}

.block.container {
  container-type: inline-size;
}

.popout {
  background-color: darkgreen;
  color: lightgreen;
  position: absolute;
  z-index: 9;
}

hr {
  margin-bottom: 4rem;
}

HTML代码

<div class="block">
  <p>Some content in regular inline-block</p>
  <div class="popout">
    <p>Popping out of it.</p>
    <p>Popping out of it.</p>
    <p>Popping out of it.</p>
  </div>
</div>
<div class="block">
  <p>Some content in regular inline-block</p>
</div>
<hr />
<div class="block container">
  <p>Some content in block with
    <pre>container-type: inline-size;</pre>
  </p>
  <div class="popout">
    <p>Popping out of it.</p>
    <p>Popping out of it.</p>
    <p>Popping out of it.</p>
  </div>
</div>
<div class="block container">
  <p>Some content in block with
    <pre>container-type: inline-size;</pre>
  </p>
</div>

我想知道如何在应用container-type时不引入新的层叠上下文。我曾尝试给所有container-type: inline-size的容器设置低z-index,虽有效果但不想在全页面设置z-index。


内容的提问来源于stack exchange,提问作者Ad van Pinxteren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 00:05:25