02/08/04

25 Comments

IE Whitespace Bug

Its been a sort of Holy Grail for me (well maybe a Holy Teacup) to find a solution the IE Whitespace bug. If you’re not familiar with this, then you probably haven’t applied display:block to links within an unordered list. Adding this CSS property creates a solid ‘button’ out of what would’ve been a humble inline link, one that can be sized and prettied up as you like. The trouble is, IE windows adds another line height between all these links – unless you remove every bit of whitespace from the HTML.

So, in effect, there is already a fix to this, but it’s not a convenient one. Editing a menu that’s all on one line isn’t my idea of an easy life. Each time I would have to take the file into BBEdit, reformat the code to be ‘Gentle Hierarchal”, make my edits, and then reformat as ‘compact’.

I’d tried various fixes, one that added a negative top margin (overruled for decent browsers by using a first-child selector) worked in 5 & 6, but not 5.5. Then I started dreaming up some php that would strip the whitespace at the server side, but that was too much for my small head.

Now, excuse me if I’m late to the party, and everyone has already discovered the solution to this, but its actually rather simple. Here’s a cut down example:

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

Suddenly, IE 5+ does what it should, and the ones that got it right in the first place just put up with it without a fuss apart from Mozilla. If you find that the menu items are all over the place, add something like this:

li>a {
float: none;
}

The first-child selector means that IE won’t see this rule, but everyone else will.

Technorati TagsTags: , , , ,

Comments | RSS

#1

Will said 1379 days ago:

Another technique can be found here .
#2

Will said 1379 days ago:

OK. That didn’t work. Let me try again
#3

marko said 1378 days ago:

Yes, and by simple addition:
li { float: left; } it is transformed to horizontal navigation. If you are going to use some background-image at <ul> element be careful to put exact height as of ‘blocked’ links.
#4

Magnus Haugsand said 1378 days ago:

Hehe, I actually discussed this bug with a friend of mine hours ago, but we never found out how IE manage to add that mysterious space.

The problem can easily be solved by adding width: 100%; to a. In most cases, 100% is the actual width in the starting point.
#5

Mike D. said 1378 days ago:

Good fixes. Thanks. I always just throw this into the anchor style:

_height: 1em

That fixes it as well, and only IE/PC (all versions) pick it up.
#6

Jon Hicks said 1378 days ago:

Thats really interesting, because neither of those fixes had worked for me! Having said that, I hadn’t tried it with the underscore, so thanks for that Mike.

Time to try them again…
#7

Phill R Kenoyer said 1378 days ago:

Why are we all programming hacks to compensate for MSIE’s bugs? I say forget MSIE. Program to web standards. Make your site look good in standards complaint browsers. If it does not look right in MSIE. Then tell everyone to quit using MSIE.

I run an ISP and I tell everyone to use Firefox or get a Mac. I have almost my whole user base switched off of MS now.

Only by force will we kill off MSIE.
#8

Thomas Scholz said 1378 days ago:

There are four solutions. I listet them a while ago on css-dicuss: http://archivist.incutio.com/viewlist/css-discuss/34926.
#9

Jon HIcks said 1378 days ago:

Great!

SO WHY DIDN”T ANY OF YOU TELL ME THIS LAST YEAR????!!!

;o)
#10

Paul D said 1378 days ago:

If it’s any consolation, Jon, I’ve been struggling with this bug and didn’t know there was a solution; so your article is timely and helpful! :)
#11

Mike D. said 1378 days ago:

Yep, the problem is that if you don’t include the underscore, real browsers will make sure that your anchor item is exactly 1em tall, even if it wraps… thus truncating things to one line and making it ugly.

But including the underscore hides the rule from every browser except IE, so when IE sees the rule it renders the anchor item at 1em tall plus whatever it needs for extra lines. Perfect.
#12

Dante Evans said 1378 days ago:

I read on somewhere Dean Edwards’ site that applying vertical-align: bottom to the LIs fixes the whitespace bug.
#13

Andy said 1378 days ago:

Jon SO WHY DIDN”T ANY OF YOU TELL ME THIS LAST YEAR????!!!

What, and spoil the thrill of the discovery? :)
#14

John Serris said 1378 days ago:

Well I put this together a while ago so I could always refer to it.
#15

Mike Stenhouse said 1377 days ago:

I normally go for the empty comments work-around and leave my CSS be.

<ul>
<li>some item</li><!––
––><li>next item</li><!––
––></ul>

It’s not pretty but it leaves no room for clashing styles…
#16

Tim McElwee said 1375 days ago:

I just ran across this fixwhile trying to fix IE’s weird background image handling.

It also worked to remove, at least, the vertical whitespace from the horizontal navigation on the problem pages when I applied a line-height to the <li> styling. I haven’t run it through earlier versions of IE, but I’ll run a few tests.
#17

Small Paul said 1375 days ago:

Yeah, I went with the:

ul#nav li
{
display: inline;
}

solution, cos all browsers I’ve tried it on have been happy with it. It’s not a hack, and it keeps all code pertaining to visual display in the stylesheet, where it belongs.

And hey, Phil: we’re coding around IE’s bugs because IE is used by millions of people the world over, who will never (and can’t fairly be expected to) change their browser. You telling them to do so isn’t going to make it happen. Just be glad Netscape 4 doesn’t have IE’s market share.
#18

Jon Hicks said 1375 days ago:

Unfortunately, for this site I need the <li> to stay as a block element. I tried all the fixes, and in the end, it was Mike’s that works the best for my nav. The only drawback is that it means my CSS doesn’t validate anymore :o(

Still I prefer keeping it in…
#19

bart said 1375 days ago:

Use:

* html ul li a {height:1em;} instead of _height:1em;. It should validate.
#20

jasidog said 1372 days ago:

li {
height: 1em;
}
html>body li {
height: auto;
}

Should mean it will validate and look ok in mozilla.
#21

Jon Hicks said 1372 days ago:

funny timing jasidog – I’ve just done that!
#22

jasidog said 1371 days ago:

I came across this exact problem last night and thought of this article.

Funnily enough I’ve been working on changing my site over to textpattern, based on how well I saw it being used here in the non blog ( I had thought it only really useful for blogs.) sections of your site.

So since I’ve never posted here before today, I thought I’d say thanks for the inspiration! :)
#23

Steve Dennis said 1362 days ago:

Virtually all of the above fixes get rid of the huge space between list items, but I’m still getting a 1px gap between items in IE that shouldn’t be there, and nothing above seems to get rid of it.

This wouldn’t usually matter, except the nature of the borders and rollovers in the design make it extremely annoying. Anyone run into anything similar?
#24

Mark said 1361 days ago:

Steve I have. I incorporated a simple height:1em; into my li a style and it fixed it in everything but IE 5.0. IE 5.0 adds the 1 pixel but I am willing to live with it. One of the reasons I started using importing for my CSS is because I no longer wanted to deal with NN 4 or IE 4 as well. As long as the design is close that should be sufficient. And your design should leave a bit of room for “movement”. Great site BTW and thanks to all commentors for your input. Very valuable stuff!
#25

Steve Dennis said 1361 days ago:

Yeh the 1px space appears in IE6 for me too :(

Commenting is closed for this article.

Elsewhere

The Rissington Podcast - weekly shenanigans with Jon Oxton

Hicksmade - unique handmade goods by Leigh Hicks width=

love

Brit Pack: A proud member I love Omniweb Coda Segment Publishing I buy my type from Veer The Forgiveness Project