Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image inside div has extra space below the image

Tags:

html

css

image

Why in the following code the height of the div is bigger than the height of the img ? There is a gap below the image, but it doesn't seems to be a padding/margin.

What is the gap or extra space below image?

#wrapper {    border: 1px solid red;    width:200px;  }  img {    width:200px;  }
<div id="wrapper">    <img src="http://i.imgur.com/RECDV24.jpg" />  </div>

Image with a gap or white space under it

like image 930
Misha Moroshko Avatar asked Apr 27 '11 12:04

Misha Moroshko


People also ask

Why is there extra space at the bottom of my div?

If you try to put an image inside a <div> element that has borders, you will see an extra white space (around 3px) at the bottom of image. It happens because image is an inline-level element so browser adds some whitespace under the baseline to adjust other inline elements.

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 do I remove the space under an image in a table cell?

In IE7, if you have a table cell with an image displayed in it, you may have noticed a small space underneath the image in the table cell. This space occurs in IE7 but not Firefox. This space can be removed by setting the style of the image to be 'block' rather than 'inline'. This is demonstrated in style-test.


1 Answers

By default, an image is rendered inline, like a letter so it sits on the same line that a, b, c and d sit on.

There is space below that line for the descenders you find on letters like g, j, p and q.

Demonstration of descenders

You can:

  • adjust the vertical-align of the image to position it elsewhere (e.g. the middle) or
  • change the display so it isn't inline.

div {    border: solid black 1px;    margin-bottom: 10px;  }    #align-middle img {    vertical-align: middle;  }    #align-base img {    vertical-align: bottom;  }    #display img {    display: block;  }
<div id="default">  <h1>Default</h1>    The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt="">  </div>    <div id="align-middle">  <h1>vertical-align: middle</h1>    The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt=""> </div>        <div id="align-base">  <h1>vertical-align: bottom</h1>    The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt=""> </div>    <div id="display">  <h1>display: block</h1>    The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt="">  </div>

The included image is public domain and sourced from Wikimedia Commons

like image 144
Quentin Avatar answered Sep 23 '22 21:09

Quentin