简体   繁体   English

在HTML5中使用“ img”标签

[英]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. 曾经有人告诉我,html中的“ img”标记必须嵌套在诸如p标记之类的元素内。 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 . 好吧,如果曾经有人告诉过您,那么您是在HTML 4的上下文中被告知的。在这里查看HTML 4 Strict的DTD。

You will see that IMG is an %inline element, but the HTML BODY element can only contain %block , SCRIPT , INS , and DEL elements: 您将看到IMG%inline元素,但是HTML BODY元素只能包含%blockSCRIPTINSDEL元素:

<!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. 因此,如果您要验证HTML 4严格标准,则绝对应在P或类似名称中包含IMG Try it on a validator, you will see: 在验证器上尝试,您将看到:

document type does not allow element "IMG" here; 文档类型此处不允许元素“ IMG”; missing one of "P", "H1", "H2", "H3", "H4", "H5", "H6", "DIV", "ADDRESS" start-tag 缺少“ P”,“ H1”,“ H2”,“ H3”,“ H4”,“ H5”,“ H6”,“ DIV”,“ ADDRESS”开始标记之一

Now, in HTML 5, you can have an img element right under the body element. 现在,在HTML 5中,您可以body元素下直接拥有一个img元素。 Check out the definition of the body element here . 在此处检查body元素的定义。 It says the content model for the body element is: 它说body元素的内容模型是:

Flow content 流量内容

If you lookup Flow content, you will see that img is allowed! 如果您查找Flow内容,您将看到img被允许!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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