你需要关闭 HTML 中的 meta 和 link 标签吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19506028/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:41:07  来源:igfitidea点击:

Do you need to close meta and link tags in HTML?

htmltags

提问by bcollins

I was just reading somebody's HTML who never closed meta and link tags in the HTML head section. The code worked fine; is closing these tags optional?

我只是在阅读某人的 HTML,他们从未关闭 HTML 标题部分中的元和链接标签。代码运行良好;关闭这些标签是可选的吗?

I thought it would be malformed if a tag was not closed.

我认为如果标签没有关闭,它会格式错误。

回答by Jukka K. Korpela

A tagmust always be closed by the tag close symbol >(if we ignore certain SGML rules that nominally apply in non-XHTML HTML but were never implemented in browsers).

一个标签必须始终由标签关闭符号关闭>(如果我们忽略非XHTML HTML通常适用于某些SGML的规则,但从来没有在浏览器中实现)。

What you mean to ask is whether the elementsneed to be closed by end tags. The answer is that non-XHTML HTML (including HTML5 in HTML serialization), no end tag is required or allowed for metaand linkelements. In practice, however, browsers just ignore explicit end tags for them, as well as the cargo-cult /before >, if you use them. And HTML5 makes this permissiveness a rule by even formally allowing the /in HTML serialization, too.

您要问的是元素是否需要由结束标记关闭。答案是非 XHTML HTML(包括 HTML 序列化中的 HTML5),metalink元素不需要或不允许结束标记。然而,在实践中,浏览器只会忽略它们的明确结束标签,以及/之前的 cargo-cult >,如果你使用它们的话。HTML5 甚至正式允许/in HTML 序列化,从而使这种许可成为规则。

In XHTML, XML rules apply, so every element, without exception, must have both a start tag and an end tag, but the same tag may be used for both roles if the element content is empty, e.g. <meta name="foo" content="bar"/>as short for <meta name="foo" content="bar"></meta>. If you violate this when serving a document with an XML (XHTML) content type to a conforming browser, then your document is not displayed at all; an error message is shown instead.

在 XHTML 中,XML 规则适用,因此每个元素,无一例外,都必须同时具有开始标记和结束标记,但是如果元素内容为空,则相同的标记可以用于两个角色,例如<meta name="foo" content="bar"/>,缩写为<meta name="foo" content="bar"></meta>。如果您在向符合标准的浏览器提供具有 XML (XHTML) 内容类型的文档时违反了这一点,则您的文档根本不会显示;而是显示错误消息。

When using an XHTML server with the HTML content type (Content-Type: text/html), as XHTML documents almost always are on the web, then browsers will actually apply the non-XHTML HTML rules.

当使用具有 HTML 内容类型 ( Content-Type: text/html)的 XHTML 服务器时,由于 XHTML 文档几乎总是在 Web 上,因此浏览器实际上将应用非 XHTML HTML 规则。

To summarize:

总结一下:

  • normally, use just <meta ...>with no /
  • if you are really using XHTML in a context where XHTML parsing is actually applied, play by XML rules (and make sure you know them)
  • if your boss tells you to write <meta ... />, do so; it's not useful, but it causes no harm (except if you try to validate e.g. against the HTML 4.01 doctype).
  • 通常,只<meta ...>使用没有/
  • 如果您真的在实际应用 XHTML 解析的上下文中使用 XHTML,请遵守 XML 规则(并确保您了解它们)
  • 如果你的老板让你写<meta ... />,就照做;它没有用,但不会造成任何伤害(除非您尝试针对 HTML 4.01 文档类型进行验证)。

回答by Sebsemillia

It depends on the doctype. HTML5 doesn't need the closing. XHTML does.

这取决于文档类型。HTML5 不需要结束。XHTML 可以。

In HTML5, so-called void elements (elements that can't have content) don't need the closing, as they are self-closing. But it is still valid if you close them..

在 HTML5 中,所谓的 void 元素(不能有内容的元素)不需要关闭,因为它们是自关闭的。但是如果你关闭它们它仍然有效..

Read more about it here: void-elements

在此处阅读更多相关信息:void-elements