views:

1087

answers:

6

In the following web page, there is a gap of a few pixels between the image and the div. (I've tested in Firefox 3 and Safari 4.)

How can I close the gap?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"&gt;

<html>
<head>
    <title>Test Page</title>
    <style type="text/css" media="screen">
        body
        {
            background-color: black;
        }

        img 
        {
            width: 250px; 
            height: 70px; 
            border: 0; 
            margin: 0; 
            padding: 0;
        }

        div
        {
            background-color: white; 
            border: 0; 
            margin: 0; 
            padding: 0;
        }

    </style>
</head>

<body>

<img alt="Stack Overflow Logo" src="http://is.gd/lEfE"&gt;
<div>text</div>

</body>
</html>
+5  A: 

Delete the line break between your image tag and the div tag.

Daniel A. White
That shouldn't be the solution, but, sadly, it is.
sblundy
This doesn't work in Safari. Guffa's answer, however, does.
Chuck
Doesn't work in Firefox either.
Patrick McElhaney
+2  A: 

Remove the newline in the HTML between the <img> line and the <div> line.

Why yes, it is annoying that HTML treats newlines as spaces.

Chad Birch
+1  A: 

Change:

<img alt="Stack Overflow Logo" src="http://is.gd/lEfE"&gt;
<div>text</div>

To:

<img alt="Stack Overflow Logo" src="http://is.gd/lEfE"&gt;&lt;div&gt;text&lt;/div&gt;
Andrew
+14  A: 

The image is an inline element, so it's placed on the base line of a text line. The gap is the distance between the base line and the bottom of the text line (I.e. the space needed below the base line for hanging characters like "g" and "j").

Make the image a block element, and the gap goes away:

 display: block;
Guffa
This seems like the best solution to me. Granted, now you have to worry about floating and other annoying shit when you want to put something to the right of that logo.
Stuart Branham
+1 for a good solution and a good description of why it works the way it does.
Chuck
There are of course other ways of keeping the image from being treated as a text element, like putting it in another div. Making it float will also turn it into a block element. Using position:relative; to turn it into a layer may also work.
Guffa
+3  A: 

Add display: block to your <img> either in css or on the style attribute.

Edit Guffa beat me to the above.

You can also wrap the <img> with a <div> which technically speaking in this case you should do anyways. Only block level elements can be descendants of <body> if I remember the HTML specs right. You then have the flexibility of adding more images inside that div (as long as there is no white-space between the close of the <img> and the </div> you should be good to go.

Josh
I think that it's valid to have inline elements in body in HTML, but not in XHTML. Anyhow it's generally problematic to mix inline elements and block elements inside any block element.
Guffa
A: 

display block didn't work for me but this does.
Give it a negative margin value - Valid CSS
..................................

img {
width : 250px;
height : 70px;
border : 0;
margin-top : -11px;
margin-left : -22px;
padding : 0;
}

....................................
adjust as necessary. ...