Strict DOCTYPE affecting spacing between images

余生颓废 提交于 2019-12-05 14:06:17
Allain Lalonde

Using Peter's answer as a start the following fixes the problem:

img { vertical-align: bottom }

The reason this works is that the default for vertical-align is baseline, which equates to the part of the text "above the line" where the dangly bits hang down (lower case g, q, etc all hang below this baseline).

So in order to leave room it was leaving 4px of space for these overhangs.

Hope that made sense.

Edit: Visual aid from source site


(source: brightlemon.com)

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

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

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.)

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

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">
<html xmlns="http://www.w3.org/1999/xhtml">

<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>

The display:inline-block solution definitely did not work for me.

The vertical-align:bottom solution did work, but with one caveat: depending on the situation, there were images that I had to instead set to vertical-align:top

Switching to loose standards instead of strict worked for me... kept the IE formatting that I required, but eliminated the odd image spacing in Firefox and Chrome.

https://developer.mozilla.org/en/Gecko%27s_Almost_Standards_Mode

Used the line:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!