Squish the Internet Explorer Z-Index Bug

 

The other day when working on my latest website; I had everything working fine until I decided to check how things looked in IE. Usually IE7 plays pretty well, but in this instance there was a pretty crucial problem. I had an absolutely positioned div (hover menu) and under it I had a transparent PNG that was also absolutely positioned as well.

IE Z-Index Bug

The absolutely positioned div had a z-index of 1000, but @jorenrapini pointed out that IE doesn’t use z-index properly. I came across this quirksmode.org article that explained the flaw in detail:

“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”

The above article does not contain a workaround but in the comments a fellow mentioned that giving the parent element a higher z-index actual fixes the bug. Don’t ask me why this works but it does!

<div style="z-index: 3000">
    <div style="position:absolute;z-index:1000;">
        <a href="#">Page</a>
        ...
    </div>
</div>
<img style="position:absolute" src="myimage.png" />

Fix Z-Index Bug in IE

If you liked this article please keep updated by subscribing to my RSS feed, and following me on twitter (@brenelz)

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Add to favorites
  • Design Float
  • DZone
  • email
  • FriendFeed
  • PDF
  • Propeller
  • Reddit
  • RSS
  • StumbleUpon
  • Twitter

Related posts:

  1. Internet Explorer Bugs
  2. CSS Line Break Bug (screencast)
  3. Fix table-cell and max-width bug in IE8
  4. CSS Rounded Corners
  5. Firefox Saved Passwords… handy, but insecure?


Written by Brenley Dueck

 

106 Responses to “Squish the Internet Explorer Z-Index Bug”

  1. Hassan Says:

    February 4th, 2009 at 2:33 am

    Nice trick! Thanks!

  2. Brian Says:

    February 5th, 2009 at 4:21 am

    Good tip, bound to come across it sometime as I have started to use absolute positioned divs now.

  3. SteveG Says:

    February 6th, 2009 at 10:59 am

    I have run into the same problem. My dropdown nav divs would appear behind the main promo div in IE 7 and IE 6. I couldn’t find a solution online at the time. I experimented and ended up using a z-index of -1 on the promo div for IE 7 and lower. That seems to work fine, but I couldn’t tell you why. I’ll try your solution next time.

  4. shin Says:

    February 9th, 2009 at 12:06 pm

    I have the same problem and I almost gave up to fix it. Thanks for 1000 times. I appreciate it and you made my day.

  5. Sean Says:

    February 9th, 2009 at 12:31 pm

    Thanks for the trick!

  6. Josh Says:

    February 9th, 2009 at 12:37 pm

    Wow, great tip! Thanks.

  7. jitendra Says:

    February 12th, 2009 at 8:01 am

    how to solve this problem for internet explorer http://jitendravyas.com/ie6.html

  8. John Says:

    February 14th, 2009 at 9:24 pm

    Thanks for the fast tip.
    I spent around 8 hours trying to fix this thing.
    Although I was trying to do it with a Javascript dynamic iframe and now I’ll do it with a div and with an AJAX call. Thanks to you.
    Good and fast demo.
    Thanks again.

  9. Andrew Says:

    February 16th, 2009 at 2:22 pm

    Thanks for the tip! Saved me a ton of grief.

  10. Joe Says:

    March 13th, 2009 at 7:11 am

    Great! Thanks, it works.

  11. Carl Says:

    March 17th, 2009 at 3:48 pm

    But this will not work in any other browser, since the content in the child div will then be (correctly) positioned behind the contents in the parent div!

  12. Bob Says:

    March 26th, 2009 at 7:14 pm

    Absolutely fantastic. Worked perfectly after trying other hacks with zero success. Many thanks!

  13. David Says:

    April 2nd, 2009 at 6:25 pm

    This post made my day! My divs were all showing up fine in FF and Safari, but disappearing behind the parent div in IE. Thanks for posting and sharing.

  14. Keith Says:

    April 5th, 2009 at 2:50 pm

    The trick works because IE doesn’t allow elements to have a higher z-index than their parent element. It’s actually a reasonable restriction, if somewhat unneccessary (like Mozilla’s insistence that opacity always inherit).

    You can sometimes get around this by taking advantage of the fact that absolutely positioned elements don’t care about their parents (though this can get inconvenient if you’re unfamiliar with your DOM in JS). Simply find a good element to position as relative, then set up independent elements and use the absolute positioning/z-index to put them where you want them.

  15. Nigel Says:

    April 13th, 2009 at 1:54 pm

    Wow – brilliant! You’ve turned my day around from seemingly unending frustration to a successful and happy one. Thank you very much indeed for sharing this neat trick.

  16. Scott Says:

    April 29th, 2009 at 8:35 pm

    Thanks. I also noticed that if you have nested absolutely positioned elements this fix won’t work unless you put the higher z-index on the top most relatively positioned element. In other words don’t just put the higher z-index on the parent element, put it on the first parent that is positioned relatively.

  17. Matthew Howland Says:

    May 5th, 2009 at 7:44 pm

    Thanks for the advice. I had the same problem. It was not just as easy as changing a couple of the indexes, so I wrote a javascript to reindex the divs. All of my stuff was in divs, so it worked. See the code below:

    function reIndex(){
    forms = document.forms
    zi = 2000;
    cn = document.getElementsByTagName(‘div’);
    for (n=0;n<cn.length;n++){
    cn[n].style.zIndex=zi–
    }
    }

    Thought this may help someone else.

    Matt

  18. Matthew Howland Says:

    May 5th, 2009 at 7:46 pm

    I forgot to mention, you call this after everything has already loaded.

  19. Bjorn Thomson Says:

    May 15th, 2009 at 5:18 pm

    Thank you! Hours of work dropped to a couple of minutes of tweaking…

  20. tison Says:

    May 22nd, 2009 at 2:37 pm

    Unfortunately both my divs were children of the same element, so luckily:

    @SteveG – thank you! Giving the second element a z-index:-1 worked :) ,

    Honestly, can’t calculate the time i’ve wasted on this browser…

  21. Z-index Internet Explorer IE6 | breakthrough design Says:

    May 22nd, 2009 at 2:45 pm

    [...] Give the element (that should be behind) a z-index:-1 [...]

  22. Rob Says:

    June 2nd, 2009 at 2:41 pm

    Really neat trick, cheers!

  23. Andrew Champ Says:

    June 8th, 2009 at 3:17 pm

    THANK YOU SO MUCH! SPENT 2 DAYS (16 HRS) TRYING TO FIX, THIS DID THE TRICK!

    .andrew champ

  24. avramir Says:

    June 19th, 2009 at 7:38 am

    man, thank you so much!!!

  25. Rival Says:

    June 25th, 2009 at 5:53 am

    Wow, thanks a lot!

  26. Barbara Says:

    June 28th, 2009 at 6:02 pm

    Thank you for this fix, this one worked for me! I have included it in my binder of potential workarounds for the IE z-index bug:
    http://www.livebinders.com/play/play?id=1808

  27. Alaa Al-Hussein Says:

    July 6th, 2009 at 5:05 am

    I had the same problem.. I added the z-index property to css of the main div and it work.

    I am waiting for the day to stop working on IEs

    Thanks.

  28. Tyron M. Says:

    July 22nd, 2009 at 5:07 am

    I had the same problem. A menu that opens up and was hidden behind the main content window.

    But adjusting z-index on any element did not help. I got it fixed however, my problem was that my parent element had overflow:hidden;
    Watch out for that ;)

  29. Joe Says:

    July 22nd, 2009 at 10:49 am

    THANK YOU!! life saver. :)

  30. Benny Says:

    July 24th, 2009 at 8:52 am

    Thanks – this saved me a lot of time!

  31. Jeff Says:

    July 24th, 2009 at 5:26 pm

    Thanks for the simple fix.

  32. Priyank Says:

    July 25th, 2009 at 1:23 am

    ThanKs!!

  33. Ryan Says:

    July 31st, 2009 at 4:41 pm

    Thanks for the tip. I had to reconfigure my code just a touch to match your example, but it worked great.

  34. Luke Says:

    August 2nd, 2009 at 7:10 pm

    after hours on end searching for a solution…

    THANK YOU!!

    may IE die a slow and painfull death

  35. Joren Rapini Says:

    August 7th, 2009 at 4:00 pm

    Just ran into this problem again, as usual. What Scott wrote earlier is correct – this only works if you are applying it to relative positioned elements – doing it on absolute positioned elements does not help! Stinking IE.

  36. " IE7 et z-index" - CoolCoyote.net Says:

    August 12th, 2009 at 9:01 am

    [...] j’ai trouvé la solution sur ce blog. J’ai résolu le problème en définissant un z-index le plus élevé sur #header. Ce qui [...]

  37. DaCoder Says:

    August 18th, 2009 at 4:59 pm

    Saved my bu++

    Thanks

  38. Andrew Says:

    August 19th, 2009 at 8:26 am

    Thanks for this… sorted out my issue in an instant!

  39. Glenn Says:

    August 20th, 2009 at 1:39 pm

    Thanks you so much.
    Helped me on my Crewe United Football Club drop down menu.

  40. pradnya Says:

    September 3rd, 2009 at 5:14 am

    thank you very much….
    It’s help me lots…

  41. Arly Says:

    September 5th, 2009 at 7:47 am

    really this helps me a lot after 2 hours crazy-ing around with IE.

    THANKS!

  42. johnmark Says:

    September 7th, 2009 at 8:06 am

    Why the hell doesnt microsoft IE conform to CSS standards like all other browsers?
    This goes to show that opensource is the way to go…
    Long live firefox…

  43. Ben Says:

    September 9th, 2009 at 2:44 am

    I’ve been searching for this solution for such a long time.

    Thank you very much!

  44. ian Says:

    September 12th, 2009 at 8:47 pm

    Life saver, thankfully you were the first hit on Google.
    You just got me out of a jam, thanks again

  45. Sarah Says:

    September 18th, 2009 at 5:58 am

    Thanks very much…perfect.

  46. Ricky Says:

    September 18th, 2009 at 11:26 am

    Keith was close when identifying the problem, but wrong.

    The issue is that when you give a z-index to a container, it restarts the z-indexing inside of it at 0, and the global-level z-index of everything inside the container becomes the same as the parent.

    When you have a container without a z-index, it gets a value of ‘auto’ and *shouldn’t* affect the z-indexing of anything inside of it.

    However when internet explorer finds a relatively positioned element with a z-index of ‘auto’, it treats it like it has z-index of ‘zero’, thus restarting the z-indexing inside that container. From that point, everything inside that container has a global-level z-index value of 0.

    Your solution worked because the menu aquired a global-level z-index that was the same as its container, which was in turn higher than the image of the car.

    To illustrate this, you could give the car a z-index of 500, and change the ‘1000′ z-index with a value of ‘1′ and it would still work.

  47. Okada Design Blog » Blog Archive » z-index problem with IE Says:

    September 29th, 2009 at 10:12 am

    [...] This article helped me how to solve IE z-index problem. There is a good comment at the end as well.Title is called Squish the Internet Explorer Z-Index Bug and written by Brenelz Web Solutions.http://www.brenelz.com/blog/2009/02/03/squish-the-internet-explorer-z-index-bug/ [...]

  48. Pete Says:

    October 14th, 2009 at 6:26 pm

    I spent a few hours on this last night and was ready to lose my mind when I found your tip. Other developers have posted complex javascript solutions online, but this solved my problem quickly. Thanks!!

  49. Jess Says:

    October 22nd, 2009 at 3:38 pm

    that’s so simple that makes me feel stupid for wasting so much time trying to fix it.

    thank you very much :)

  50. JW Says:

    October 25th, 2009 at 4:49 pm

    This was tweaking me off big-time. Thank you so much for the fix. Your solution was the simplest one for this ridiculous problem. Thanks again!

  51. Lloyd Armbrust Says:

    November 4th, 2009 at 12:26 am

    Awesomesauce. I was about to kill someone before finding this fix. You rock it.

  52. Greg Says:

    November 10th, 2009 at 3:55 am

    Hi,
    it’s nice solution but not worked for me.
    I have same case with menu,but under menu I have tabs. The issue with relative z-index -1 make tabs unavailable.
    Finally I did my solution with Jquery and relative position:

    jQuery().ready(function(){
    $(".slidemenu").hover(
    function(e) {
    $('#neg_position').css("z-index","-1").css("position","relative");
    },
    function() {
    $('#neg_position').css("z-index","").css("position","");
    }
    ).mousemove(
    function(e) {
    $('#neg_position').css("z-index","-1").css("position","relative");
    }
    );
    });

    slidemenu is container of my menu,neg_position my tabs container

    I hope it help to somebody.

  53. timkg» Blog Archive » IE z-index bug Says:

    November 10th, 2009 at 7:46 pm

    [...] But giving a higher z-index to the menu didn’t made it appear on top of the div below it. This post got the solution: the enclosing element of the menu needs a higher z-index than the actual menu. It [...]

  54. Andrew Mason Says:

    November 18th, 2009 at 9:02 am

    Just wanted to say thank you. I was struggling with this bug for a while until I found your post. Cheers.

  55. Jason M. Says:

    November 24th, 2009 at 6:41 pm

    Wow … I spent a couple hours today tryin’ to deal with this same issue. Apparently, not just the parent needs an updated z-index, but so do all the parents! Right up to the HTML tag.

    Thank you for this fix! You saved me hours and hours of time.

  56. Alex Says:

    November 26th, 2009 at 4:54 am

    Hey, this is great, works like a charm!

    Thanks for the easy fix

  57. York Says:

    December 1st, 2009 at 5:37 pm

    thanks mate, it saved me a lot of time~!

  58. Alina Says:

    December 1st, 2009 at 6:15 pm

    I cannot seem to get this to work for this page: http://yogasports.biz

    Can anyone help?

    The dropdowns appear behind the flash animation

  59. Anna Says:

    December 2nd, 2009 at 1:53 pm

    Worked perfectly. Thank you!

  60. Anna Says:

    December 2nd, 2009 at 1:54 pm

    Alina,

    Try making your Flash transparent. Should fix the problem.

  61. dev Says:

    December 3rd, 2009 at 5:42 am

    In addition to what Ricky said, IE always assigns a global z-index of 0 no matter whether the element is relatively or statically positioned whenever its z-index is not specified.

    So, the workaround would be to set a higher z-index of the topmost parent element (immediately inside body) containing the menu.

  62. yenny Says:

    December 6th, 2009 at 9:18 am

    thank you

  63. Ricky Says:

    December 9th, 2009 at 11:15 am

    dev, you’re right, but it is only on relative & absolutely positioned elements. If you remove the position: relative or position: absolute from the parent, the problem goes away. However this is rarely an option that is available.

  64. amrish Says:

    December 10th, 2009 at 3:15 am

    thanks a lot…saved my day :)

  65. Vera Says:

    December 11th, 2009 at 8:12 am

    great tip! now just to squash IE…

  66. Mohammed AbdelRahman Says:

    December 15th, 2009 at 1:56 pm

    saved my time! thanks :)

  67. Frank Says:

    December 15th, 2009 at 9:07 pm

    Thank you so much, that just saved me so much time!

  68. Neal Says:

    December 16th, 2009 at 3:31 pm

    You da man. Thanks!

  69. Internet Explorer Stylesheet Limit | Damian Nicholson Says:

    December 21st, 2009 at 3:28 am

    [...] figure IE  inconsistencies like the double margin bug, png fix and z-index issue all to be common knowledge amongst web developers and designers. But why not this one? Surely the [...]

  70. David McMeans Says:

    December 29th, 2009 at 1:34 pm

    Saved my bacon. Thanks! (I hate ie7)

  71. Jared Says:

    December 29th, 2009 at 2:06 pm

    Thanks! This helped a lot. Also, thanks for the link to the quirksmode page.

  72. Jerome St. Sauver Says:

    January 1st, 2010 at 11:29 pm

    Thank you, Thank you, Thank you.

  73. Prowebdesign Says:

    January 5th, 2010 at 9:32 am

    Scott – you are genious! Excatly what i needed here!

  74. Jason Says:

    January 5th, 2010 at 7:08 pm

    Thanks mate, solved my problem

  75. Flemming Rubak Says:

    January 13th, 2010 at 3:07 am

    Good trick – unfortunately it doesn’t always solve the problem, especially with javascript and flash elements.

    Instead you can stack the parent elements and use position:relative together with z-index. That stacks correctly in all IE versions.

  76. Bart van Irsel Says:

    January 15th, 2010 at 4:15 am

    Thanks a lot!

    This really helped us out. Time for IE7 itself to be squished! ;)

  77. Stanny Says:

    January 15th, 2010 at 12:06 pm

    I think I love you, man ;)

  78. @nomadone Says:

    January 17th, 2010 at 7:20 am

    Thank you so much, life saver, been struggling with this for ages now. Your solution worked, so quick & easy.

  79. Aleksandar Says:

    January 19th, 2010 at 3:19 am

    Thank you…

  80. mccormicky Says:

    January 19th, 2010 at 10:50 pm

    The plague of my existence, squished!!!

  81. eg Says:

    January 20th, 2010 at 6:03 pm

    There’s 2 days of my life I won’t get back…

    So quick, so easy, so works in every browser. Thanks!

  82. Sam Says:

    January 21st, 2010 at 12:45 am

    Thanks heaps, life saver :)

  83. Sushil Kumar Raghav Says:

    January 25th, 2010 at 8:27 am

    Thanks Bro… its really work.. :)

    Keep it up bro.. :)

  84. vedran Says:

    January 27th, 2010 at 4:13 am

    This solve my problem. Thanks a lot!

  85. Revisiting the HTML image map « Work with ChoiceCuts Says:

    January 27th, 2010 at 8:53 am

    [...] Map due to IE not co-operating when I put a drop-down menu on the same page. This article from Brenelz Web Solutions saved the [...]

  86. Colby Wright Says:

    January 29th, 2010 at 1:38 am

    Just wanted to say thanks for this tip. Its so simple but really works.
    Thank you.

    Colby Wright

  87. Kley Says:

    February 5th, 2010 at 12:50 am

    I am going crazy here. Where do I actually put this code? In the body html or in some js file? I have no idea where to put the code and I have tried everything but the menu still displays behind the graphic. Please I need help

  88. Sådan fixes IE7’s z-index bug med jQuery » Netsans Says:

    February 9th, 2010 at 5:51 am

    [...] løste ikke umiddelbart mit problem. Jeg havde imidlertid andetsteds læst, at man skulle kunne løse problemet ved at give det relativt positionerede forfade…, end det absolut positionerede [...]

  89. Internet Explorer 7 Z-index Bug | Mvied Designs Says:

    February 9th, 2010 at 11:59 am

    [...] I found a pretty good example of this bug in this article – Squish the Internet Explorer Z-Index Bug | Brenelz Web Design Solutions [...]

  90. Tim Says:

    February 10th, 2010 at 9:12 pm

    Wow you rock, finally found a solution!

  91. Mark Says:

    February 11th, 2010 at 1:17 pm

    My Thanks as well – I basically walked every node up from my Navigation DropDown and added one to the z-index or added it if it was not ther to every CSS item up the tree in my IE7 Override css and my IE 6 overrides. I then removed each one until I found the one that mattered – about 20 minutes total after 3 days of trying to solve…. Was the first node even with mine

  92. Dizzle Says:

    February 16th, 2010 at 3:20 pm

    Thank you for posting a straight forward solution with example, everyone else likes to point out they are aware of the bug don’t don’t bother sharing how they fixed it!

  93. Nikita Sumeiko Says:

    February 18th, 2010 at 4:14 am

    Thanks a lot. Really helped!

  94. Nikita Sumeiko Says:

    February 18th, 2010 at 4:15 am

    Also twitted your post here:
    http://twitter.com/manakor/status/9277818368

  95. m.a.c.h.designs Says:

    February 19th, 2010 at 10:33 am

    Simply thankyou!

    damn it I hate IE!

  96. Jeff G Says:

    February 19th, 2010 at 4:46 pm

    I love you! I had read through 5 other answers on other forums, etc, and nothing worked. Thanks, man!

  97. jappe liljedahl Says:

    February 22nd, 2010 at 1:51 pm

    thanks it solved my problem

  98. Jody Says:

    February 24th, 2010 at 4:25 pm

    Still not working…I’ve changed every z-index in my CSS file back and forth and the dropdown still does behind the div. In Firefox too. *sob*

  99. Joey G Says:

    February 24th, 2010 at 6:35 pm

    Like everyone else here I am very thankful for the nice job you did on this and the other solutions. I never would have thought that one bug would get so many people. I am lucky that I didn’t spend as much time as some of these people thanks to your site!

    Much Appreciation!!!

  100. Teresa Hardy Says:

    February 24th, 2010 at 8:15 pm

    That actually worked for me on two divs that weren’t even positioned absolute, but relative. Thank you, you saved my evening!

  101. ken Says:

    February 25th, 2010 at 2:39 pm

    I remember this was working for me, too, once in a while but today for whatever reason is not. Keep trying.

  102. Jonny Says:

    March 3rd, 2010 at 5:11 am

    Thank you! I had forgot how to fix this bug and after reading your solution I’ve fixed it in no time!!
    Great!

  103. Gaby Says:

    March 4th, 2010 at 7:17 pm

    life saving … thanks a lot ..

  104. Vicki Says:

    March 5th, 2010 at 10:46 am

    Thanks so much for this.

  105. Marcysecrets Says:

    March 11th, 2010 at 8:38 am

    Fantastic tip. After 4 days of frustration, Im half way getting it to work based on your solution. My left menu subitems flyout to the right and overlap content on the right but still ‘underlaps’ the content below. A little more tweaking I guess but nearly there.
    Wish you had some adsense ads I could click to say thanks.
    Well, very many thanks.

    Awardebooks.com, DigitalProductStore.com

  106. Haluk Says:

    March 12th, 2010 at 4:37 pm

    Worked great for me! Thanks a lot!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
connect with me!