开发者

CSS z-index overridden by element order on page?

I've been battling with CSS z-index in IE7 foe a couple of hours now, maybe you can help!

I have a absolutely positioned div, appearing above its parent div, which is great. But - it appears under later divs that are siblings to its parent.

That seems like quite bizarre开发者_开发技巧 behavour, like the z-index only applies to the local scope of each div or something.

How can I set things up so that when I set an element to have a greater z-index than any other element on the page, it will appear on top regardless?

Here's my test page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>test</title>
</head>
<body>
<div style="position: relative; z-index: 0">
    div1
    <div style="position: relative; z-index: 0">
        div2
    </div>
    <div style="color: red; background-color: #ffffff; position: absolute; z-index: 2">
        div3
    </div>
</div>
<div style="position: relative; z-index: 0">
    div1
    <div style="position: relative; z-index: 0">
        div2
    </div>
    <div style="color: red; background-color: #ffffff; position: absolute; z-index: 2">
        div3
    </div>
</div>
<div style="position: relative; z-index: 0">
    div1
    <div style="position: relative; z-index: 0">
        div2
    </div>
    <div style="color: red; background-color: #ffffff; position: absolute; z-index: 2">
        div3
    </div>
</div>
</body>
</html>


IE has a major z-index bug:

In Internet Explorer positioned elements generate a new stacking context, starting with a z-index value of 0. Therefore z-index doesn't work correctly.

You could try to change z-indexes, but if you want both elements to appear, you might have to pull them out of their ancestor structure and make them top-level DOM elements.


An element with position:absolute will take the absolute position inside its container, especially if this container is set to position:relative.

All position:absolute elements that need to be positioned according to the viewport should come in the DOM directly under the "body" element. It is usually considered good practice to put them last in your html code, just before the closing body tag.


Just remove position: relative


Can an image have a style='z-index: #' or must the image reside in a div of it's own?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜