46

How to you get rid of the white space between list items? I am trying to make it so that the images are right next to each other. Even though I have set the styling to margins: 0;, they are still separated.

CSS

ul.frames{
  margin: 20px;
  width: 410px;
  height: 320px;
  background-color: grey;
  float: left;
  list-style-type: none;
}

ul.frames  li {
  display:inline;
  margin: 0;
  display: inline;
  list-style: none;
}

ul.frames li img {
  margin: 0 0 0 0;
}

HTML

<li>
  <img id="myImg" src="img.jpg" width="102.5px" height="80px"/>
</li>
<li>
  <img id="myImg2" src="img.jpg" width="102.5px" height="80px"/>
</li>
<li>
  <img id="myImg3" src="img.jpg" width="102.5px" height="80px"/>
</li>
<li>
  <img id="myImg4" src="img.jpg" width="102.5px" height="80px"/>
</li>
YasirA
  • 9,531
  • 2
  • 40
  • 61
user1940007
  • 721
  • 3
  • 10
  • 14

8 Answers8

111

Updated Sept. 1st, 2014

In modern browsers, flex-box is the preferred method of doing this. It's as simple as:

ul {
  display: flex;
}

See a JSFiddle here.

For legacy browser support refer to the other options below, which are still just fine, albeit slightly more complex.


Though each of the other answers gives at least one good solution, none seem to provide all of the possibilities. And that's what I'll try to do here.

Firstly, to answer your implicit question of why there's spacing, it's there because you've set your LIs to display as inline elements.

inline is the default display value for text and images in all of the browsers that I know of. Inline elements are rendered with spacing between them whenever there's whitespace in your code. This is a good thing when it comes to text: these words that I've typed are spaced apart because of the space I've included in the code. And there's also space between each line. It's this very behavior of inline elements is what makes text on the web readable at all.

But sometimes we want non-text elements to be inline to take advantage of other properties of this display style. And this typically includes a desire for our elements to fit snugly together, quite unlike text. And that seems to be your problem here.

Without further ado, here are all the ways I know of to get rid of the spacing:

Keeping them inline

  1. (Not recommended) Apply negative margin to the LIs to move them over.

    li { margin: -4px; }
    

Note that you'll need to 'guess' the size of a space. This isn't recommended because, as Arthur excellently points out in the comments below, users can change the Zoom of their browser, which would more than likely mess up the rendering of your code. Further, this code requires too much guesswork and calculation. There are better solutions that work under all conditions.

  1. Get rid of the whitespace

    <li>One</li><li>Two</li>
    
  2. Use comments to make the whitespace a comment JSFiddle

    <li>One</li><!--
    --><li>Two</li>
    
  3. Skip the closing tag (HTML4 valid / HTML5 Valid) JSFiddle

    <li>One
    <li>Two
    
  4. Put the whitespace in the tag itself (Note: Early Internet Explorers will not like this)

    <li>One</li
    ><li>Two</li
    >
    
  5. Take advantage of the fact that the spacing between the elements is calculated as a percentage of the font size of the parent. Consequently, setting the parent's font size to 0 results in no space. Just remember to set a non-zero font-size on the li so that the text itself has a nonzero font size. View on JSFiddle.

Floating them

  1. Float them instead. You'll probably want to clearfix the parent if you do this.

    li { float: left; display: block; }
    
Community
  • 1
  • 1
jamesplease
  • 12,547
  • 6
  • 47
  • 73
  • 10
    I don't care if no.4 is valid ... it still looks completely wrong to me! – Sean Jan 05 '13 at 16:27
  • 3
    +1 and I don't think the negative margin is a good idea because you don't know the real size of the space character which could change with the zoom level of the browser. – arthur Jan 05 '13 at 16:33
  • That worked, but when I resize the images then there is a space between the two picture lines here is a jsfiddle http://jsfiddle.net/jspence29/PP94M/ – user1940007 Jan 05 '13 at 16:57
  • user, a few problems: (1) Close your UL. (2) Images are inline, so you need to set them to display: block; to get rid of their spacing (3) You overwrote your display:block; a few lines down in your LI properties. [View on JSFiddle](http://jsfiddle.net/wV7TD/) – jamesplease Jan 05 '13 at 17:05
  • Thank you the images was the problem – user1940007 Jan 05 '13 at 17:10
  • 4
    +1. there's also the font-size:0; on the parent element way – Danield Aug 28 '13 at 05:49
  • 2
    font-size: 0 i think is best way for this, if you can't change markup.(even spaces) – Павел Иванов Sep 23 '14 at 06:41
  • ha @ПавелИванов did you downvote me because of that? I have that as the 5th item on this list, and I attempted to avoid saying that any given method is better than any other (other than flexbox, which is, assuming browser support, likely actually the best way to do this). – jamesplease Sep 23 '14 at 13:59
  • +1 didn't even think to use float... – Feign Oct 21 '14 at 20:08
  • 5
    After 2 years.. I love the `ul { font-size: 0; } ul li { font-size: $size; }` option the best. So I can still have my guidelines without "hacks". – Cagatay Ulubay Dec 03 '15 at 12:58
  • Thank you guy for the answer – leandroungari Sep 18 '16 at 23:36
  • 1
    font-size: 0 in parent should be addded to the answer if possible as it suggests all methods. i am suprised i did know all =) – sewo Nov 18 '16 at 00:20
  • @sewo it's already there. Item 5 under "Keeping them inline" – jamesplease Nov 18 '16 at 02:39
  • If you float the li items left, then ```vertical-align: middle``` stops working. – gbenroscience Mar 13 '19 at 03:47
  • Thanks for this answer! One addition -- you can use `ul { display: inline-flex; }` for inline lists. – Mike T Apr 05 '23 at 15:07
32

Incredible but no one here has provided the proper solution for this problem.

Just do this:

ul.frames {
  font-size: 0;
}
ul.frames li {
  font-size: 14px; font-size:1.4rem;
  display: inline;
}

Keep in mind that we won't always have access to modify the markup. And trying to remove the spaces from the <li>s with JavaScript would be totally unnecessary when the solution is simply two font-size properties.

Also, floating the <li>s introduces another potential problem: You wouldn't be able center and right align the list items.

If you try to do float:right; on the <li>s then their order will be swapped, meaning: the first item in the list would be last, the second item is the one before the last, and so on.

Check out this other post here in SO: A Space between Inline-Block List Items

Community
  • 1
  • 1
Ricardo Zea
  • 10,053
  • 13
  • 76
  • 79
3

You should just remove all the spaces in the ul tags just like this: http://jsfiddle.net/dFRYL/3/

Since the <li> elements are inline, in you write spaces in or between them you will have spaces displayed.

arthur
  • 950
  • 1
  • 5
  • 18
3

The reason you get the spaces is because you have spaces between the elements (line break)

<ul>
     <li>One</li><li>
     Two</li><li>
     Three</li>
</ul>
Andy
  • 39
  • 1
  • Mine was style sheet linking problems. If you are using bootstrap, styles from them can get inherited on to your elements. I've answered here: https://stackoverflow.com/questions/38962503/override-bootstrap-style-not-working/49657636#49657636 – Deke Apr 04 '18 at 18:13
1

You can use negative margins like this:

margin-right: -4px;
margin-bottom: -4px;

Take a look here.

It also works up and down, I added another one to show that here.

jackcogdill
  • 4,900
  • 3
  • 30
  • 48
0

Using display:inline; causes whitespace in your HTML to create whitespace when displaying the HTML.

There are two solutions to this:

1) Change how you make them appear inline, I would recommend using floats on all of the list items, then using a clearfix of sorts.

2) Remove all whitespace between your list items, e.g.

<li><img id="myImg" src="http://stephboreldesign.com/wp-content/uploads/2012/03/lorem-ipsum-logo.jpg" width="102.5px" height="80px"/></li><li><img id="myImg2" src="http://stephboreldesign.com/wp-content/uploads/2012/03/lorem-ipsum-logo.jpg" width="102.5px" height="80px"/></li><li><img id="myImg3" src="http://stephboreldesign.com/wp-content/uploads/2012/03/lorem-ipsum-logo.jpg" width="102.5px" height="80px"/></li><li><img id="myImg4" src="http://stephboreldesign.com/wp-content/uploads/2012/03/lorem-ipsum-logo.jpg" width="102.5px" height="80px"/></li>

Personally I would recommend option 1 (I hate display: inline)

Sean
  • 6,389
  • 9
  • 45
  • 69
0

here is my attempt at it. Hope it helps. As Sean Dunwoody mentioned, white space in your html can be a cause of the space, but I've floated the li and made the image to display:block;. Left comment on where I made changes. Hope it helps: http://jsfiddle.net/FJ3nV/

Here my small but main changes:

/*
* Added float left
*/
ul.frames  li {
  margin: 0;
  list-style: none; 
  float:left;
}

/* 
*  Moved inline sizing here just to clear up obtrusive html.
*  Added display block.
*/
ul.frames li img{width:102px; height:80px; display:block;}
robx
  • 3,093
  • 2
  • 26
  • 29
0

I would change your li elements to inline-block.

One person did not recommend

li { margin: -4px; }

But making a slight change to it will cause it to work even when the font size changes or when the browser zooms in

li{ margin-right: -0.25em; }

That should fix that white space problem completely. However, if you are using a poorly designed font-face that doesn't follow correct letter-height standards then it may cause a problem. However those are harder to find and most of the fonts google hosts don't have that issue.