180

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.

2

4 Answers 4

216

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 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 <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).
10
  • 5
    using /> caused a problem where google bot did not recognise meta robots tag. strange!!!
    – DevZer0
    May 11, 2015 at 10:50
  • 25
    I find the term “cargo-cult” inappropriate in this context. Some people close those elements, simply because they repress that XHTML did not prevail.
    – Marcus
    Jan 17, 2016 at 12:29
  • 12
    Pedant alert! "Cargo cult" is not hyphenated in normal practice, but only when using the entire phrase as an adjective to something else (i.e., "cargo-cult programming", "cargo-cult / before >"). Just thought I'd clarify to avoid muddying the internet with more confusion. </pedant_alert> : ) Jun 24, 2016 at 21:34
  • 2
    @DevZer0 - are you sure the slash was the cause? I would expect Google to be flexible with things like that.
    – Simon East
    Nov 16, 2016 at 23:11
  • 4
    Isn't making your HTML consistent with XHTML rules a reason to include the / before >? Mar 18, 2018 at 17:54
42

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

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..

Read more about it here: void-elements

1
  • 9
    HTML5 has two serializations, and one of them (XHTML serialization) makes end tags mandatory. Oct 22, 2013 at 5:58
-1

In HTML, you generally need to close meta tags, but you do not need to close link tags.

Meta tags typically provide metadata about the HTML document, and they are self-closing tags, which means they don't have a closing tag. For example:

<meta charset="UTF-8">
<meta name="description" content="This is a description">

As for link tags, these are used to link external resources like stylesheets and are also self-closing. For example:

<link rel="stylesheet" type="text/css" href="styles.css"/>

In both cases, the closing tag is not required because these tags are self-contained and don't enclose any content. They are meant to provide information or reference external resources, so they don't have a corresponding closing tag.

1
  • why is what you're saying inconsistent with itself?
    – starball
    Nov 5 at 11:00
-2

Meta tags are normally closed wi this ending tag > in stead of /> such as

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 3, 2022 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.