Fixing IE7 Z-Index Issues with jQuery

For some reason, Internet Explorer 7 does some pretty funky things, and has several known bugs with it’s rendering engine that drive web developers like me crazy.  While most of the known bugs occur in relatively obscure situations and go largely unnoticed, there are a few that really stick out and cause web developers to waste many hours trying to fix them.  The way IE7 renders z-index stacking orders is one of them.

One way to fix many of the issues with IE7 is to dynamically reverse the default z-index stacking order of the elements on your page. This will ensure the elements higher in your HTML source will also have a higher z-index order on your page, solving most of the IE stacking issues. If you’re using jQuery (the best Javascript library there is), here’s the quick fix:

1
2
3
4
5
6
7
$(function() {
	var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
});

This code will start with a z-index of 1000, and decrement the z-index for each DIV element of the page by 10, giving the first DIV a z-index of 1000, the second, 990, the third 980, and so on. Notice that the selector will find all DIV elements with the code “$(‘div’)”, using the same syntax as CSS selectors. If your HTML code has different requirements, feel free to change the code or the selector to suit your needs by following jQuery’s documentation on selectors.

Update for MooTools (04/14/2009):

A generous commenter has posted the code for fixing z-index issues with MooTools 1.2:

1
2
3
4
5
6
7
if(Browser.Engine.trident){
	var zIndexNumber = 1000;
	$$('div').each(function(el,i){
		el.setStyle('z-index',zIndexNumber);
		zIndexNumber -= 10;
	});
};

Hire Me

Have a problem you need help solving?
I'm available for freelance work. (and I love solving hard problems)

Trackbacks

Comments

  1. You have just saved me some serious grief. I’ve been fightin a dropdown showing over the wslide.js on IE7 for days now. Thanks for the fix.

  2. This tip solved the issue I was messing with for the last 5 hours.

    I have multiple Suckerfish menus on a page (a flyout list of social site links below article summaries on an article archive page) and in IE7 the menu button _below_ the active suckerfish menu would show _above_ the active menu. It was driving me mad.

    Thanks a ton for posting this solution.

  3. You have no idea, you’ve just save my life!
    Thanks so much !

  4. WOW this was an awesome fix, i spent hours trying all kinds of css hacks but this works like a charm espically if you got jquery runing!!

  5. Thank you Vance, your hack works well!

    One suggestion to other guys facing the same problem: you can add some extra jQuery code after Vance’s one to force spcecific elements of the page to have a z-index greater that the intial set value.

    Vance’s example starts with z-index=10000, so if you want to (for example) your dropdonw menu to appear over every other element, just set its z-index value to 20000.

  6. thanks ……… this z-index ie7 jQuery really help’s me , before i am scared of z-index problem in ie , now i am cool about z-index , really its working fine …….thanks once again………….

  7. Worked perfectly thanks !

  8. hi thanks for this fix, I would like to have certain specific elements have a greater z-index than inital set vale as lorenzo suggests, however am not sure what extra jQuery code i need to do this, have tried

    $(“.bridge”).css(“z-index”,”50000″); but dosent seem to have any effect, if anyone can point me in right direction be much appreciated

  9. Wow, that solved our problems for both IE6 and IE7. Thank you sir!

  10. Just amazing. I was going through the whole bloody net trying to solve this problem. Very simple fix for page were you have a good work flow.

    Thanks !!

  11. MooTools 1.2 version for anyone who needs it:

    if(Browser.Engine.trident){
    var zIndexNumber = 1000;
    $$(‘div’).each(function(el,i){
    el.setStyle(‘z-index’,zIndexNumber);
    zIndexNumber -= 10;
    });
    };

  12. Super helpful! Been struggling with this for days!

  13. Thanks Vance :-)

  14. So simple. So awesome.

  15. thanks men worked perfectly

  16. This script saved me from the verge of mental breakdown.

  17. Well, thank you VERY much.I agree with the previous comments, you saved a billions of neurons in our brains. I used some jquery tabs with superfish, set the styles for ff and every kind of normal browsers, but ie7 just made me crazy, i wanted to kill someone .I had to use li instead of div at the selector part, but it works fantastic now.

  18. Hi,

    Just amazing Thank you very very… much.

    Thakns,
    Bhanu.

  19. I LOVE YOU FOR THIS.

    I was about to rip my hair out. thank you.

  20. Hey dude, i’ve kissed your photo when i saw that this lines of code worked for me! i’ve wasted more than 10 hours trying to solve my problem! Thanks!
    ps: i’m not gay! (hehe)

  21. Wow. This problem has been making me mad the past few days. Thank you for your easy fix!

  22. thanks for this post! just amazing…

  23. Where do I insert this code into the jQuery.js? I initially put it right in at the top, but it has not worked for me yet.

    Thanks.

  24. Talk about saving me some serious grief with a simple and elegant solution! Thank you thank you.

  25. This was a GREAT solution. After struggling with getting a simple drop menu in the header to show about the content, and trying every css fix under the sun, this worked!

    One thing to note, z-index can sometimes effect the ‘clickability’ (is that a word? not according to spell check!) of an area of a page. So you could very easily apply this to a page and discover that some of your links and form elements are unusable. I ran into this problem myself.

    Just to give an example for anyone still reading – I’m working on a three column layout with a header and footer. I have a css/js dropdown in the header. The submenus overlap the center column. But in IE 7 they underlapped it. When I applied this fix, the menus worked perfectly, but my whole center column became unclickable. The fix to THAT was very easy. I simply restricted the script to header divs. So in my case my code looked like .
    So I changed the script to apply to divs in header thus $(div).each( … ) -> $(#header div).each( … )

    Thanks so much!

  26. Thanks for the quick fix. I struggled with this for days initially without any success. I wish I’d gotten to this site earlier and my z-index headache could have been fixed much earlier.

    Anyway, your code can always be reserve for my future use when I’ve a new site. The theme developer had advised me to add in certain z-index value to my style.css file and it worked.

  27. thanks, this sorted out my superfish issues on ie7 perfectly. :)

  28. This DOES work. I can verify in viewing generated source in Firefox that all my div’s are getting the z-indexing applied. I have jQuery Megamenus that are showing up underneath my Cycle plugin. The indexing is correct, I also used $(”.megamenuitem”).css(”z-index”,”2000″); to make my menu higher than the Cycle z-indexing. This all gets applied in code but makes no difference in IE7. :(

  29. No worries I fixed my problem! The top portion of my site that has the menu was a table (not by choice, the CMS) the rest of my page that has the cycle was custom divs. I gave that table a high z-index well above anything else, and I don’t need the jquery fix anymore for IE7…. for now.

  30. I want to kiss your face right now. Thank you thank you thank you.

  31. Wow! Many thanks !!! At first i thought that that would not help me, but i’ve change to “$(‘li’)” and it save my day.

    thank you a lot !

  32. Well, this really was a great and quick solution to a problem I was fearing of tackling in all sorts of other unimaginable ways and it turns out just a few lines of code does the trick. It’s a snippet of code I’ll be reusing throughout many projecfts I’m sure!

    Thanks!

  33. Thank you so much for solving this stacking problem – it rattled my brain for so long – I use a MAC and hate dealing with IE – but its all part of the game.

    Again – thanks.

  34. Thanks Vance – this has solved my drop-down menu not showing over a group of fading images – brilliant!

    BTW, I’m using WordPress, so to get your jQuery code to work I had to change all instances of ‘$(‘ with ‘jQuery(‘ (this is because WP also uses Prototype so you must specify jQuery explicitly or it won’t work!)

  35. Thanks a million.

  36. Thanks, Nice job man…

  37. Absolute saviour – thanks a plenty for this!

  38. Hi, can where should I place this function and how to call it? Thanks

  39. Absolutely Great :) I love you…

  40. Mait, you saved me from visiting psycho-ward… ***** bloody IE. Sometimes i wonder… does microsoft produce their crappy browser just to drive normal people mad or what *?

  41. Thank you. Just, THANK YOU!!

  42. holy crap! it works in ie6 too! i changed ‘div’ to ‘*’ just to get it working (im dealing with ul and div) and now i can leave work knowing that i have a fix to work with. ill work out the details later so im not applying it to everything…

    thank you so much!

  43. Oh! Your the verry best!!! Tnx for this great post.

    Life smiles once again!

  44. Horray! Thank you for the jquery code! Fastest fix for an annoying IE bug ever.

  45. This isn’t necessary at all, though it does work in some cases.

    For IE, you just need to make sure the parents of each element have the proper z-index. Putting the z-index only on the item itself will do no good in IE.

    However, if you put a z-index on the parent of the jquery element that is lower than the z-index of the parent element of the element that you would like above the jquery. It works just fine, and you don’t have to wait for the page to load for the fix to occur.

    I tried this fix, noticed it didn’t fix anything until the page fully loaded (but it did in fact fix it), then I removed this and fixed it myself with 2 lines of CSS.

    Please stop using javascript to fix problems that CSS, and a general understanding of how elements are rendered in a browser, can fix.

  46. This fix worked like a charm! Thank you so much!

  47. Thank youuuuuuuuuu !!!
    Saved a lot of headaches mate. Thanks again :D

  48. This solution totally worked for us, after trying just about everything else (even the parent z-index solution did not work for us)… Thanks!! We also made it custom for us by using a different selector, upping the starting z-index to 10000, and setting specific elements on the page to even higher values.

  49. THANKS!!! YOU SAVE MY DAY!!
    GREAT JOB!!!
    CREDITS TO YOU Vance

  50. Guess this sollution works for most people.
    But for me z-index in Firefox and IE8 started to behave just like IE6 and I7.

    So it just made it worse…

  51. After two hours I solved it.

    What I did was to to exclude one <li from the loop that had two classes. This was the li the contained the active menu items.

    I wanted this <li and all it´s children to have a higher z-index than other <li no matter what.

  52. Thanx a lot

  53. Amazing! I’ve never seen a fix like this! I just copied and pasted it and it worked hahaha. It’s kind of scary but I verified in IE7 and everything else is working fine.

    Thanks a lot!

  54. I really love you, man

  55. Thanks for this fix; you just saved me a lot of time and effort!

  56. Wow it worked! \o/ genius

  57. Me too. I’ve been scratching my head over this silly menu and a jquery display and it was IE7 all along.

    Thank you!

    Lance

  58. Thanks guy!
    You solved my problem with my menu!

  59. Awesome fix thanks

  60. Hey works well but I did struggle to get it working until i scrolled down and read to replace the “div” with “*”

    Is it worth putting that at the top???

  61. THANK YOU VERY MUCH!! I work hours making a layout and implment Superfish and Jquery, but, when open the site in IE, Fuc..#@#$@#!!! And lather I was encountred this post! Great!!

  62. I want to yell Yihaaaaaaaaaaaaaaaaaa !!!
    After all those hours trying, your code is like magic and save me so much time ! I can’t believe it but yes it’s solved ! thank you so much !!

  63. Thanks everyone for posting some great comments, praises and suggestions. I am glad it was helpful! :)

  64. You, sir, are a savior. I had a CSS dropdown menu combined with a jquery box replacement and the box items kept showing up behind the CSS dropdown menus even with z-index declared. Your code rescued me from the abyss of eternal dread. Thank you and season’s greetings!

  65. Much thanks!

  66. Thanks alot buddy….. :)

  67. This just saved me some time! thx!

  68. Yeah, luckily this was the FIRST thing I went to from the Google search so I did not spend much time on this problem after trying for about ten minutes to modify the css. The query I used was “jquery z-index ie7″.

    Thanks Vance. I don’t normally get my problems solved this quickly so I was pleasantly surprised.

  69. After wrestling with this issue for 2 hours, I finally stumbled upon your post and fixed my post. I can now go to bed a happy man.

    Thank you gentlest of princes.

  70. This is wonderful, thank you!

  71. hey,

    just wanted to send you a big cheers, you saved my day (or days)!!!

    d(ô.ô)b

All content copyright © 2013 Vance Lucas | Powered by WordPress | Entries (RSS) | Comments (RSS)