深度(z-index)噩梦

发布于 2024-10-16 15:16:21 字数 739 浏览 4 评论 0原文

说明这个问题的最好方法是用......小提琴! 之前< /strong> 您访问小提琴,请注意最灰色的元素后面有文本,该元素位于具有边框的浅灰色元素的顶部。

有一个主包装 div (root) 和两个内部包装 div (wrap1wrap2)。这里的问题是我需要将 wrap2 的内容(突出显示)放在 wrap1 的内容(文本)后面>),但在根的背景前面。

然而,这一点不能改变

  • HTML、元素和换行应保持不变。排除 rootwrap1wrap2 的顺序。

  • highlight div 必须保持绝对定位。

  • background-color设置highlight样式不是一个选项,highlight的存在是必须的。

PS:斜体引用了小提琴示例中

的id,对于那些懒得访问它的人来说。

The best way to illustrate this question is with...a Fiddle! Before you visit the fiddle, notice there is text behind the grayest element, which is on top of a light gray element that has a border.

There is a main wrapping div (root), and two wrapping divs inside (wrap1 and wrap2). The problem here is that I need the content of wrap2 (highlight) to be behind the content of wrap1 (text), but in front of the background of the root.

This, however, must not change:

  • The HTML, the elements and wraps should be left untouched. Excluding the order of wrap1 and wrap2 inside root.

  • The highlight div must keep the absolute positioning.

  • Styling highlight with background-color is not an option, the existence of highlight is a must.

PS: the italics reference the id's of <div>s in the fiddle example, for whomever was too lazy to visit it.

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

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

发布评论

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

评论(3

怪我入戏太深 2024-10-23 15:16:21

我能够通过 添加 z-index 来显示突出显示前面的文本文本。 (将 z-index 添加到 wrap1 也可以。)诀窍是记住 z-index 不适用于静态定位元素,因此您需要给出相同的 div position:relative

#text {
    position: relative;
    z-index: 1000;
}

z-index 较大,因为我过去曾被 IE 不尊重低值所困扰。可能仍然是问题,也可能不是问题。;-)

I was able to display the text in front of the highlight by adding a z-index to text. (Adding the z-index to wrap1 also works.) The trick is to remember that z-index doesn't apply to statically-positioned elements, so you need to give the same div position: relative.

#text {
    position: relative;
    z-index: 1000;
}

(Large z-index because I've been bitten by IE not respecting low values in the past. May or may not still be an issue. ;-)

轻拂→两袖风尘 2024-10-23 15:16:21

z-index 可能很难掌握。我认为有人已经回答了您的问题,但如果您想了解更多它们的工作原理,这是一个非常全面的指南:

http://www.onextrapixel.com/2009/05/29/an-indepth-coverage- on-css-layers-z-index-relative-and-absolute-positioning/

另外,这里有一个链接,您可以在其中尝试不同的 z-index 以及它们如何受到不同位置属性的影响(主要困难原因)

http://tjkdesign.com/articles/z-index/teach_yourself_how_elements_stack.asp

z-index can be difficult to grasp. I think somebody already answered your question, but if you want to learn more how they work, this is a pretty comprehensive guide:

http://www.onextrapixel.com/2009/05/29/an-indepth-coverage-on-css-layers-z-index-relative-and-absolute-positioning/

And also, here is a link where you can try out different z-index and how they are affected by different position properties (the main reason for difficulty)

http://tjkdesign.com/articles/z-index/teach_yourself_how_elements_stack.asp

飘逸的'云 2024-10-23 15:16:21
#wrap1{position:absolute;z-index:2;}
#wrap1{position:absolute;z-index:2;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文