A Space between Inline-Block List Items

HtmlCssXhtml

Html Problem Overview


> Possible Duplicate:
> Unwanted margin in inline-block list items
> How to remove “Invisible space” from HTML

Why do the inline-block list items have a space in them? No matter how I make my list items into a menu, I always get spaces.

li {
  border: 1px solid black;
  display: inline-block;
  height: 25px;
  list-style-type: none;
  text-align: center;
  width: 50px;
}
ul {
  padding: 0;
}

<ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
</ul>

Html Solutions


Solution 1 - Html

I have seen this and answered on it before:

After further research I have discovered that inline-block is a whitespace dependent method and is dependent on the font setting. In this case 4px is rendered.

To avoid this you could run all your lis together in one line, or block the end tags and begin tags together like this:

>

    >
  • >
    first
    >
  • >
    first
    >
  • >
    first
    >
  • >
    first
    >
  • >

Example here.


As mentioned by other answers and comments, the best practice for solving this is to add font-size: 0; to the parent element:

ul {
    font-size: 0;
}

ul li {
    font-size: 14px;
    display: inline-block;
}

This is better for HTML readability (avoiding running the tags together etc). The spacing effect is because of the font's spacing setting, so you must reset it for the inlined elements and set it again for the content within.

Solution 2 - Html

Solution:

ul {
    font-size: 0;
}

ul li {
    font-size: 14px;
    display: inline-block;
}

You must set parent font size to 0

Solution 3 - Html

I would add the CSS property of float left as seen below. That gets rid of the extra space.

ul li {
    float:left;
}

Solution 4 - Html

Actually, this is not specific to display:inline-block, but also applies to display:inline. Thus, in addition to David Horák's solution, this also works:

ul {
    font-size: 0;
}
ul li {
    font-size: 14px;
    display: inline;
}

Solution 5 - Html

Another solution, similar to Gerbus' solution, but this also works with relative font sizing.

ul {
    letter-spacing: -1em; /* Effectively collapses white-space */
}
ul li {
    display: inline;
    letter-spacing: normal; /* Reset letter-spacing to normal value */
}

Solution 6 - Html

I had the same problem, when I used a inline-block on my menu I had the space between each "li" I found a simple solution, I don't remember where I found it, anyway here is what I did.

<li><a href="index.html" title="home" class="active">Home</a></li><!---->
<li><a href="news.html" title="news">News</a></li><!---->
<li><a href="about.html" title="about">About Us</a></li><!---->
<li><a href="contact.html" title="contact">Contact Us</a></li>

You add a comment sign between each end of, and start of : "li" Then the horizontal space disappear. Hope that answer to the question Thanks

Solution 7 - Html

just remove the breaks between li's in your html code... make the li's in one line only..

Solution 8 - Html

Even if its not inline-block based, this solution might worth consideration (allows nearly same formatting control from upper levels).

ul {
  display: table;
}
ul li {
  display: table-cell;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTyler CromptonView Question on Stackoverflow
Solution 1 - HtmlKyleView Answer on Stackoverflow
Solution 2 - HtmlDavid HorákView Answer on Stackoverflow
Solution 3 - HtmlTrevor GView Answer on Stackoverflow
Solution 4 - HtmlGerbusView Answer on Stackoverflow
Solution 5 - HtmlLeo PittView Answer on Stackoverflow
Solution 6 - HtmlFabienView Answer on Stackoverflow
Solution 7 - HtmlMyaView Answer on Stackoverflow
Solution 8 - HtmlFrosty ZView Answer on Stackoverflow