Standards-aware web designers generally know of the z-index bug in all versions of Internet Explorer (though, version 8 is rumored to fix it).
In the W3C’s specs, z-index is designed to affect the stacking order of positioned elements on a web page. So, an element with a z-index of 2 should always appear above an element with a z-index of 1.
In Internet Explorer, this doesn’t work like it should. Internet Explorer resets the stack when the positioned elements are separated from each other.
So let’s say we have the following HTML. Fairly standard header, navigation, etc.
<div id="wrapper"><div id="header"><ul id="nav"><li><a href="#">home</a></li><li><a href="#">item one</a><ul><li><a href="#">sub item one</a></li><li><a href="#">sub item two</a></li></ul></li><li><a href="#">item two</a><ul><li><a href="#">sub item one</a></li><li><a href="#">sub item two</a></li></ul></li></ul></div><div id="container"><h1>Hi. This is a positioned H1</h1><p>This page is just some friendly content to show you just how
bad IE really is.</p></div></div>Then, we have the following CSS.
#wrapper #header {position: relative;}#wrapper #nav {clear: both;margin: 0 5px;padding: 0 5px;width: 750px;height: 30px;list-style: none;border-top: 1px solid #335a86;border-bottom: 1px solid #335a86;text-align: center;position: relative;z-index: 2;}#wrapper #nav li {float: left;margin: 0;padding: 0 0 5px 0;border: 0;position: relative;}#wrapper #nav li a {margin: 0;padding: 7px 15px;display: block;text-decoration: none;font-size: 1.2em;}#wrapper #nav a:link, #wrapper #nav a:visited {color: #888;}#wrapper #nav a:hover, #wrapper #nav a:focus {color: #335a86;}#wrapper #nav li ul {background-color: #ccc;width: 150px;height: auto;list-style: none;margin: 0;padding: 5px 0 10px 0;border: 0;text-align: left;position: absolute;display: none;}#wrapper #nav li ul li {float: none;margin: 0;line-height: 30px;height: 30px;}#wrapper #nav li ul a {padding: 7px 10px;white-space: nowrap;display: block;}#wrapper #nav li:hover ul {display: block;}#wrapper #container {padding: 10px;position: relative;}#wrapper h1 {position: absolute;left: 10px;top: 10px;height: 60px;line-height: 60px;vertical-align: middle;font-size: 2em;background: #335a86;color: #fff;}#wrapper #container p {margin-top: 60px;}This is very common code used to trigger a CSS dropdown menu in all modern browsers. Remember that IE6, of course, requires a small JavaScript. A good example is the Sons of Suckerfish. I do not have this JavaScript on my current example, since there are plenty of other great articles about that.
When the code below the navigation, in this case the absolutely positioned h1, is any positioned element (or a select element, Flash movie, etc.), all versions of Internet Explorer prior to version 8 will cause the dropdown menus to fall behind the content.
The fix for this is very simple, but there are incredibly large websites that use jumbled masses of iframes, extra divs, and other horrors to get Internet Explorer to display the dropdowns above the offending elements.
For a fix, we use the following CSS for the header div. See the screenshot for an example of this (again, in IE7). Click it to see a larger version.
#wrapper #header {position: relative;z-index: 2;}Now, you will also need to make sure that your container div, whatever it is called, is styled correctly. In my example:
#wrapper #container {position: relative;}This ensures that the header and the container, whatever you call them, understand their relationship to each other for the z-index fix to work.
This fix causes the dropdowns to appear above everything that is inside the content areas of the page. It works in IE7 and IE6. My example adds some code to show the <select> statement, which is another common offender.
Jonathan Stegall is a web designer and emergent / emerging follower of Jesus currently living in Atlanta, seeking to abide in the creative tension between theology, spirituality, design, and justice.
Mike Todd continues his series reflecting on his recent trip to Israel/Palestine. This one introduces us to the "security wall" that is under construction there, and is a violation of international law. He shows its role in preserving the prevailing narrative there and here, and how it affects Palestinians.
More links & commentary delicious
Full Blogroll Blogroll & Friends
More music Last.fm
More books revish
RT @EugeneCho: Face the facts of being what you are, for that is what changes what you are. ~ Soren Kierkegaard 6 hrs ago
jonathanstegall Twitter
Thank you! Elegant & perfect & simple solution! You saved me a great deal of grief, too!
damn.. its not work on ie 6 ..
frame solution always better !
@kuse: Do you have an example of the solution not working in IE6? I’m always interested to see if these kind of challenges can be resolved in a standards-friendly way.
Thank you thank you thank you.
Works for me in ie 7.
@Larry: Glad it worked for you. Feel free to come back, if you run into any issues or have any questions with it.
Larry you are a livesaver! This fix I have been looking for, for some time. Thank you so much.
@Matt: I’m Jonathan, but you’re welcome
. Glad it helped.
This is the CORRECT solution. Lots of forums on the net have you adding z-index to all different parts of the drop down. You only need to add it to the header. This works on Suckerfish and Son of Suckerfish as well.
Thank you so much. You helped me a lot.
Sorry it doesn’t work in my IE 6 (6.0.2800.1106 with SP1). The items don’t even drop down.
IE 7+ works great, but I need for IE6, any suggestions? Thanks.
@Bill: Right. To get dropdowns to work in IE6, you do need a small JavaScript. This is because IE6 does not support pseudo classes on lists, like new browsers do.
To fix this, there are a variety of scripts available. One of the easiest to use is the Suckerfish dropdown technique from several years ago. There are others, but this one is a great start.
The dropdowns themselves are not what I was looking to show with this particular post, though of course it is a related issue. Thanks for asking.
Thanks so much for this. Im working on a new project and knew that z-index was the problem. I tried adjusting various elements of the navbar CSS, based on many blog posts, without success. It did not occur to me to raise the z-index on the entire header wrapper (which the navbar is inside). I had given up, and instead had simply put a message in the fine print, suggesting that IE users upgrade to IE8 in order to properly view the site. Now the whole header sits in front of the content, including the navbar dropdowns. It finally works in IE7, which is what I was testing with! Handy to know it wont work in IE6 too! (Not that I care about that).
Im leaving that message in the fine print though!
Thank you!
I couldn’t figure out the reason for the life of me why the drop down menu was displaying behind only SOME of the content. This was a very concise and helpful explanation.
Thank you again!
Thank you for such a simple and elegant solution. I finally found your website way past midnight and was ready to tear my hair out. It worked beautifully.
-V
So simple, and works so beautifully.
I tried pasting the code in my css sheet, but the drop sown menu is still behind the text and images in IE8. Any suggestions as to what I am doing wrong?
you save my day buddy, fantastic just simply awesome. end of the IE sucks a lot
LOL
@Ronek: This is IE8 where the issue is happening? Does it also happen in IE6/IE7? What about Firefox? Also, does your HTML and CSS validate?
Look into these things first, and see if you notice anything that tips you in the right direction. If you don’t, feel free to post a link where we can see the offending page.
[...] dropdown menu Filed under: Code, Firefox, Internet Explorer — papercliphand @ 4:09 pm IE z-index bug with CSS dropdown menu | jonathan stegall: creative tension Leave a [...]
Fuggin’ sweet, thanks so much. Couldn’t figure this one out.
Thanks for this. Worked perfectly! Had been trying to get it to work by applying the z-index to the ul ul before I found this.
YOU ROCK! Thank you!!!
You save my life! Love you!!!
Thank you sooo much! Great solution.
Thanks for the fix Jonathan. It is the best explanation of it that I have seen, but it is not working on my site in ie7.
@Joshua: Can you send a link? I’d be happy to look at it. Also, have you checked to make sure the HTML and CSS validate? Does it work in IE6?
Thanks! Worked like a charm!
Thank you!
But it’s not working in IE 6.
Please give some solution.
@shirley: Well, the solution depends on what you are seeing in IE6. To get dropdowns to work in IE6, which is the only difference in this case with IE7, you do need a small JavaScript. This is because IE6 does not support pseudo classes on lists, like new browsers do.
To fix this, there are a variety of scripts available. One of the easiest to use is the Suckerfish dropdown technique from several years ago. There are others, but this one is a great start.
The dropdowns themselves are not what I was looking to show with this particular post, though I’m happy to link to those kind of solutions.
Thanks. This may have saved me. Still testing, but it seemed to work.
Hi Jonathan,
Great article!
On my site it works great in IE8 but in IE7 my drop down menu appears behind a table that loads a javascript rss reader…it is bugging me a bit..
If you ping via mail I can give you the details. I have tried the z-index but something is still off.
Best regards,
Phillip
Shot – works well, IE is so #$@!$#@%@$%
Wow, your fix did indeed fix my very thorny IE7 / z-index / drop down menu issue on this URL – http://weygandtwines.com/wine-french.htm. The theory makes perfect sense (in a warped IE7 sort of way), and to me better code is better fix than javascript.
A thousand thank yous for saving my skin.
Excellent, this was driving me nuts, had the same problem with a dropdown falling behind a slider.
Thanks
Awesome, saved me a ton of time today, now I can get on with my work. Thanks!!!
hmmmmm.
This fix appears to work in showing the menu above content, but unfortunately mouse events on the contents of the menu (ul>li>ul>li>a) don’t appear to bubbling. It’s as if the display is showing the header at the correct level, but the event model in the browser is still not able to figure this out…
got an annoying issue here that will not seem to resolve itself.
strange…
Thanks, exactly what I needed
Thanks, I am looking it during last 6 month. It’s really help to me. Today, i solved my problem.
Thanks again….. God bless you
doesn’t work in IE 6 for me either. in fact, when i hover over the menu nothing happens.
@j.m.: If nothing happens in IE6, you do need a small JavaScript as I mentioned earlier. This is because IE6 does not support pseudo classes on lists, like new browsers do.
To fix this, there are a variety of scripts available. One of the easiest to use is the Suckerfish dropdown technique from several years ago. There are others, but this one is a great start.
A nice, clear description of the issue yields a timely 3AM bug fix for me. Thanks!
Thanks – helped me greatly!
Thank you!. It works perfectly as you described for IE6 and IE7.
thanks very much, this was exactly what i needed to cater for the users languishing in an ie7 hell…
Awesome. I had been struggling with fixing the z-index bug even with help from some other respectable websites, but the clarity with which you have explained and the simple example have helped me understand almost instantly the solution. how do i thank you?
Cheers.
you are a good man.