日本語

How to Remove the Small Gap Below an Image in CSS

​​CSS​HTML,Playwright

The few-pixel gap below an <img> disappears when you add display: block to the image. If you want to keep the image on the same line as text, leave display unchanged and add vertical-align: bottom instead. This is the fix for card layouts and banners where a thin strip of background color shows under a photo, or where the space between an image and its caption is too large.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened a page with only HTML and CSS in Playwright and measured positions and sizes with getBoundingClientRect(). Unless noted otherwise, the parent text was 16px, line-height was 1.5, the font was Noto Sans CJK JP, and the image was 200×100px. I did not test Firefox or Safari.

Two-row comparison. In the top row, with no styles on the img, a thin pink strip runs the full width between the mountain picture and the black "next element" bar below it. In the bottom row, with img { display: block; }, the black bar follows directly under the image and no pink is visible
Chromium screenshot with the parent background set to pink so the gap under the image is visible. With no styles, a 5px gap remains (top). With display: block, the gap disappears (bottom). The labels in the image are in Japanese.

The gap comes from placing the image on a text line

An <img> with no styles had display: inline and vertical-align: baseline when read with getComputedStyle(). The image is placed inside a line box like text and aligned to the baseline (the line along which text sits). MDN's description of vertical-align says that baseline aligns the element's baseline with the parent's baseline, and that an element with no baseline uses the bottom edge of its margin box. Because the bottom edge of the image sits on the baseline, the part of the line below the baseline stays as a gap.

A <div> containing only a 100px-high image was 105px tall, leaving a 5px gap below the image. Changing the parent's font size or line height changed the gap.

Parent setting Parent height Gap below image
None (16px, line-height: 1.5) 105px 5px
font-size: 12px 104px 4px
font-size: 24px 108px 8px
font-size: 32px 110px 10px
line-height: 1 101px 1px
line-height: 2 109px 9px
font-family: serif 107px 7px

Even with no text after the image, the parent's font size, line-height, and font change the gap. This means space for the parent's text is reserved below the baseline. This is why the "few pixels" varies from page to page.

Ways to remove the gap and when each one fits

Changing either the image or the parent removed the gap. In every case, the parent height became 100px, the same as the image.

Approach Gap Good for
img { display: block; } 0px Images stacked one per line (cards, banners, figure)
img { vertical-align: bottom; } 0px Placing text or icons next to an image
img { vertical-align: top; } 0px Same as above, but a small image still leaves a gap (see below)
Parent with display: flex or display: grid 0px Placing images side by side
Parent with line-height: 0 0px Not recommended (text overlaps the next element)
Parent with font-size: 0 0px Not recommended (text inside becomes invisible)

Use display: block for images placed one per line

If each image sits on its own line, display: block is the most straightforward choice. The image is no longer placed inside a line box, so the gap disappears regardless of the parent's font size. The gap was 0px with the parent font at 32px and with a small 20px-high image.

css
img {
  display: block;
}

One thing to watch is that centering works differently. In a 400px-wide parent with text-align: center, an inline image was centered (its left edge at 100px). With display: block, it moved back to the left edge (0px). To center a block image, set its left and right margin to auto. With margin-inline: auto, the left edge was at 100px.

css
img {
  display: block;
  margin-inline: auto; /* Set left and right margins to auto to center the image */
}

Use vertical-align: bottom for images placed beside text

When text continues beside an image, you cannot use display: block. In a <div> with text after the image, setting the image to display: block moved the text to a separate line below the image (100–124px from the top). In this case, change vertical-align instead. In a <div> with text after the image, vertical-align: bottom made the parent 100px tall, and the text lined up with the bottom edge of the image.

Be careful with vertical-align when the image is shorter than the line. Here is what I measured with a <div> containing only a 20px-high image:

Setting on the 20px-high image Parent height Gap below image
None 25px 5px
vertical-align: top 24px 4px
vertical-align: bottom 24px 0px
display: block 20px 0px

When the image is shorter than one line (24px), the line height stays at 24px. With vertical-align: top, the image aligns to the top of the line, leaving 4px empty below it. With bottom, the bottom edge of the image sits on the bottom of the parent, but the parent stays 24px tall, taller than the image. If you want the parent to be exactly the same height as the image, use display: block.

text-bottom left a 1px gap when the parent font was 32px. In the conditions I tested, bottom gave 0px in every case. If you are unsure, choose bottom.

Make the parent flex for images side by side

When three 50px-high images were placed side by side with line breaks between their tags, there was a 5px gap below them and a 3.59px gap between the images. vertical-align: bottom removed only the gap below; the horizontal gap stayed at 3.59px. Adding display: flex to the parent removed both, leaving 0px.

css
.gallery {
  display: flex;
  gap: 8px; /* Use gap to set the space between items */
}

To put space between images, add gap to the parent. With gap: 8px, the space between images was 8px and the gap below was 0px, whether the parent text was 16px or 32px.

line-height: 0 and font-size: 0 have side effects

Setting line-height: 0 or font-size: 0 on the parent also removes the gap when the parent holds only an image. However, problems appear when the same parent also contains text.

In a <div> with text after the image, line-height: 0 made the parent 100px tall, but the text stayed at 81–105px from the top. The next paragraph starts at 100px, so the 5px below the text overlaps it. With font-size: 0, the text's width and height became 0px, and it disappeared. Both approaches assume the parent contains no text.

Images inside links and figures also need display: block on the img

When an image is wrapped in a link like <a href="…"><img></a>, the gap was still 5px. Adding display: block to the a did not remove it: the a became 105px tall, and the gap just moved inside it. Adding display: block to the image itself brought the gap to 0px.

html
<div class="card">
  <a href="https://example.com/">
    <img src="photo.png" alt="写真の説明">
  </a>
</div>
css
.card img {
  display: block;
}

When a <figcaption> follows an image inside a <figure>, there was a 5px space between the bottom of the image and the top of the caption. Adding display: block to the image brought it to 0px. To control the space before the caption, set margin-top on the figcaption rather than relying on the gap. With margin-top: 8px, the space was 8px whether the parent text was 16px or 32px.

The gap that appears when you add a doctype

Old HTML without a doctype had no gap below an image on a line by itself. Adding <!doctype html> makes the gap appear. This is because the doctype changes the rendering mode the browser uses. MDN's explanation of quirks mode and standards mode says that the browser decides whether to render in quirks mode based on the doctype at the start of the document.

I opened a <div> containing only an image with different doctypes and got these results:

Start of document document.compatMode Gap with image only Gap with text after image
<!doctype html> CSS1Compat 5px 5px
No doctype BackCompat 0px 5px
XHTML 1.0 Transitional doctype CSS1Compat 0px 5px

Without a doctype, there was no gap in a line containing only an image. The Quirks Mode standard defines two line-height rules for quirks mode and limited-quirks mode. The first is the rule that a block containing only inline content ignores its line-height when computing the minimum line height. The second is the rule that an inline box with no text is treated as having a line-height of 0 when calculating line height. When there is text after the image, that text adds its own height to the line, so the 5px gap appeared with every doctype.

The XHTML 1.0 Transitional doctype is listed as a doctype that triggers limited-quirks mode in the HTML Living Standard's rules for choosing the rendering mode from the doctype. document.compatMode returns CSS1Compat for it, so compatMode cannot tell you whether this mode is active.

If the gap appears after you change the doctype to <!doctype html>, do not revert the doctype. Instead, fix the image or the parent CSS using the methods above.

​Posted by 86