Sub topic 7
The whitespace gap between inline-block elements
This chapter assumes you know the CSS box model and the difference between block, inline, and inline-block boxes from the earlier chapters. It explains why a few pixels of space appear between inline-block elements that have no margin. It also covers how to remove or control that space.
Where the gap comes from
Put three list items in a row by making them inline-block:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Inline-block gap</title>
<style>
.row { list-style: none; padding: 0; margin: 0; }
.row li {
display: inline-block;
padding: 0.5rem 1rem;
background: #cde;
}
</style>
</head>
<body>
<ul class="row">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</body>
</html>You set no margin, yet the blue boxes do not touch. The space comes from the line breaks and indentation between the <li> tags in your markup.
An inline-block box is an inline-level box. It sits inside a line box, the same way words do. CSS treats whitespace between inline-level boxes as text. The browser collapses any run of spaces, tabs, and newlines into one space character, and it draws that character like any other character in the line. The space has the width of a space glyph in the parent's font, so the gap is typically about a quarter to a third of the font size. That is roughly 4 px at a 16 px font size.
Block-level siblings show no such gap. Each block starts its own line, and whitespace between blocks creates no line box, so the browser draws nothing for it.
Properties of the gap
Knowing what the gap is tells you what will and will not remove it.
- It is not a margin. Setting
margin: 0on the items changes nothing. Dev tools show it as empty space between the boxes, not as part of either box. - Its width depends on text styling of the parent. The space belongs to the parent's text, so the parent's
font-family,font-size,word-spacing, andletter-spacingall affect it. - It collapses. Five blank lines between two items still produce one space.
- It disappears at line edges. A space at the start or end of a line box is removed. When a row wraps, the gap at the wrap point vanishes, so rows of different lengths look slightly different.
A common failure: columns that wrap
The gap causes a frequent bug. You want three equal columns, so you write this:
.col {
display: inline-block;
width: 33.333%;
vertical-align: top;
}The three widths already total about 100%. Two space characters are added on top of that, so the row is wider than its container. The third column drops to a new line.
Percentage widths are the usual place you notice the gap, because there is no spare room to hide it.
Ways to remove the gap
| Technique | How it works | Main drawback |
|---|---|---|
| Flex container | Items become flex items, and whitespace between them is not rendered | Changes the layout model of the parent |
| `font-size: 0` on the parent | The space glyph gets zero width | Must reset the font size on every child |
| Remove whitespace in the markup | No space character exists | Hard to read and easy to break |
| Negative margin | Pulls items together by the gap width | Fragile, because the gap width depends on the font |
| `word-spacing` on the parent | Shrinks or grows the space glyph | Needs resetting on children, and still font-dependent |
Use a flex container
This is the preferred fix in current code:
.row {
display: flex;
gap: 0;
list-style: none;
padding: 0;
}Whitespace-only text between flex items is not rendered, so no gap appears. The li elements are blockified, which means their computed display becomes block. You no longer need display: inline-block on them. Two behaviors change:
- Items do not wrap by default. Add
flex-wrap: wrapif you want wrapping. gapnow gives you an explicit, font-independent spacing control. A value likegap: 0.5remreplaces the accidental gap with a deliberate one.
Set the parent's font size to zero
When you need to keep inline-block, make the space glyph zero-width:
.row {
font-size: 0;
list-style: none;
padding: 0;
}
.row li {
display: inline-block;
font-size: 1rem;
padding: 0.5rem 1rem;
background: #cde;
}This works because the space belongs to the parent's font. You must restore a real font size on the children, or their text disappears. Use rem rather than em for that reset, because em would compute against the parent's zero size. Any text placed directly in the parent, outside the children, also becomes invisible.
Remove the whitespace from the markup
If no whitespace exists, no space character is drawn. You can close tags against the next opening tag:
<ul class="row"><li>One</li
><li>Two</li
><li>Three</li></ul>You can also fill the gap with comments:
<ul class="row">
<li>One</li><!--
--><li>Two</li><!--
--><li>Three</li>
</ul>Both forms work in every browser. Both are awkward, because a formatter or a teammate who tidies the markup can bring the gap back without any visible error. Avoid them in shared code unless you cannot change the CSS.
Use a negative margin
You can pull each item left by about the width of the space:
.row li + li {
margin-left: -0.25em;
}This is easy to write and unreliable. The space is 0.25em in some fonts and different in others, so you get either a small gap or overlapping boxes after a font change. Treat it as a last resort.
Making the gap deliberate with word-spacing
Sometimes you want the gap, for example with tag-style labels that read like words. Since the gap is a space character in the parent, word-spacing on the parent controls it:
.tags {
word-spacing: 0.5rem;
padding: 0;
list-style: none;
}
.tags li {
display: inline-block;
word-spacing: normal;
padding: 0.25rem 0.5rem;
background: #eec;
}word-spacing is inherited, so the children would also pick up the wider spacing between the words inside each tag. The word-spacing: normal rule on the children stops that. The gap still depends on the font, which is acceptable when you want it to scale with the text.
What to use in practice
Items that form a navigation bar, a toolbar, or a column layout belong in a flex or grid container. They are not text, and flex gives you wrapping, alignment, and spacing without the whitespace problem. Keep inline-block for boxes that live inside a line of text, such as small badges next to words. In that case the space between a badge and the neighboring word is usually what you want.
Edge cases
white-space: preon the parent keeps all the whitespace in the markup as written, so you may see several spaces or line breaks. Do not use it as a layout tool.- Inline elements trigger the same gap. Two
<span>elements separated by a newline also have a space between them. This is expected for text, and it is whyHello</span> <span>worldreads as two words. - Vertical gaps are a different problem. If you see extra space below inline-block items, that comes from baseline alignment and line height, not from whitespace. The next chapter covers it.
Exercise
Three .col elements are display: inline-block; width: 33.333% inside a container. The third one wraps to a new line even though no margin is set. Explain why, and give two CSS fixes.
Show answer
The markup has whitespace between the elements. The browser collapses it to one space character per boundary and draws it in the line, so the total width is about 100% plus two spaces and the row overflows. The third item wraps. Fix one: make the container display: flex, which ignores the whitespace between items. Fix two: set font-size: 0 on the container and restore a normal font-size on each .col, so the spaces have zero width.
Exercise
You set word-spacing: 1rem on a parent containing inline-block items. The gap between items grows, but the text inside each item also spreads out. What change fixes this while keeping the wider gap?
Show answer
Add word-spacing: normal to the inline-block children. word-spacing is inherited, so the children took the parent's value. Resetting it on the children keeps the wider gap between items, because that space belongs to the parent, and restores normal word spacing inside each item.