17

I have the following HTML;

li 
{
    list-style: none;
    border: solid 1px blue;
    display: inline;
    margin: 0px 0px 0px 0px;
}
...
<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 2</li>
</ul>

When I add the list items on their own line they appear with a horizontal space between them, but when I do it as;

<li>Item 1</li><li>Item 2</li><li>Item 2</li>

they dont.

Is there anyway to stop the new line from showing up as the blank space, or do I need to use a negative margin?

cosullivan
  • 426
  • 1
  • 4
  • 9

5 Answers5

30

You can remove that space by using HTML comments:

<ul>
   <li>Item 1</li><!--
--><li>Item 2</li><!--
--><li>Item 2</li>
</ul>

There is also a css property supported by newer browsers, but I can't remember what it's called (this is a hard search query).

Kevin Beal
  • 10,500
  • 12
  • 66
  • 92
  • 3
    This is a very unique alternative to having disorganize code like everyone suggests. – Crystal Miller Jul 30 '14 at 13:38
  • 5
    You may be referring to [`text-space-collapse: discard;`](https://drafts.csswg.org/css-text-4/#propdef-text-space-collapse) which has not been implemented in any browsers yet. – Timothy Zorn Feb 16 '17 at 03:20
10

The reason there is space between them is because there is space between them. :-)

You can float the li's to the left and that'll get rid of it:

li { float: left; }

Ciao!

docwhat
  • 11,435
  • 6
  • 55
  • 54
  • To clarify the "...because there is space between them...": HTML will collapse all white-space into a single space element between words or elements. It will not remove all white-space altogether or else things would be a mess. :-) – docwhat Dec 19 '09 at 04:24
  • This is good but is there any other solution without using float? – Rupam Datta Jun 28 '17 at 05:22
8

To avoid those spaces you can use flex css property

ul {
    display: flex;
}

See example.

eremcha
  • 93
  • 1
  • 5
4

That space is correct with inline content. You have two alternatives:

  1. Put them on the same line like you're doing; or
  2. Use floats.

For example:

ul { overflow: hidden; }
li { float: left; border: solid 1px blue; margin: 0px; }

The overflow: hidden ensures the containing <ul> won't collapse. Compare the difference with and without it if you add a red border to the <ul>.

cletus
  • 616,129
  • 168
  • 910
  • 942
2

Apply float property to li's and use CSS reset or atleast:

* {
 margin:0;
 padding:0;
}
eozzy
  • 66,048
  • 104
  • 272
  • 428