IE7 Z-Index Layering Issues

后端 未结 11 768
执念已碎
执念已碎 2020-11-22 11:31

I\'ve isolated a little test case of IE7\'s z-index bug, but don\'t know how to fix it. I have been playing with z-index all day long.

Wha

相关标签:
11条回答
  • 2020-11-22 11:35

    Z-index is not an absolute measurement. It is possible for an element with z-index: 1000 to be behind an element with z-index: 1 - as long as the respective elements belong to different stacking contexts.

    When you specify z-index, you're specifying it relative to other elements in the same stacking context, and although the CSS spec's paragraph on Z-index says a new stacking context is only created for positioned content with a z-index other than auto (meaning your entire document should be a single stacking context), you did construct a positioned span: unfortunately IE7 interprets positioned content without z-index this as a new stacking context.

    In short, try adding this CSS:

    #envelope-1 {position:relative; z-index:1;}
    

    or redesign the document such that your spans don't have position:relative any longer:

    <html>
    <head>
        <title>Z-Index IE7 Test</title>
        <style type="text/css">
            ul {
                background-color: #f00; 
                z-index: 1000;
                position: absolute;
                width: 150px;
            }
        </style>
    </head>
    <body>
        <div>
            <label>Input #1:</label> <input><br>
            <ul><li>item<li>item<li>item<li>item</ul>
        </div>
    
        <div>
            <label>Input #2:</label> <input>
        </div>
    </body>
    </html>
    

    See http://www.brenelz.com/blog/2009/02/03/squish-the-internet-explorer-z-index-bug/ for a similar example of this bug. The reason giving a parent element (envelope-1 in your example) a higher z-index works is because then all children of envelope-1 (including the menu) will overlap all siblings of envelope-1 (specifically, envelope-2).

    Although z-index lets you explicitly define how things overlap, even without z-index the layering order is well defined. Finally, IE6 has an additional bug that causes selectboxes and iframes to float on top of everything else.

    0 讨论(0)
  • 2020-11-22 11:37

    If the previously mentioned higher z-indexing in parent nodes wont suit your needs, you can create alternative solution and target it to problematic browsers either by IE conditional comments or using the (more idealistic) feature detection provided by Modernizr.

    Quick (and obviously working) test for Modernizr:

    Modernizr.addTest('compliantzindex', function(){
        var test  = document.createElement('div'),
            fake = false,
            root = document.body || (function () {
                fake = true;
                return document.documentElement.appendChild(document.createElement('body'));
            }());
    
        root.appendChild(test);
        test.style.position = 'relative';
        var ret = (test.style.zIndex !== 0);
        root.removeChild(test);
    
        if (fake) {
            document.documentElement.removeChild(root);
        }
    
        return ret;
    });
    
    0 讨论(0)
  • 2020-11-22 11:38

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

    Try give the parent element a higher z-index value (can be even higher than the child’s z-index value itself) to fix the bug.

    0 讨论(0)
  • 2020-11-22 11:44

    I encountered this issue, but on a large project where HTML changes had to be requested and became a whole issue, so I was looking for a pure css solution.

    By placing position:relative; z-index:-1 on my main body content my header drop down content suddenly displayed above the body content in ie7 (it was already displaying without issue in all other browsers and in ie8+)

    The problem with that was then this disabled all hover and click actions on all content in the element with the z-index:-1 so i went to the parent element of the whole page and gave it a position:relative; z-index:1

    Which fixed the issue and retained the correct layering functionality.

    Feels a bit hacky, but worked as required.

    0 讨论(0)
  • 2020-11-22 11:50

    In IE6 in general, certain UI-elements are implemented with native controls. These controls are rendered in a completely separate phase (window?) and always appear above any other controls, regardless of z-index. Select-boxes are another such problematic control.

    The only way to work-around this issue is to construct content which IE renders as a seperate "window" - i.e. you can place a selectbox over a textbox, or, more usefully, an iframe.

    In short, you'll need to put "on-hover" like things such as menu's in an iframe in order to let IE place these above built-in UI controls.

    This should have been fixed in IE7 (see http://blogs.msdn.com/ie/archive/2006/01/17/514076.aspx) but perhaps you're running in some kind of compatibility mode?

    0 讨论(0)
  • 2020-11-22 11:54

    It looks like not a ie bug, just for diffrent understanding to the css standard. If outside container is not specified the z-index, but the inner element specified a higher z-index. So the container's sibling maybe overlay the high z-index element. Even if like that, it only occurs in IE7, but IE6, IE8 and Firefox is ok.

    0 讨论(0)
提交回复
热议问题