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

前端 未结 2 657
太阳男子
太阳男子 2020-12-01 03:44

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

2条回答
  •  心在旅途
    2020-12-01 04:35

    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. as short for . 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 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.

    To summarize:

    • normally, use just 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 , 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).

提交回复
热议问题