Coder Perfect

Optional closing tags in HTML: Include or exclude?


Some HTML1 closing tags, for example:


Note: This is not to be confused with closing tags that are not allowed to be used, such as:


Note that xhtml is not the same as HTML. Every element must have a closing tag in xhtml, which is a type of xml. In html, a closing tag is optional, but in xhtml, it is required.

Are the concluding tags optional?

To put it another way, should I include them or exclude them?

The HTML 4.01 specification mentions that ending element tags are optional, but it doesn’t specify whether it’s better to use them or not.

On the other hand, according to a random DevGuru article:

The reason I ask is that you already know it’s optional for compatibility reasons; and if they could, they would have made them (required | banned).

To put it another way, what did HTML 1, 2, and 3 do with these now-obsolete ending tags? What is the purpose of HTML 5? And what should I do?

Some HTML elements are not allowed to have closing tags. You may disagree, but it is the standard, and it is not open to discussion. I’m curious about optional closing tags and their purpose.

1HTML 4.01

Asked by Ian Boyd

Solution #1

Explicit tags can be useful in some situations, but they can also be a source of unnecessary pedantry.

It’s worth noting that the HTML spec defines when omitting tags is acceptable, so it’s not always an error.

/body>/html>, for example, is never required. Nobody ever remembers to use the tbody> tag (to the point that XHTML made exceptions for it).

Unless you have DOM-manipulating scripts that really search head>, you don’t require /head>body> (in which case it’s preferable to shut it explicitly, because rules for implied end of head> could surprise you), you don’t need /head>body>.

Nested lists are really better off without /li> because it’s more difficult to generate an incorrect ul > ul tree that way.





Also, regardless of whether you try to close all elements or not, end tags are assumed. Adding end tags won’t make parsing any more reliable:

<p>foo <p>bar</p> baz</p>

will parse as:

<p>foo</p><p>bar</p> baz

When you validate documents, it can only benefit.

Answered by Kornel

Solution #2

All of the optional ones should be semantically obvious where they terminate without the requirement for the end tag. E.G. each implies a if there isn’t one right before it.

Because the forbidden end tags would all be immediately followed by their end tags, having to put img src=”blah” alt=”blah”>/img> every time would be a bit redundant.

I nearly always use optional tags (unless there’s a compelling reason not to) since it makes the code more legible and maintainable.

Answered by aslum

Solution #3

I’ve included some resources to assist you understand the history of HTML and the numerous inconsistencies. This isn’t the answer to your inquiry, but after reading these digests, you’ll know more.

The following are some excerpts from Dive Into HTML5:

Answered by Srikar Doddi

Solution #4

That’s an intriguing conclusion. My interpretation is that a tag is optional almost every time it may be properly inferred. The design shows that the goal was to make writing quick and simple.

The HTML 2 DTD is incorporated in the RFC, which has optional start and end tags all over the place, just like the original HTML DTD.

HTML 3 was deprecated (due to the browser wars) and HTML 3.2 was introduced (which was designed to describe the then current state of the web).

HTML 5 was designed from the start to “pave the cowpaths.”

Ah, now we’re talking about something subjective and debatable:)

Some argue that explicit tags are preferable for readability and maintainability because they are visible to the reader.

Inferred tags, according to some, are superior for readability and maintainability because they don’t clog up the editor.

Answered by Quentin

Solution #5

The W3C Working Draft has the answer to this question:

It’s a matter of personal taste. End tags are something I strive to avoid omitting because it forces me to be more thorough and avoid omitting tags that are required.

Answered by ghoppe

Post is based on