一些 HTML1 结束标记是可选,即:
</HTML>
</HEAD>
</BODY>
</P>
</DT>
</DD>
</LI>
</OPTION>
</THEAD>
</TH>
</TBODY>
</TR>
</TD>
</TFOOT>
</COLGROUP>
注意:不要与禁止包含的结束标签相混淆,即:
</IMG>
</INPUT>
</BR>
</HR>
</FRAME>
</AREA>
</BASE>
</BASEFONT>
</COL>
</ISINDEX>
</LINK>
</META>
</PARAM>
注意: xhtml
与 HTML 不同。 xhtml 是 xml 的一种形式,它要求每个元素都有一个结束标记。结束标签在 html 中可以是禁止,但在 xhtml
中是强制。
可选的结束标记
- 是否理想地包含,但如果您忘记了它们,我们会接受它们,或者
- 理想地不包含,但如果您将它们放入
In, 我们会接受它们换句话说,我应该包含它们,还是不应该包含它们?
HTML 4.01 规范讨论了关闭元素标签是可选的,但没有说明是否最好包含它们,或者最好不包含它们。
另一方面,一篇关于 DevGuru 的随机文章说:
结束标签是可选的。不过,建议将其包括在内。
我问的原因是因为您知道出于兼容性原因它是可选的;如果可以的话,他们会制定这些规定(强制 | 禁止)。
换句话说:HTML 1、2、3 对这些现在可选的结束标记做了什么。 HTML 5 有什么作用?我应该做什么?
注意
HTML 中的某些元素禁止拥有结束标记。您可能不同意这一点,但这就是规范,没有争议。我问的是可选结束标签,以及其意图是什么。
脚注
1HTML 4.01
Some HTML1 closing tags are optional, i.e.:
</HTML>
</HEAD>
</BODY>
</P>
</DT>
</DD>
</LI>
</OPTION>
</THEAD>
</TH>
</TBODY>
</TR>
</TD>
</TFOOT>
</COLGROUP>
Note: Not to be confused with closing tags that are forbidden to be included, i.e.:
</IMG>
</INPUT>
</BR>
</HR>
</FRAME>
</AREA>
</BASE>
</BASEFONT>
</COL>
</ISINDEX>
</LINK>
</META>
</PARAM>
Note: xhtml
is different from HTML. xhtml is a form of xml, which requires every element have a closing tag. A closing tag can be forbidden in html, yet mandatory in xhtml
.
Are the optional closing tags
- ideally included, but we'll accept them if you forgot them, or
- ideally not included, but we'll accept them if you put them in
In other words, should I include them, or should I not include them?
The HTML 4.01 spec talks about closing element tags being optional, but doesn't say if it's preferable to include them, or preferable to not include them.
On the other hand, a random article on DevGuru says:
The ending tag is optional. However, it is recommended that it be included.
The reason I ask is because you just know it's optional for compatibility reasons; and they would have made them (mandatory | forbidden) if they could have.
Put it another way: What did HTML 1, 2, 3 do with regards to these, now optional, closing tags. What does HTML 5 do? And what should I do?
Note
Some elements in HTML are forbidden from having closing tags. You may disagree with that, but that is the specification, and it's not up for debate. I'm asking about optional closing tags, and what the intention was.
Footnotes
1HTML 4.01
发布评论
评论(14)
在某些情况下,明确的标签会有所帮助,但有时这是不必要的迂腐。
请注意,HTML 规范明确指定了何时可以省略标签,因此并不总是错误。
例如,您永远不需要
。没有人记得明确地放置
(以至于 XHTML 对此做出了例外)。
您不需要
除非您有实际搜索
的 DOM 操作脚本(那么最好显式关闭它,因为
隐含结束的规则可能会让您感到惊讶)。
嵌套列表实际上没有
会更好,因为这样就更难创建错误的
ul > 了。 ul 树。
有效:
无效:
请记住,无论您是否尝试关闭所有元素,都隐含结束标记。添加结束标签不会自动使解析更加健壮:
将解析为:
它仅在验证文档时才有帮助。
There are cases where explicit tags help, but sometimes it's needless pedantry.
Note that the HTML spec clearly specifies when it's valid to omit tags, so it's not always an error.
For example you never need
</body></html>
. Nobody ever remembers to put<tbody>
explicitly (to the point that XHTML made exceptions for it).You don't need
</head><body>
unless you have DOM-manipulating scripts that actually search<head>
(then it's better to close it explicitly, because rules for implied end of<head>
could surprise you).Nested lists are actually better off without
</li>
, because then it's harder to create erroneousul > ul
tree.Valid:
Invalid:
And keep in mind that end tags are implied whether you try to close all elements or not. Putting end tags won't automatically make parsing more robust:
will parse as:
It can only help when you validate documents.
可选的都是那些在语义上应该清楚其结束位置的内容,而不需要结束标记。
EG 每个
隐含一个
(如果其前面没有
)。
禁止的结束标记都将立即跟随其结束标记,因此必须输入
每次。
我几乎总是使用可选标签(除非我有充分的理由不这样做),因为它可以提供更具可读性和可更新性的代码。
The optional ones are all ones that should be semantically clear where they end, without needing the end tag.
E.G. each
<li>
implies a</li>
if there isn't one right before it.The forbidden end tags all would be immediately followed by their end tag so it would be kind of redundant to have to type
<img src="blah" alt="blah"></img>
every time.I almost always use the optional tags (unless I have a very good reason not to) because it lends to more readable and updateable code.
我在这里添加一些链接来帮助您了解 HTML 的历史,以便您了解各种矛盾。这不是您问题的答案,但阅读这些不同的摘要后您会了解更多。
深入了解 HTML5< 的一些摘录/a>:
I am adding some links here to help you with the history of HTML, for you to understand the various contradictions. This is not the answer to your question, but you will know more after reading these various digests.
Some excerpts from Dive Into HTML5:
这是一个有趣的推论。我的解读是,几乎任何时候可以可靠地推断出标签,该标签都是可选的。该设计表明其目的是使其快速且易于编写。
HTML 2 的 DTD 嵌入在 RFC 中,它与原始的 < a href="http://www.w3.org/MarkUp/html-spec/html.dtd" rel="noreferrer">HTML DTD,到处都有可选的开始和结束标记。
HTML 3 被放弃(由于浏览器战争)并被 HTML 3.2 取代(它旨在描述当时的 Web 状态)。
HTML 5 从一开始就致力于“铺平牛道”。
啊,现在这是主观的和有争议的:)
有些人认为显式标签由于位于读者眼前而更具可读性和可维护性。
有些人认为推断标签不会使编辑器变得混乱,因此具有更好的可读性和可维护性。
That's an interesting inference. My reading of it is that just about any time a tag could be reliably inferred, the tag is optional. The design suggests that the intention was to make it quick and easy to write.
The DTD for HTML 2 is embedded in the RFC which, along with the original HTML DTD, has optional start and end tags all over the place.
HTML 3 was abandoned (thanks to the browser wars) and replaced with HTML 3.2 (which was designed to describe the then current state of the web).
HTML 5 was geared towards "paving the cowpaths" from the outset.
Ah, now that is subjective and argumentative :)
Some people think that explicit tags are better for readability and maintainability by virtue of being in front of the readers eyes.
Some people think that inferred tags are better for readability and maintainability by virtue of not cluttering up the editor.
这个问题的答案在 W3C 工作草案中:
http://www.w3.org/TR/html5/syntax .html#syntax-tag-omission
这是风格问题。我尽量不省略结束标签,因为它可以帮助我保持严谨,不省略必要的标签。
The answer to this question is in the W3C Working Draft:
http://www.w3.org/TR/html5/syntax.html#syntax-tag-omission
It's a matter of style. I try to never omit end tags because it helps me to be rigorous and not omit tags that are necessary.
如果它是多余的,请将其删除。
如果它有某种目的(即使是看似微不足道的目的,例如安抚您的 IDE 或安抚您的眼睛),请将其保留。
在明确定义的规范中很少会看到不影响行为的可选项目。当然,“评论”除外。但 HTML 规范与其说是设计规范,不如说是当前主要实现状态的文档。因此,当 HTML 中的某个项目是可选的并且它似乎没有任何用途时,我们可能会猜测可选性质仅仅是特定浏览器中的怪癖的文档。
查看上面链接的 HTML-5 规范 RFC 部分,您会发现可选标签奇怪地与注释的存在相关联!这应该告诉你作者并没有戴设计帽子。相反,他们在主要实现中玩“记录怪癖”的游戏。所以我们在这方面不能太认真地对待规范。
所以,解决办法就是:别出汗。继续做真正重要的事情。 :)
If it is superfluous, leave it out.
If it serves a purpose (even a seemingly trivial purpose, such as appeasing your IDE or appeasing your eyes), leave it in.
It's rare in a well-defined spec to see optional items that do not affect behavior. With the exception of "comments", of course. But the HTML spec is less of a design spec, and more of a document of the state of current major implementations. So when an item is optional in HTML and it seems to serve no purpose, we may guess that optional nature is merely documentation of a quirk in specific browser.
Looking at the HTML-5 spec RFC section linked above, you see that the optional tags are strangely linked to the presence of comments! That should tell you that the authors are not wearing design hats. They are instead playing the game of "document the quirks" in major implementations. So we can't take the spec too seriously in this respect.
So, the solution is: Don't sweat it. Move on to something that actually matters. :)
我认为最好的答案是包含结束标签以提高可读性或错误检测。但是,如果您有大量生成的 HTML(例如数据表),则可以通过省略可选标签来节省大量带宽。
I think the best answer is to include closing tags for readability or error detection. However, if you have lots of generated HTML (say, tables of data), you could save significant bandwidth by omitting optional tags.
我的建议是省略大多数可选的关闭标签以及所有可以忽略的可选属性。许多 IDE 会抱怨,因此您可能无法忽略其中一些,但通常对于较小的文件大小和较少的混乱来说更好。如果您有代码生成器,那么肯定会省略结束标记,因为您可以从中获得一些良好的尺寸减小。通常,无论哪种方式都并不重要。
但当事情确实重要时,就采取行动。在我最近的一些工作中,通过消除开放标记的大部分生成的结束属性和冗余值属性,我能够将渲染的 HTML 大小从 1.5 MB 减少到 800 KB,其中元素的文本与价值。我有大约 200 个标签。我可以完全以其他方式实现这一点,但这会需要更多工作($$$),因此这使我可以轻松地使页面更具响应性。
出于好奇,我发现如果删除不需要的属性周围的引号,我可以节省 20 KB,但我的 IDE (Visual Studio) 不喜欢它。我还惊讶地发现 ASP.NET 生成的非常长的 ID 占我文件的 20%。
我们可以获得严格有效的 HTML 任何相关部分的想法从一开始就被误导了,所以请采取最适合您和您的客户的方式。我见过或使用过的大多数工具都会说它们生成 xhtml,但它们并不是真正 100% 工作,而且严格遵守也没有任何好处。
My recommendation is that you omit most optional close tags, and all optional attributes that you can get away with. Many IDEs will complain so you may not be able to get away with omitting some of these but it is generally better for smaller file size and less clutter. If you have code generators definitely omit end tags there because you can get some good size reduction from it. Usually it doesn't really matter one way or the other.
But when it does matter then act on it. On some recent work of mine I was able to reduce the size of my rendered HTML from 1.5 MB to 800 KB by eliminating most of the generated end and redundant value attributes for the open tag, where the text of the element was the same as the value. I have about 200 tags. I could implement this some other way entirely, but that would be more work ($$$), so this allows me to easily make the page more responsive.
Just out of curiosity I found that if I removed quotes around attributes that didn't need them I could save 20 KB, but my IDE (Visual Studio) doesn't like it. I also was surprised to find that the really long ID that ASP.NET generates account for 20% of my file.
The idea that we could ever get any relevant fraction of HTML strictly valid was misguided in the first place, so do whatever works best for you and your customers. Most tools that I have ever seen or used will say they generate xhtml, but they don't really work 100%, and there isn't any benefit to strict adherence anyway.
就我个人而言,我是 XHTML 的粉丝,并且像 ghoppe 一样,“我尽量不省略结束标记,因为它可以帮助我保持严谨,而不是省略必要的标记。”
但是,
如果您有意使用 HTML 4.n,则不能说包含它们可以更轻松地使用文档,因为与有效性相对的格式良好的概念是一个 XML 概念,并且当您您禁止某些关闭标签。所以唯一的问题是有效性......如果没有它们仍然有效......你最好节省带宽,不是吗?
Personally, I'm a fan of XHTML and, like ghoppe, "I try to never omit end tags because it helps me to be rigorous and not omit tags that are necessary."
but
If you're deliberately using HTML 4.n, one can't argue that including them makes it easier to consume the document, as the notion of well-formedness as opposed to validity is an XML concept, and you lose that benefit when you forbid certain close tags. So the only issue becomes validity... and if it's still valid without them... you might as well save the bandwidth, no?
使用结束标签可以更轻松地处理片段,因为它们的行为不依赖于同级元素。仅这个理由就应该足够令人信服了。有人再处理单一的 html 文档吗?
Using end tags makes dealing with fragments easier because their behaviour is not dependant on sibling elements. This reason alone should be compelling enough. Does anyone deal with monolithic html documents anymore?
在某些大括号语言(例如 C#)中,如果 if 语句只有两行长,则可以省略 if 语句周围的大括号。例如...
if ([条件])
[代码]
但你不能这样做...
if ([条件])
[代码]
[code]
第三行不会成为 if 语句的一部分。它会损害可读性,并且很容易引入错误,并且很难发现。
出于同样的原因,我关闭了所有标签。像 img 标签这样的标签仍然需要关闭,只是不需要单独的关闭标签。
In some curly bracket languages like C#, you can omit the curly braces around an if statement if its only two lines long. for example...
if ([condition])
[code]
but you can't do this...
if ([condition])
[code]
[code]
the third line won't be a part of the if statement. it hurts readability, and bugs can be easily introduced, and be difficult to find.
for the same reasons, i close all tags. tags like the img tag do still need to be closed, just not with a separate closing tag.
做任何你认为能让代码更具可读性和可维护性的事情。
就我个人而言,我总是倾向于关闭
和
,但我永远不会打扰
。
Do whatever you feel makes the code more readable and maintainable.
Personally I would always be inclined to close
<td>
and<tr>
, but I would never bother with<li>
.如果您正在编写 HTML 解析器,那么解析包含可选结束标记的 HTML 或不包含可选结束标记的 HTML 会更容易吗?我认为存在的可选结束标签会让事情变得更容易,因为我不必推断结束标签应该在哪里。
出于这个原因,我总是包含可选的结束标签 - 理论上我的页面可能会渲染得更快,因为我为浏览器的 HTML 解析器创建了更少的工作。
If you were writing an HTML parser, would it be easier to parse HTML that included optional closing tags, or HTML that doesn't? I think the optional closing tags being present would make it easier, as I wouldn't have to infer where the closing tag should be.
For that reason, I always include the optional closing tags - on the theory that my page might render faster, as I'm creating less work for the browser's HTML parser.
对于禁止的关闭类型,请使用如下语法:
使用
/>
来关闭 xml 中接受的标签For forbidden closing types use syntax like:
<img />
With the/>
to close the tag which is accepted in xml