固定元素内嵌套绝对元素的 z 索引

发布于 2024-12-08 00:25:01 字数 1044 浏览 0 评论 0原文

我正在为这个问题摸不着头脑。我希望绿色矩形位于黄色顶栏下方。

http://jsfiddle.net/PY9ss/1/

现在的样子

HTML:

<div class="body">
    <div class="topbar">
        <div class="mid">
            <div class="attention"></div>
        </div>
    </div>
</div>

CSS:

.body { background: gray; width: 100%; height: 800px;}
.topbar { background: yellow; width: 100%; height: 50px; position: fixed; top:0; left:0; z-index: 10; }
.mid { background: red; width: 400px; height: 40px; margin: 10px auto 0; position: relative; }
.attention { background: green; width: 100px; height: 40px; border: 1px solid black; position: absolute; top: 30px; left: 0; }

编辑: 这是通过使用 z-index:-1 来修复的;关于 .attention 的回答。然而,我的实际问题是,如果有人遇到同样的问题,我有一个透明的红色背景,这愚弄了我。

我的实际问题

I'm scratching my head over this one. I would like for the green rectangle to be underneath the yellow topbar.

http://jsfiddle.net/PY9ss/1/

what it looks like now

HTML:

<div class="body">
    <div class="topbar">
        <div class="mid">
            <div class="attention"></div>
        </div>
    </div>
</div>

CSS:

.body { background: gray; width: 100%; height: 800px;}
.topbar { background: yellow; width: 100%; height: 50px; position: fixed; top:0; left:0; z-index: 10; }
.mid { background: red; width: 400px; height: 40px; margin: 10px auto 0; position: relative; }
.attention { background: green; width: 100px; height: 40px; border: 1px solid black; position: absolute; top: 30px; left: 0; }

EDIT:
This is fixed by using z-index:-1; on .attention as answered. However, my actual problem was that I had a transparent red background which fooled me, if anyone runs into the same problem.

my actual problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2024-12-15 00:25:01

只需将其 z-index 设置为 -1 即可。

这是小提琴: http://jsfiddle.net/PY9ss/2/

Just give it a z-index of -1.

Here's the fiddle: http://jsfiddle.net/PY9ss/2/

征棹 2024-12-15 00:25:01

.attention { z-index: -1 } 是否符合您的要求?

http://jsfiddle.net/thirtydot/PY9ss/3/

Does .attention { z-index: -1 } do what you want?

http://jsfiddle.net/thirtydot/PY9ss/3/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文