PDA

View Full Version : img tag not validating


seanruss
12-06-2007, 10:33 PM
Is an img not a block level element? They won't validate when they are in the body and not wrapped in a div.

This WON'T validate:
<!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" lang="en" xml:lang="en">

<head><title>test - no div</title></head>

<body>

<img alt="test" src="#" />

</body>

</html>


This WILL:

<!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" lang="en" xml:lang="en">

<head><title>test - with div</title></head>

<body>

<div><img alt="test" src="#" /></div>

</body>

</html>


Why do I have to wrap the image in a div to validate? It disgusts me.

Majoracle
12-06-2007, 10:50 PM
It's just a rule (one of oh-so many) of XHTML Strict. <img> tags can't be directly inside the <body> tags. They need to be in some sort of container.

seanruss
12-06-2007, 10:54 PM
Thanks. I was trying to design my site with markup kept to a minimum. I guess I'll just have to wrap it all in a div. Lame.

Excavator
12-06-2007, 10:55 PM
Hello seanruss,
No need to be disgusted...
If you want it to be valid then back off of your DocType a little. XHTML 1.0 Transitional validates.

seanruss
12-06-2007, 11:04 PM
Ya, I thought about that, but I'm really committed to keeping it strict, more than keeping my code pretty.

VIPStephan
12-06-2007, 11:24 PM
Ya, I thought about that, but I'm really committed to keeping it strict, more than keeping my code pretty.

I like your attitude. :)
I wouldn’t worry too much about that one div. Most sites have a wrapping div that is used to set overall properties like page width, centering, font family, etc. And that one div really won’t hurt.

Note: You also cant’t put inline form elements directly inside the <form></form> element either. They need to be put in a block-level element.