user1762554
user1762554

Reputation: 139

using the "img" tag with html5

I was once told that the "img" tag in html had to be nested within an element such as the p tag. Is this true? or was mislead?

<p><img src "img/mittromneylittleface.png" alt="drinks" /></p>

Upvotes: 0

Views: 1126

Answers (1)

Ray Toal
Ray Toal

Reputation: 88378

Well if you were once told that, you were told this in the context of HTML 4. Take a look at the DTD for HTML 4 Strict here.

You will see that IMG is an %inline element, but the HTML BODY element can only contain %block, SCRIPT, INS, and DEL elements:

<!ELEMENT BODY O O (%block;|SCRIPT)+ +(INS|DEL) -- document body -->

<!ENTITY % block 
   "P | %heading; | %list; | %preformatted; | DL | DIV | NOSCRIPT |
    BLOCKQUOTE | FORM | HR | TABLE | FIELDSET | ADDRESS">

So if you want to validate HTML 4 strict, you should definitely include the IMG inside a P or similar. Try it on a validator, you will see:

document type does not allow element "IMG" here; missing one of "P", "H1", "H2", "H3", "H4", "H5", "H6", "DIV", "ADDRESS" start-tag

Now, in HTML 5, you can have an img element right under the body element. Check out the definition of the body element here. It says the content model for the body element is:

Flow content

If you lookup Flow content, you will see that img is allowed!

Upvotes: 6

Related Questions