← Back to HTML & CSS, page 8

html-css-0141 · Easy fresher

There is a small gap under an image inside a box, with no margin or padding set. Why is it there, and how do you remove it?

An img is inline, so it sits on the text baseline, and the line box keeps room below it for descenders. That creates the gap. Set img to display: block, or use vertical-align: top, middle or bottom.

Full answer

An img is an inline element by default. Inline content sits on the text baseline of its line. Below the baseline, the line box keeps room for the descenders of letters such as g and y. So the line is a little taller than the image, and a gap appears under it. The size depends on the font and the line height of the parent. It exists even when the parent holds no text, because the browser still builds the line.

You can remove the gap in a few ways.

  • img { display: block; } is the simplest. The image is no longer in a line, so there is no baseline. Use margin-inline: auto if you need to centre a block image.
  • img { vertical-align: top; } (or middle or bottom) moves the image off the baseline. It keeps the image inline, which helps when it sits next to text.
  • Setting line-height: 0 or font-size: 0 on the parent also works, but it can affect other content inside, so it is a weaker fix.

A modern reset often sets img, svg, video { display: block; max-width: 100%; } for this reason. The same gap appears for svg, canvas, video and input type="image", and for inline-block elements.

To find the cause, open the developer tools, select the parent and look at its height compared with the image height. The difference is the baseline gap. Highlight the line box if your tools show it.

The first box is taller than its 50px image, so a pink gap shows below it. The second and third boxes are exactly 50px high.

This is a normal part of how inline layout works, and it is not a browser bug.

html
<style>
  .box { background: #fdd; width: 100px; font: 16px/1.5 sans-serif; margin-bottom: 8px; }
  .box img { width: 100px; height: 50px; }
  .fix1 img { display: block; }
  .fix2 img { vertical-align: top; }
</style>
<div class="box" id="a"><img alt="" src="/a.png"></div>
<div class="box fix1" id="b"><img alt="" src="/a.png"></div>
<div class="box fix2" id="c"><img alt="" src="/a.png"></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Images are inline and sit on the baseline.
  • The line box reserves descender space below.
  • display: block removes the baseline problem.
  • vertical-align moves the image off the baseline.
  • The same happens with svg, video and canvas.

Common mistakes

  • Adding a negative margin to hide the gap.
  • Setting a fixed height on the parent to cut it off.
  • Not knowing it happens with svg, video and canvas as well.

Follow-ups

  • Does the gap depend on the parent's font?
  • Which other elements show the same gap?

Tips

  • Explain baseline and descender space in one or two sentences.
  • Give display: block as the first fix and vertical-align as the second.