For as long as I've been validating web pages, I never realized that the closing </head> tag was optional in HTML. I feel as if I've missed the boat at times.

While I'm all for trimming excess code, I just cannot for the life of me strip out closing tags because they are optional in HTML. I'd be concerned that something, somewhere would not parse the document correctly if I removed the closing </head> element.

Are my concerns unfounded?

Dani AI

Generated

Good question, . The short reality: modern HTML parsers will accept many omitted end tags (including </head>), but that is a parser convenience rather than a best practice. and rightly flagged concerns about standards and parsing — here are the practical details and rules to use going forward.

Common end tags that the HTML parser can infer include html, head, body, li, dt, dd, p, option, optgroup, colgroup, thead, tbody, tfoot, tr, td, and th (the spec lists them precisely). For the authoritative reference see the HTML living standard’s optional-tags section: WHATWG HTML living standard: optional tags.

Why this can bite: omitted tags change how fragments are re-parsed. Server-side templates, fragment concatenation, innerHTML manipulations, client-side templating, or tools that process the file as XML/ XHTML can create invalid nesting or hard-to-find layout bugs. Example (browsers will autocorrect this, but it can be surprising when fragments are stitched together):

<ul>
  <li>Item one
  <li>Item two
</ul>

Practical guidance: keep full end tags in source for clarity and maintainability; if payload size matters, run an automated minifier in the build pipeline rather than hand-editing tags. Add a linter/validator step (for example, the W3C validator or an HTML linter) to catch unintended structure changes. Document team style: if you ever serve content as XML (application/xhtml+xml) or use XML tooling, require well-formed markup with all end tags. Following that approach preserves readability while avoiding subtle runtime or tooling failures.

Recommended Answers

All 4 Replies

Your concerns are valid. All tags should be closed (must be closed in XHTML).

Don't feel that you missed the boat; that was probably a boat you were better off missing anyway. It's good to get into the habit of writing correct code; breaking the habit of writing bad code is difficult.

Depending on your DOCTYPE declaration, this may raise concerns. As PKern states, closing tags are required in xhtml.

Although trimming access code saves you in bandwidth usage, this could make the difference between forcing the browser to go into quirks mode or standards mode. Browsers perform better in standards mode because the parsing requires less overhead. You can find out more about this by google-ing "Quirks Mode vs. Standards Mode".

I.e. Trimming code benefits you; good code benefits the user. If done correctly, trimming code benefits you and the end user.

If you want to save bandwidth by cutting out access code, you can start by cutting out unnecessary white space. This helps dramatically, while still preserving code correctness.

this is the first time i m hearing of tags being optional, and luckily too.
i m way too used to closing tags and if i had know that sooner, i most probably would of not closed a lot of tags and become a sloppy programmer.

all tags musty closed

Thanx,
Sree

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.