views:

418

answers:

4

I have a horizontal menu consisting of <li> elements with display: inline.

The elements are supposed to be next to each other seamlessly.

In the source code, I would like to have each li on one line for easier debugging:

<li class="item1 first"> ... </li>
<li class="item2"> ... </li>
...

However, if I add a \n after each element, the menu items have a gap between each other. I gather this is the intended behaviour, but is there any way to turn it off using a clever "white-space" setting or something?

Edit: I can't use float, this is in a CMS with the option to center the list items.

A: 

CSS+float is your friend.

EricSchaefer
I can't use float, this is in a CMS with the option to center the list items
Pekka
A: 

HTML is whitespace independent - so adding line breaks should have no effect. Some browser's rendering engines don't quite get this right however.

What you probably want to do is add

float: left;

to your li tags to get them next to each other.

Adam Pope
I can't use float, this is in a CMS with the option to center the list items.
Pekka
Adam: of course whitespace is not ignored in `inline` mode. Otherwise there would be no spaces between the words in your sentences.
Ben James
+1  A: 

Do you have the ability to edit the CSS? If so, you could try adjusting the padding/margins on the <li> element. It does seem to be a lot of effort of readability.

Depending on what browser you are using you can get the HTML Tidy http://users.skynet.be/mgueury/mozilla/, which gives you the option to Tidy up your source, which might be useful enough for debugging

Jeepstone
HTML Tidy is not an option because it is production code I am debugging. Still, good idea.
Pekka
+3  A: 

You can avoid the rendering issues but keep the code maintainable like this:

<ul
  ><li>item 1</li
  ><li>item 2</li
  ><li>item 3</li
></ul>

It removes the whitespace but it's still easy to edit the items in a text editor, provided your CMS doesn't mess with the markup you enter!

John O'Rourke
Hey, this is a great idea! I think I will use this.
Pekka
I was going to suggest the same thing, John. The new line character is treated as white space just like any other white space and most browsers convert those to a single blank space separating words or whatever elements. If you put the new line inside the tag, the browser won't render it.
Tracy Probst