Well-formed element

Source: Wikipedia, the free encyclopedia.

In

SGML, HTML, and XML, a well-formed element is one that is either a) opened and subsequently closed, or b) an empty element, which in that case must be terminated; and in either case which is properly nested so that it does not overlap
with other elements.

For example, in HTML: <b>word</b> is a well-formed element, while <i><b>word</i> is not, since the bold element <b> is not closed.

In XHTML, and XML, empty elements (elements that inherently have no content) are terminated by putting a slash at the end of the "opening" (only) tag, e.g. <img />, <br />, <hr />, etc. In HTML 4.01 and earlier, no slash is added to terminate the element. HTML5 does not require one, but it is often added for compatibility with XHTML and XML processing.

In a well-formed document,

  • all elements are well-formed, and
  • a single element, known as the root element, contains all of the other elements in the document.

For example, the code below is not well-formed HTML, because the

strong
elements overlap:

<!-- WRONG! NOT well-formed HTML! -->
<p>Normal <em>emphasized <strong>strong emphasized</em> strong</strong></p>
<!-- Correct: Well-formed HTML. -->
<p>Normal <em>emphasized <strong>strong emphasized</strong></em> <strong>strong</strong></p>
<p>Alternatively <em>emphasized</em> <strong><em>strong emphasized</em> strong</strong></p>

In

Document Type Definition
).