CSSnewbie

About CSSnewbie

Our mission is to help the beginning to intermediate web designer master the subtleties of CSS by offering CSS tutorials, tips, and techniques.

Recent Article

Fixing the IE6 Whitespace Bug

Posted on May 14, 2008. By Rob Glazebrook.

Sometimes, when I’m building a vertical navigation menu (like the one pictured above), Internet Explorer 6 will fight with me in a fairly annoying way: it adds a bunch of white space between the list items (specifically, I think it’s adding space below each list item). This space isn’t a margin, and it isn’t padding… it’s just empty, un-selectable space. So what’s going on, and how do we fix it?

The “what’s going on” part is simultaneously simple and perplexing: IE6 is treating all the empty space inside of your HTML lists – that is, the stuff between your closing </li> and next opening <li> tag — as real space. That’s the simple part. The confusing part is, why would it choose to interpret this space as “real,” when it ignores all of the other space between tags in pretty much every other situation out there? Well, that’s just IE6 for you, and that’s the perplexing bit.

Luckily, there is a really easy solution to this problem. Actually, truth be told, there are several solutions. Depending on your situation, each might be the right fix for you.

Fix #1: Remove All Whitespace

This is the most arcane of all the solutions, but to be honest, it’s the one I’ve used most often in the past – because I didn’t know about the other solutions! If you remove the white space from your code, this prevents IE from having anything to screw up. Specifically, if you remove the white space between your closing list item and the next opening list item, and your last closing list item and the end of your list, this will fix the problem. So you just need to turn this:

<ul>
	<li><a href="#">First Item</a></li>
	<li><a href="#">Second Item</a></li>
	<li><a href="#">Third One!</a></li>
</ul>

Into this:

<ul>
	<li><a href="#">First Item</a></li><li>
	<a href="#">Second Item</a></li><li>
	<a href="#">Third One!</a></li></ul>

Like I said, this isn’t exactly an elegant solution. But it does work, and it can be useful in places when CSS isn’t entirely reliable (like in HTML emails).

Fix #2: Float the Anchor Tags

Jon Hicks popularized this fix years ago. Basically, you float the anchor tags left, and then clear them left as well, like so:

ul a {
	display: block;
	float: left;
	clear: left; }

This causes your links to behave like floated elements, which naturally don’t have any space between them. Unfortunately, it also prevents your links from filling all the horizontal space available, which would make a mess of the example I used above, because I’m relying on that width to create a background color and a border. However, if your links don’t have any fancy hover states or borders, this solution would work just fine.

Fix #3: Apply a Bottom Border

Another useful solution is to apply a border to your list item. Why does this work? I have no idea. Does a border just anywhere work? Nope. It has to be applied to the bottom of the list item (or to all four sides, as long as the bottom is included):

ul li {
	border-bottom: 1px solid #666; }

I suppose the logic on this one is that the space is on the bottom of the element, so specifying a definitive edge to the box causes IE6 to recognize the “real” end of the element. Whatever the reasoning, it works, and it’s a good solution if you don’t mind a pixel of extra space in between your elements in return for getting rid of the great swaths of space IE had introduced. And sometimes it even works in the design. In the image above, for example, the border between the items could just as easily be applied to the list item instead of the anchor (which is where I had placed it initially).

Fix #4: Use Display: Inline

In my experience, the most useful solution of the four is to force your list items to display inline, instead of as a block-level element (which they are by default):

ul li {
	display: inline; }

As far as I can tell, this technique was first popularized by Andy Budd waaay back in 2003 (of course, he was dealing with IE5 back then). I like this solution because it seems to do the least harm to my lists. It doesn’t add any additional space that I need to account for. It also doesn’t harm the size of my anchor tags: if I apply a “display: block” to my anchors, like I’ve done in the image above, my anchor will stretch out the “inline” list item to its standard size.

Any of these solutions will remove the extra space from between list items in IE6. The key is simply to pick the fix that works best with your particular design.

Responses

On 5/14/2008 at 1:34pm, Derek P. Collins said,

You can also put a space between the list text and the closing anchor tag, like this:

First Item
Second Item
Third One!

On 5/14/2008 at 1:36pm, Derek P. Collins said,

Let’s try that again:

<ul>
<li><a href="#">First Item </a></li>
<li><a href="#">Second Item </a></li>
<li><a href="#">Third One! </a></li>
</ul>

On 5/15/2008 at 1:13pm, Rob Glazebrook said,

Great to know, Derek! That makes for a total of five useful techniques to solve one very annoying bug. I love it when I get to learn new things just from writing articles. :) Thanks for sharing!

On 5/15/2008 at 1:23pm, David Hucklesby said,

I think you meant vertical menus, no?

But I do get the same issue in horizontal menus as well. Will any of these techniques work on those? (Apart from eliminating white-space between the tags, that is.)

Thanks for all your work. I help teach a beginner web design course, and often cite your clear explanations when students get stuck.

On 5/15/2008 at 2:59pm, Rob Glazebrook said,

Ooh, you’re right, David. Thanks for noticing that. I’ve fixed it in the article.

I’m not sure if the same bug would cause problems in horizontal menus or not. Anyone else know for sure? I’d almost expect that to be more of a double margin bug than anything else.

Where do you teach your course? I’m finishing up a beginning web design course here in Des Moines. I’m glad to know the site is proving useful!

On 5/20/2008 at 10:54pm, Anton said,

Thanks. I was suicidal, but your article saved me. Made list elements display: inline; and corrected the bug.

On 6/3/2008 at 6:47am, Kevin said,

I would also like to add that the first fix fixes the IE6 bug where it would put a invisible border onto the bottom of images.

I was searching for a solution to my problem when I came across this, though I’d give the fist solution a try and low and behold it worked.

Thanks.

On 6/9/2008 at 3:01pm, Stephen Emlund said,

I believe I’ve had the same problem in the past and I thought it was because the list items have a default margin and/or padding. So when I set the margin and padding to O it fixed it.. but I haven’t done it in a while, so I might be wrong.

BTW, nice website. I’m a graphic design intern at Workbench magazine.

On 6/30/2008 at 4:04pm, Debbie said,

Hey - GREAT info here! This is a pet peeve of mine as well! Great fix - that “inline”.

Leave a Reply

Do More