Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DIV height based on child image height adds few extra pixels at the bottom

Tags:

Why does the parent div of the image have a few extra pixels at the bottom. How can I remove the pixels without hard code the parent div height.

http://jsfiddle.net/6x8Dm/

HTML

<div class="wrapper">     <div class="column">         <img src="http://www.lorempixel.com/200/200/" />     </div>     </div>   

CSS

.wrapper {     width:200px;     margin:0 auto; } .column {     width:100%;     background:#cc0000; }  img {     width:100%; } 
like image 646
Adrian Florescu Avatar asked Oct 06 '13 18:10

Adrian Florescu


People also ask

Why is div bigger than image?

The reason behind your issue is that you did not specify the width of the container but, in the same time, you set a width: 100%; for the image.

How do I make my Div take 100% height of parent DIV?

container div has two parent elements: the <body> and the <html> element. And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.


2 Answers

That space actually is a result of descender elements in fonts. You can get rid of it in a number of ways:

  • add a vertical-align:top rule to the image jsFiddle example
  • add font-size:0; to the containing div jsFiddle example
  • add display:block; to the image jsFiddle example
like image 101
j08691 Avatar answered Oct 13 '22 04:10

j08691


One way is by setting display:block on the img, causing it to fill the parent.

jsFiddle here - it works.

img {     width:100%;     display:block; } 

Alternatively, if you don't like that approach, you can also change the vertical alignment, as the default is baseline.

like image 23
Josh Crozier Avatar answered Oct 13 '22 05:10

Josh Crozier