Sub topic 7
The whitespace gap between inline-block elements
This chapter assumes you know basic HTML, how to write CSS rules, and the box model. It also assumes you have read the previous chapter on display: inline-block. Here you will see why a small gap appears between inline-block items, and how to remove it.
Where the gap comes from
An inline-block box sits in a line of text, like a word. In HTML, a line break or a run of spaces between two tags counts as whitespace. The browser collapses that run into a single space character. When the neighbors are inline-level, that space is drawn.
This markup shows the problem:
<ul class="row">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>Suppose the CSS sets .row li { display: inline-block; }. The newline and indentation between each </li> and the next <li> become one space each. The result is a gap about as wide as a space in the current font, usually 3 to 5 pixels at normal text sizes.
The gap is not a margin or padding. You will not find it in the developer tools box model view. It is a text character, so its width depends on font-size and font-family of the parent.
Why block and flex layouts do not show it
Block boxes stack vertically, so there is no line for the space to sit in. In a flex or grid container, whitespace-only text between items is not rendered. Only inline-level siblings share a line with that space.
Fix 1: use flex on the container
The most common modern fix is to stop using inline-block for the row.
.row {
display: flex;
gap: 0;
list-style: none;
padding: 0;
}The items become flex items. The whitespace between them is ignored, so no gap appears. If you want spacing, set it deliberately with gap.
Fix 2: set font-size to zero on the parent
If you must keep inline-block, you can shrink the space itself. Set the parent's font-size to 0, then restore it on the children.
.row {
font-size: 0;
}
.row li {
display: inline-block;
font-size: 1rem;
}This works because the space character is sized by the parent's font. A zero-size font gives a zero-width space. The drawback is that any text placed directly in .row also disappears, and children using em units resolve against the parent, so you must restore sizes carefully.
Fix 3: change the markup
You can remove the whitespace from the HTML so no space character exists. One approach places the closing bracket of each tag on the next line:
<ul class="row">
<li>One</li
><li>Two</li
><li>Three</li>
</ul>An HTML comment between the items also works. Both are valid, but they make the markup harder to read and easy to break during editing. Use them only when you cannot change the CSS.
Fix 4: use a negative margin
You can pull each item left by the width of the space. This is fragile, because the space width changes with the font. Avoid it unless the font is fixed.
Comparing the options
| Fix | Changes | Main risk |
|---|---|---|
| Flex container | CSS on parent | Items are no longer inline-level |
| `font-size: 0` | CSS on parent and children | Hides stray text, affects `em` units |
| Markup trick | HTML | Hard to maintain |
| Negative margin | CSS on children | Breaks when the font changes |
Common errors
- Looking for the gap in the box model view. It is a text space, so no margin or padding explains it.
- Setting
margin: 0on the items and expecting the gap to close. The margin is already zero. - Using
font-size: 0and forgetting to restore the size on the children, so their text vanishes. - Adding
display: flexto the items instead of the container. Flex applies to the parent.
Exercise
Three li elements are display: inline-block with no margin. The markup puts each on its own line, and a gap still shows between them. What causes the gap, and which single CSS rule on the parent ul would remove it while keeping the items in a row?
Show answer
The newline and indentation between the li tags collapse into one space character, which is laid out in the line between inline-level boxes. Setting display: flex on the ul removes it, because whitespace between flex items is not rendered. (Setting font-size: 0 on the ul also works if the li elements restore their own font size.)
Exercise
Why does setting font-size: 0 on the parent shrink the gap, and what is one side effect?
Show answer
The gap is a space character sized by the parent's font, so a zero font size makes it zero wide. A side effect is that any text placed directly in the parent becomes invisible, and em units on children now resolve against the zero size unless the children set their own font-size.