How to remove the space between inline/inline-block elements?

Alternatively, you should now use flexbox to achieve many of the layouts that you may previously have used inline-block for:

Since this answer has become rather popular, I’m rewriting it significantly.

Let’s not forget the actual question that was asked:

How to remove the space between inline-block elements? I was hoping
for a CSS solution that doesn’t require the HTML source code to be
tampered with. Can this issue be solved with CSS alone?

It is possible to solve this problem with CSS alone, but there are no completely robust CSS fixes.

The solution I had in my initial answer was to add font-size: 0 to the parent element, and then declare a sensible font-size on the children.

This works in recent versions of all modern browsers. It works in IE8. It does not work in Safari 5, but it does work in Safari 6. Safari 5 is nearly a dead browser (0.33%, August 2015).

Most of the possible issues with relative font sizes are not complicated to fix.

However, while this is a reasonable solution if you specifically need a CSS only fix, it’s not what I recommend if you’re free to change your HTML (as most of us are).

This is what I, as a reasonably experienced web developer, actually do to solve this problem:


Yes, that’s right. I remove the whitespace in the HTML between the inline-block elements.

It’s easy. It’s simple. It works everywhere. It’s the pragmatic solution.

You do sometimes have to carefully consider where whitespace will come from. Will appending another element with JavaScript add whitespace? No, not if you do it properly.

Let’s go on a magical journey of different ways to remove the whitespace, with some new HTML:

    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
  • You can do this, as I usually do:

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

  • Or, this:

         <li>Item 1</li
         ><li>Item 2</li
         ><li>Item 3</li>
  • Or, use comments:

         <li>Item 1</li><!--
         --><li>Item 2</li><!--
         --><li>Item 3</li>
  • Or, if you are using using PHP or similar:

         <li>Item 1</li><?
         ?><li>Item 2</li><?
         ?><li>Item 3</li>
  • Or, you can even skip certain closing tags entirely (all browsers are fine with this):

         <li>Item 1
         <li>Item 2
         <li>Item 3

Now that I’ve gone and bored you to death with “one thousand different ways to remove whitespace, by thirtydot”, hopefully you’ve forgotten all about font-size: 0.

Leave a Comment