Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysterious padding/margin appears after image in strict mode

Tags:

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?

like image 329
John Avatar asked Jun 19 '11 20:06

John


People also ask

How do you stop padding from increasing width?

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.

What is between padding and margin in the CSS box model?

Border. The border is the layer of the CSS box model that sits between margin and padding.

What is the difference between padding and margin?

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.


1 Answers

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.

like image 87
James Montagne Avatar answered Sep 28 '22 21:09

James Montagne