I created a new document both with xhtml 1.0 and html 4.01 STRICT just to isolate this. All I have in its body is:
<div style="border: blue 3px solid;">
<img src="testimage.jpg" width="800" height="400">
</div>
The result is normal except there's a 5px space below the image that goes away if I change the doctype to transitional. It also goes away if I set display: block to the image.
You can see the result yourself here (I know the white space on the right is normal since its a block element): http://i52.tinypic.com/2prd1jd.jpg
I tried setting margin/padding to 0, even this:
*
{
margin: 0; padding: 0;
}
but it's still the same.
Can anyone please explain this behavior?
To avoid the width or height getting increased or decreased when using CSS properties like margin , padding , etc, we can use the CSS property called box-sizing and set its value to border-box on the element in CSS.
Border. The border is the layer of the CSS box model that sits between margin and padding.
The tabular difference between Padding and Margin. The outer space of an element i.e. margin is the space outside the border. The inner space of an element i.e.padding is space inside the element's border. It can be negative or any float number.
It has to do with vertical alignment of the image. Try this:
img{
vertical-align: top;
}
and the space will go away.
To clarify, if you put some text next to the image you will see the issue. The image is aligning with the base of the text but letters like y and g will go below that line. The extra space is for those overhanging letters.
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