Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mystery white space underneath image tag [duplicate]

Tags:

html

css

I just changed the header image on my site from

<div style="background-image... width=1980 height=350>

to using

<img src="... style="width:100%;">

so the image would scale down which it now does...

But now I have this mysterious 10px gap or so.

I've checked the inspector in Chrome, and I just can't see what's causing the space. I've searched other posts but can't find anything that applies.

Anyone out there have any idea? Appreciate any help, Bob :)

like image 573
bob32 Avatar asked Jul 16 '15 03:07

bob32


People also ask

How do I get rid of the extra white space under an image in CSS?

Line Height Property: The CSS line-height-property can be used to set the height of the line, whose value is set to normal, by default. By setting the height of the container at 0%, the white space can be removed.

How can the gap under the image be removed in HTML?

Answer: Use the CSS display property.

How do I get rid of extra white space in HTML?

Normally, setting margin:0 and padding:0 for the html and body elements, like you have done, should be enough to remove all spacing around the page.

How do I get rid of a gap between pictures?

To remove the unwanted space between images or similar HTML elements do one of the following: Put all the elements on one line with no spaces between them. Put the closing bracket of the previous element immediately before the next element on the next line. Comment out the end of line marker (carriage return).


2 Answers

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:

  • Why is my textarea higher up than its neighbor?
like image 157
Michael Benjamin Avatar answered Sep 23 '22 02:09

Michael Benjamin


By default, IMG is an inline element. You need to set your IMG tag to be a block element, which can be accomplished with this style:

display: block; 
like image 32
Kaitebug Avatar answered Sep 21 '22 02:09

Kaitebug