Html 为什么是 <div class="clear"></div> 而不是 <div class="clear"/>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4059781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why <div class="clear"></div> instead of <div class="clear"/>?
提问by Edward Tanguay
I just realized that:
我才意识到:
<div class="clear"/>
after a series of floating divs causes layout havoc, while
在一系列浮动 div 导致布局破坏之后,而
<div class="clear"></div>
works fine.
工作正常。
Can anyone explain this?
谁能解释一下?
This is the CSS:
这是CSS:
div.clear {
clear:both;
}
回答by bobince
<div class="clear"/>
If you are serving an XHTML page as text/html
, browsers aren't reading it using a real XML parser. They're using a plain old HTML parser which doesn't know about self-closing tags. To an HTML4 parser, that's just an open tag with some weird punctuation in it. (If browser parsers really used the rules of SGML, it would be some other undesirable thing completely, but they don't.)
如果您将 XHTML 页面作为 提供text/html
,浏览器不会使用真正的 XML 解析器读取它。他们使用的是一个普通的旧 HTML 解析器,它不知道自关闭标签。对于 HTML4 解析器来说,这只是一个带有一些奇怪标点符号的开放标签。(如果浏览器解析器真的使用了 SGML 的规则,那完全是其他一些不受欢迎的事情,但他们没有。)
Until IE9 becomes widespread, we won't be able to serve general-purpose web pages as application/xhtml+xml
, which is what's required to make the browser use real XML parsing rules. Until that time, if you write XHTML documents you will need to make your documents also backwards-compatible with plain HTML, which means alwaysand onlyusing self-closing syntax on elements that are declared to have an EMPTY
content model. (img
, br
, etc.)
在 IE9 普及之前,我们将无法将通用网页作为 提供服务application/xhtml+xml
,而这是使浏览器使用真正的 XML 解析规则所必需的。在那之前,如果您编写 XHTML 文档,您将需要使您的文档也向后兼容纯 HTML,这意味着始终且仅对声明为具有EMPTY
内容模型的元素使用自关闭语法。(img
,br
等)
There are more rules about what you have to do to write HTML-compatible XHTML, but that's the most important one. Appendix Cof the XHTML 1.0 standard is a list of the differences; it looks a bit complicated, but many of the points address features you don't want to be using anyway, and some are now irrelevant as they're talking about being ancient browsers like Netscape 4. The practice of putting a space before />
is no longer required by anything in common use, for example.
关于编写与 HTML 兼容的 XHTML 必须执行的操作有更多规则,但这是最重要的规则。XHTML 1.0 标准的附录 C是差异列表;看起来有点复杂,但是很多点都解决了您无论如何都不想使用的功能,有些现在已经无关紧要,因为他们谈论的是像 Netscape 4 这样的古老浏览器。 之前放置空格的做法/>
是不例如,任何常用的东西都需要更长的时间。
回答by Donut
According to the HTML 4.01 spec, Section 7.5.4, the <div>
tag requires a closing tag.
按照HTML 4.01规范,第7.5.4节中,<div>
标签需要关闭标签。
回答by NickG
<div class="clear"/>
This syntax isn't valid HTML/XHTML. Any tag which cancontain content cannotbe self closing (even if no content is required in the tag. Therefore content containing tags like div, span, p etc, can never be self closing tags. Conversely, tags which cannot contain content like <br />
should always be self closing.
此语法不是有效的 HTML/XHTML。任何可以包含内容的标签都不能自闭合(即使标签中不需要任何内容。因此,包含 div、span、p 等标签的内容永远不能是自闭合标签。相反,不能包含类似内容的标签<br />
应该总是自我关闭。