How to Remove Gaps Between Inline and Inline-Block Elements
A gap between inline-block elements can remain after margin: 0 because it comes from HTML whitespace. Use Flexbox with gap to control spacing between independent buttons or tags. If the layout must stay inline, remove whitespace only between the items that should touch.
Distinguish whitespace from CSS margins
Inline boxes participate in a line like words. With white-space: normal, line breaks and indentation between adjacent tags can become one space. An inline-block also participates in inline layout on the outside. In developer tools, check the computed margins and inspect the text nodes between tags. The space width depends on the font and font size. CSS Text · White Space Processing
Compare four layouts with the same items
Save the code as inline-space.html and open it. The four lists all use zero item margins. The first keeps the gap from line breaks. The second has touching borders. The third uses a 12px gap. The fourth sets the parent’s font size to zero and restores item text to 1rem.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare gaps between inline-block elements</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
h1 { font-size: 24px; }
.demo { margin: 0; padding: 0; list-style: none; max-width: 650px; }
.demo li { display: inline-block; width: 80px; margin: 0; padding: 8px;
border: 1px solid #7692ae; background: #edf3fa; vertical-align: top; }
.flex { display: flex; flex-wrap: wrap; gap: 12px; }
.zero { font-size: 0; }
.zero li { font-size: 1rem; }
</style>
</head>
<body>
<h1>Why the gaps differ</h1>
<h2>With line breaks between tags</h2>
<ul role="list" class="demo normal">
<li>HTML</li>
<li>CSS</li>
<li>JS</li>
</ul>
<h2>With whitespace removed between tags</h2>
<ul role="list" class="demo tight"><li>HTML</li><li>CSS</li><li>JS</li></ul>
<h2>Using Flexbox and gap</h2>
<ul role="list" class="demo flex">
<li>HTML</li>
<li>CSS</li>
<li>JS</li>
</ul>
<h2>Setting the parent’s font size to zero</h2>
<ul role="list" class="demo zero">
<li>HTML</li>
<li>CSS</li>
<li>JS</li>
</ul>
</body>
</html>
Change the Flexbox gap: 12px to 20px. The spaces between items and wrapped rows grow, without adding an outer gap. Restore the original value. When keeping an inline layout, remove whitespace only between the items that should touch. A template or formatter can bring the spacing back by inserting line breaks. CSS Box Alignment · gap
Choose between removing whitespace and setting a gap
With font-size: 0 on the parent, its whitespace has zero width. Without the restore rule, the item text disappears too. Check any em values that depend on the parent’s size. A fixed negative value such as margin-right: -4px can overlap items or leave an extra gap in another environment.
Watch for side effects of zero font size
To change the space between the text and border, check item padding. For an indent on the whole list, check the default ul padding. Regular non-replaced inline elements do not use width, height, or vertical margins like block boxes. Use inline-block or Flexbox when items need their own sizes and vertical spacing. CSS 2.2 · Inline, non-replaced elements




