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%; }
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.
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.
That space actually is a result of descender elements in fonts. You can get rid of it in a number of ways:
vertical-align:top
rule to the image jsFiddle example font-size:0;
to the containing div jsFiddle example display:block;
to the image jsFiddle example 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With