Q&A: Extra whitespace in lists

Q: I keep having problems with a list showing up as two lines of instead of one (there is a big gap between the items) in Internet Explorer 6. I don’t know what is causing it as there is nothing inside the list items but text.

A: The problem is that you most likely have line breaks and tabs in between the closing LI and the next opening LI. This is a great thing to do to keep your code readable but Internet Explorer 6 (MSIE6) has a problem with it. One workaround would be to add the linebreaks and tabs inside the brackets of the LI elements, but that is neither pretty nor clever as MSIE6 is on the decline. You can use CSS to work around the same problem by using a fixed height for MSIE6 and overriding this for newer and better browsers that understand the child selector. A sequence like li{height:1em;} html>body li{height:auto;} will fix your problem as MSIE6 also increases the size of the element when the font is larger or the content of the list item spills out into two lines. Without the second selector newer browsers would cut off your content.

This has been published in the Q&A session of the UK based paper magazine “net”. Reproduction rights were given by the publisher.

4 Responses to “Q&A: Extra whitespace in lists”

  1. OPC Toolbox » Blog Archive » Extra whitespace in lists Says:

    [...] tems if your li tags are on separate lines. This brief article suggests a couple of simple methods for coding IE-friendly lists. This entry was posted on S [...]

  2. Ed Eliot Says:

    Here’s a more compact version.

    li { _height: 1em; }

    Doing it this way means you don’t need to add a second rule to reset for well behaved browsers.

  3. Ben Morrison Says:

    If you know the width of the element you could use that to trigger HasLayout, negating any hacks.

    li {width:200px};

  4. WEB 3.0 » Blog Archive » ¿Así que querías saber de CSS? Says:

    [...] Extra White Space in Lists – Christian Heilmann [...]

Leave a Reply

Wait till I come! is the blog of Christian Heilmann , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).