
Put two inline-block boxes next to each other and you may see a small gap. It often comes from the source code, even when both boxes have zero margin.
Why inline-block elements have a gap
Inline-block boxes participate in the inline formatting context. A newline or spaces between the two tags often collapse to one visible space, just as a space separates two words in a sentence. How wide it looks depends on the font.
<div class="row">
<div class="item">One</div>
<div class="item">Two</div>
</div>.item { display: inline-block; }Five ways to handle the whitespace
1. Remove the whitespace in the HTML
Put the closing and opening tags together, or put a comment between them so the line break is inside the comment. This keeps inline layout but makes the result sensitive to automatic formatting and template changes.
<div class="item">One</div><!--
--><div class="item">Two</div>2. Set the parent’s font size to zero
The whitespace text node shrinks when the parent’s font size is zero. Then restore the size on the children with a unit such as rem. This technique can hide actual text nodes and make inherited sizing harder to reason about.
.row { font-size: 0; }
.item { font-size: 1rem; }3. Float the boxes
Floats are outside the inline flow, so whitespace between them does not create the same gap. The parent can contain them with display: flow-root. This can help when maintaining an older layout that already depends on floats.
.row { display: flow-root; }
.item { float: left; }4. Use Flexbox
Flexbox is a good fit for a row of items. Whitespace-only text nodes do not produce visible flex items. Set gap if you want controlled spacing, and use flex-wrap if items should move onto another line.
.row { display: flex; flex-wrap: wrap; gap: 0; }5. Use CSS Grid
Grid is useful when items should align across rows and columns. The following code creates two equal columns with no gap.
.row {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 0;
}Open the original example for this section
Choosing a solution
Use Flexbox for a row with flexible items, Grid for alignment in two dimensions, or keep inline-block if the boxes need to remain part of a line of text. Test other viewport sizes and check how the boxes behave when their labels wrap.