tags:

views:

1555

answers:

5

I am having problems with my image spacing when I switched to XHTML Strict DOCTYPE.

The following code - which uses Yahoo's reset stylesheet to kill off all default browser padding - leaves a gap of about 4 pixels between the two images below but ONLY when I use the strict doctype. Why is this?

It is only a problem in Chrome and Firefox. IE doesn't show a single pixel between the two images.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head>
    <link rel="stylesheet" type="text/css" 
     href="http://yui.yahooapis.com/2.6.0/build/reset/reset-min.css"&gt;
</head>

<body>

<div><img src="http://www.catfacts.org/cat-facts.jpg" border="0"/></div>
<div><img src="http://www.catfacts.org/cat-facts.jpg" border="0"/></div>


</body>
</html>
A: 

Trying to rule out common errors, I made the code pass validation by fixing no less than 8 validation errors.

Generally if a browser can't parse the document in the DOCTYPE given, results are unspecified.

It still doesn't work mind you, but here's the validating code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head>
   <link rel="stylesheet" type="text/css" 
     href="http://yui.yahooapis.com/2.6.0/build/reset/reset-min.css" />
    <title>Required</title>
</head>

<body>

    <div><img src="http://www.catfacts.org/cat-facts.jpg" alt="cat1" /></div>
    <div><img src="http://www.catfacts.org/cat-facts.jpg" alt="cat2" /></div>

</body>
</html>
Allain Lalonde
thanks - even though it doesn't work. i should get in the habit of doing things like that
Simon_Weaver
this isn't a common issue? most searches for this issue just yield advice about setting padding to 0 everywhere - which i've done
Simon_Weaver
+3  A: 

Using Firebug shows that it is the DIV that causes the spacing, rather than the image.

I set font-size: 0; for the top div and the gap goes away.

(Oddly, there is/should be an inherited font-size:0; from the body in the reset-min.css so not sure why this worked.)

Peter Boughton
well that works. i HATE it thought! is this the best i'm going to get? interstingly setting font-size:1 or font-size:1000 gives the same sized gap
Simon_Weaver
seeing that gap disappear is quite a relief. and an even BIGGER relief to see the gap on my actual site disappear. i'm very curious as to any other solutions though so for now i'll leave the question unanswered
Simon_Weaver
oh and i didn't mean i hate your answer - i just meant - well you know :-) i just reread my comment and i thought i sounded a little ungrateful!
Simon_Weaver
Don't worry - I know what you mean. :)
Peter Boughton
+5  A: 
Allain Lalonde
kinda made sense - and it works too. any known side effects of this? i guess i'll see if any of my other images look in the wrong place as i come to it. only thing i'm wondering about is if i ever have inline images in text such as bulletpoints - but then i can always use baseline there
Simon_Weaver
be sure to also read the article mentioned by ionut (https://developer.mozilla.org/en/Images,_Tables,_and_Mysterious_Gaps). it goes into great detail and talks about future fix
Simon_Weaver
+3  A: 

More information about the misterious image gaps can be found here:

https://developer.mozilla.org/en/Images,_Tables,_and_Mysterious_Gaps

Ionuț G. Stan
very thorough and helpful article. thanks
Simon_Weaver
+2  A: 

In strict doctypes, image becomes an inline element, and behaves like text. Hence you need to change its vertical-align property, or change its display property to display: block, or display:inline-block

facildelembrar