Fixing IE7 z-index issue using simple jQuery Solution

For web developers, achieving cross browser compatibility is an headache, especially when it comes to Internet Explorer 6, and Internet Explorer 7.

While there are many bugs IE 6 is having, there are various fixes available to fix them. I’ll go through some of them in future, and explain you how to fix them.

For the time being, lets look at Internet Explorer 7 funky bugs which drives web developers like me crazy. While most of the known bugs occur in relatively obscure situations and go unnoticed, there are few bugs, that are really stick out and cause web developers to waste many hours to fix them. It is because of the IE7 bug which causes the browser to render z-index in unpredictable orders. This causes multi-level navigation menu to render unpredictable, and disturbs your other html elements.

One way to fix many of this issues with IE7 is to dynamically reverse the default z-index stacking order of the elements on 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. Here’s the quick fix using on the best Javascript library- jQuery.

<!--[if IE 7]>

<script type="text/javascript">

$(function() {
 var zIndexNumber = 500;
 $('div').each(function() {
 $(this).css('zIndex', zIndexNumber);
 zIndexNumber -= 5;
 });
});    

</script>
<![endif]-->

Explaination of the Code:

If IE 7, is the conditional tag, which asks browsers to run this javascript code only in case of the IE7.

This code will start with a z-index of 500, and decrement the z-index for each DIV element of the page by 5, giving the first DIV a z-index of 500, the second, 595, the third 590, and so on. Note that the selector will find all DIV elements with the code $(‘div’). If you have different requirements, feel free to change the code or the selector to suit your needs by following jQuery’s documentation on selectors.

I found this cool website where I read that ie7 does something weird with the stack order when it renders the page and solves the problem using jQuery. Surprisingly I just copied and pasted the code (after analyzing it of course) and it fixed my problem! I just changed code to start z-indexing from 500 instead of 1000, because of the z-index I’ve been using for other element.

Update:

This may not work correctly on Xhtml document without Doctype Specification. Learn more about Doctype Specification. Moreover, its also important to Validate your markup for correctly using this trick.

Enjoyed this post?

17 Comments (Post your comment)

  • Comment by Piyush — December 28, 2010 @ 12:18 pm

    Thanks for the help guys.. it really worked :)

  • Comment by landscape plants — January 2, 2011 @ 1:54 am

    I’m getting a javascript error, is anyone else?

  • Comment by Xcellence-IT — January 3, 2011 @ 11:10 am

    @landscape: what error are you getting?

  • Comment by clark — January 8, 2011 @ 12:14 pm

    it helped… t.y

    great for the starters..

  • Comment by StarJerk — January 12, 2011 @ 11:47 pm

    Im happy U did it. Thanks for writing so informative post. Im lookin’ forward to c and read ur next post. Cheers

  • Comment by amit — January 13, 2011 @ 8:52 am

    Your right Clark.
    its really gud for starters

  • Comment by Giuseppe Barrett — January 13, 2011 @ 3:13 pm

    Great article. Waiting for more.

  • Comment by Jenoffa — January 14, 2011 @ 5:20 am

    I found here something new and interesting. Thanks

  • Comment by Xcellence-IT — January 16, 2011 @ 9:21 am

    Thank you clark and Amit for your comments.

    Glad to see that it helps you.

  • Comment by Dionisio — February 1, 2011 @ 4:48 pm

    Why don’t you guys try to use a Doctype especification?

    I had these problems today, and it all solved when I specificated a Doctype.

    BTW, i used xHTML Transitional Doctype, like this:

  • Comment by Xcellence-IT — February 2, 2011 @ 10:48 am

    @ Dionisio, thank you for your comments on this. We’ve updated article so that other visitors can find this information.

    Regards
    Krunal

  • Trackback by favshare.net — February 6, 2011 @ 7:05 am

    Fixing IE7 z-index issue using simple jQuery Solution…

    Fix Internet Explorer 7 funky bug which causes funky z-index order. One way to fix many of this issues with IE7 is to dynamically reverse the default z-index stacking order of the elements on page. We tried to achieve that using jQuery….

  • Comment by M Gothar — February 10, 2011 @ 5:00 am

    Thank you so much, I’ve been searching this since last 2 days.

  • Comment by Mike — February 15, 2011 @ 6:04 pm

    I’m trying to fix my drop downs in ie7. It’s a wordpress site, I want to try this fix but I’m not sure how and where to add the code.

  • Comment by Xcellence-IT — February 15, 2011 @ 6:09 pm

    Hi Mike,

    You can add this code in the footer. Make sure you load jQuery before this code. Other wise it will cause error. If you’re still unable to fix it, please a share a link, I’ll be happy to guide you.

    Thank you
    Krunal

  • Comment by Reny J — June 5, 2011 @ 7:46 pm

    Appreciate it for all your efforts that you have put in this. very interesting info .

  • Comment by Adel — June 16, 2011 @ 10:11 am

    I found it interesting. It solved my problem. Thanks a lot

RSS feed for comments on this post. TrackBack URL

Leave a comment

Allowed tags:
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>

Please do not spam, no link dropping, no keywords or domains as names; and do not advertise!

asp dot net development e-commerce development it consulting company it web outsourcing company india open source development psd to xhtml conversion web development company wordpress theme customization development

Want to hire us to work for you?
View our Portfolio or Request a quote.

Live Chat or Call us at +91-8866 7711 00     Client Login