It's by design and is related to the baseline alignment. To understand what is happening add more text inside your container
why parent div height is larger than child div?
<div style="background-color:red;">
<div style='background-color:blue; width:32px; height:32px; display:inline-block'></div>
p
</div>
<hr>
adding any content fixes sizing
<div style="background-color:red;">
<div style='background-color:blue; width:32px; height:32px; display:inline-block;color:#fff'>
x
</div>
p
</div>
& nbsp; works too
<div style="background-color:red;">
<div style='background-color:blue; width:32px; height:32px; display:inline-block;color:#fff'> </div>
p
</div>
In the 3 cases, the inline-block
is aligned with the baseline but the baseline of an empty inline-block
element is its bottom edge so you have that bottom space that is for descender.
The baseline of an 'inline-block' is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its 'overflow' property has a computed value other than 'visible', in which case the baseline is the bottom margin edge. ref
You will also have that issue in the 3 cases if you add oveflow:hidden
why parent div height is larger than child div?
<div style="background-color:red;">
<div style='background-color:blue; width:32px; height:32px; display:inline-block;overflow:hidden'></div>
p
</div>
<hr>
adding any content fixes sizing
<div style="background-color:red;">
<div style='background-color:blue; width:32px; height:32px; display:inline-block;color:#fff;overflow:hidden'>
x
</div>
p
</div>
& nbsp; works too
<div style="background-color:red;">
<div style='background-color:blue; width:32px; height:32px; display:inline-block;color:#fff;overflow:hidden'> </div>
p
</div>
To avoid this you simply change the alignment to something different than baseline:
why parent div height is larger than child div?
<div style="background-color:red;">
<div style='background-color:blue; width:32px; height:32px; display:inline-block;overflow:hidden;vertical-align:top;'></div>
p
</div>
<hr>
adding any content fixes sizing
<div style="background-color:red;">
<div style='background-color:blue; width:32px; height:32px; display:inline-block;color:#fff;overflow:hidden;vertical-align:top;'>
x
</div>
p
</div>
& nbsp; works too
<div style="background-color:red;">
<div style='background-color:blue; width:32px; height:32px; display:inline-block;color:#fff;overflow:hidden;vertical-align:top;'> </div>
p
</div>
Related:
Why is this inline-block element pushed downward?
Image inside div has extra space below the image