A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

Don’t allow nested headings


I propose to change the HTML spec for heading elements <h1>, <h2> etc. to no longer allow nested headings, i.e. heading tags nested inside other heading tags.

Currently it is allowed to have nested headings, e.g.
<h1>Top level <h2>First nested level<h2>Second nested level</h2></h2></h1>

Reason for this suggestion:

  • This doesn’t make sense from a semantic point of view
  • I can not think of a single use case where headings should be nested inside each other
  • Current implementation causes problems: The following page is well known for missing all the heading end tags which causes all sub headings to be nested inside each other, on some systems this even causes the browser to crash: Embroidery Trouble Shooting Guide

Suggested change:

  • Change the spec to not allow nested headings. Open for suggestions what exactly is allowed inside headings, but I suggest to base it on <p> element with some additional elements allowed inside headings.
  • Change browser implementation of headings. All heading opening tags should auto-close any heading that is already open

So the following code

<h1>Top level <h2>sub heading</h2></h1>

would show two parsing errors (missing </h1> end tag before <h2> and orphaned </h1> end tag after </h2>) and would be treated as

<h1>Top level</h1> 
<h2>sub heading</h2>

It’s already defined so. The definition of heading elements says the content model only allows phrasing content, which doesn’t have headings.


Hi @ssollinger. Along with what @myakura pointed out about the conformance rules, the other thing to keep in mind is that if somebody intentionally puts <h1>Top level <h2>sub heading</h2></h1> into a document, they’re not going to end up with what a DOM that’s like they’d expect, because the HTML parsing algorithm causes it to go into the DOM as <h1>Top level </h1><h2>sub heading</h2> anyway.

You can confirm that using the DOM inspector in a browser or using Live DOM Viewer