IE List Item CSS Margin Hack
March 16th, 2008 by John CrenshawAs a web designer you learn something new everyday about the intricacies of Internet Explorer’s faulty rendering of certain elements on a screen. While I’m not always a proponent of using a Strict Doctype, it does help solve some of those issues…then again, it also requires a bit more work and time is money, right? Well, one of the problems I’ve had with internet explorer in the past is the way it renders lists, unordered and ordered lists, especially IE6. Even with a Strict DOCTYPE declaration, internet explorer has a nasty little habit of adding margin between list items when you’ve specifically written into the CSS that it shouldn’t behave this way. I have to admit, I do this everyday and it took me a while to figure this out, so hopefully this will help someone else having margin problems with list items in IE 6 or any other version of internet explorer.
Internet Explorer List Item Margin Problem
So what is the problem exactly? Here’s a snapshot of the sidebar of a site I’m working on in Firefox:

Incidentally, this is also how it appears in IE 7. Now, the snapshot of how that same unordered list appears in IE 6:

Whoa! Where’d all that extra margin come from? Well, it certainly didn’t come from the CSS because every single other browser tested displays fine and all the margins are set to 0px, as well as padding. So what’s the problem here?
IE Problem with List Items and Display: Block
The problem rears its ugly head, in this situation, because I have the display property of those list items set to block in the stylesheet. Internet Explorer, but especially version 6, just freak out when trying to display lists when the links within those lists are styled display: block. So, how do we fix it? We need to trick internet explorer (because it’s bugginess also allows us to trick it into behaving the way we want) into thinking that the display property is set to inline, after we’ve done that we can essentially tell it we’ve changed our mind and we want the display property set to block. This will make IE really throw a temper tantrum, but it’s exactly the temper tantrum we want it to throw.
Conditional Comment Styles
To solve this we need to add a conditional comment in our header section, preferably near our other stylesheet declarations. This conditional comment is something only IE will read because, you guessed it, it’s psychotic:
<!--[if IE]>
<link rel=”stylesheet” type=”text/css” href=”http://yourdomain.com/styles/ie.css” />
<![endif]–>
Now you need to create a new stylesheet for Internet Explorer only and call it ie.css.
Once you’ve done that add these two lines to the stylesheet:
#sidebar li a{display:inline-block !important;}
#sidebar li a {display:block !important;}
The “#sidebar” part is part of my stylesheet because the sidebars on that particular site are wrapped in a
#sidebar ul li a{display: block; }
IE may try to ignore the hack above in the ie.css file. Why? Because this second declaration is more specific. The !important declaration prevents that.
So, now that we’ve added those two lines to ie.css, what does IE 6 show us?

Ahhh, beautiful ain’t it?




