Mystery white space underneath image tag [duplicate]

Look at this line of text. Notice there are no letters that breach the baseline.

Now look at the following sentence:

By just crossing the bridge he probably got away.

Note the letters j, g, p and y. These letters, known in typography as descenders, breach the baseline.

enter image description here

Source: Wikipedia.org

The default value of the vertical-align property is baseline. This applies to inline-level elements.

Your img is inline-level by default and, like text, span, input, textarea and other inline boxes, is aligned to the baseline. This allows browsers to provide the space necessary to accommodate descenders.

Note that the gap is not created by margin or padding, so it’s not easy to detect in developer tools. It’s a slight elevation of content from the container’s bottom edge resulting from baseline alignment.

Here are several ways to handle this:

  1. Apply vertical-align: bottom to the img tag. In some cases bottom won’t work, so try middle, top or text-bottom.

  1. Switch from display: inline to display: block.

  1. Adjust the line-height property on the container. In your code reference (since removed due to linkrot), line-height: 0 did the trick.

  1. Set a font-size: 0 on the container. You can restore the font-size on the child element directly, if necessary.

Related:

Leave a Comment