您是否需要关闭HTML中的元标记和链接标记? [英] Do you need to close meta and link tags in HTML?

查看:74
本文介绍了您是否需要关闭HTML中的元标记和链接标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在阅读某人的HTML,他从未关闭HTML头部分中的meta和link标签。代码工作正常;是关闭这些标签可选?



我认为如果一个标签没有关闭,它将会变形。

解决方案 标记必须总是由标记关闭符号> 关闭(如果我们忽略某些SGML规则名义上应用于非XHTML HTML,但从未在浏览器中实现过)。



您要表达的是元素是否需要关闭通过结束标签。答案是非XHTML HTML(包括HTML序列化中的HTML5),不需要或允许使用 meta link 元素。然而,在实践中,浏览器仅仅忽视了它们的明确的结束标签,以及> / >,如果你使用它们。 HTML5通过在HTML序列化中正式允许 / 来实现这种宽容。



在XHTML中,应用XML规则,因此每个元素都必须同时具有开始标记和结束标记,但是如果元素内容为空,则可以为这两个角色使用相同的标记,例如< meta name =foocontent =bar/> 作为< meta name =foocontent =bar >< /元> 。如果您在向具有XML(XHTML)内容类型的文档提供符合要求的浏览器时违反此规定,则根本不会显示您的文档;显示错误消息。



使用HTML内容类型( Content-Type:text / html ),因为XHTML文档几乎总是在Web上,那么浏览器实际上会应用非XHTML HTML规则。



总结:


  • 通常只使用< meta ...> ,并且没有 /

  • 如果您真的在实际应用XHTML解析的上下文中使用XHTML,请按XML规则播放(并确保您知道它们)。如果你的老板让你写< meta ... /> ,b $ b
  • 它没有用处,但它不会造成任何伤害(除非您尝试验证例如agaist HTML 4.01文档类型)。


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?

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

解决方案

A tag must 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).

What you mean to ask is whether the elements need 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 meta and link elements. 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.

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.

When using XHTML server with 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.

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. agaist HTML 4.01 doctype).

这篇关于您是否需要关闭HTML中的元标记和链接标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆