简体   繁体   中英

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>

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM