Nesting Requirements

It has always been a rule in HTML that elements should be properly nested within one another. The closing tag of a contained element should always appear before the closing tag of the element that contains it. In XHTML, this rule is strictly enforced. So be sure that your elements are nested correctly, like this:

<p>I can <em>fly!</em></p>

and not overlapping like this:

<p>I can <em>fly!</p></em>

In addition, XHTML enforces other nesting restrictions that have always been a part of the HTML specification. The XHTML DTD includes a special “Content Models for Exclusions” note that reinforces the following:

  • An a element cannot contain another a element.

  • The pre element cannot contain img, object, applet, big, small, sub, sup, font, or basefont.

  • The form element may not contain other form elements.

  • A button element cannot contain a, form, input, select, textarea, label, button, iframe, or isindex.

  • The label element cannot contain other label elements.

Get Web Design in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.