Free HTML tutorial

HTML Elements: Tags, Nesting and Void Elements

Learn the difference between HTML tags and elements, how to nest elements correctly, and which common elements are void.

Tags and elements

An HTML element is made from tags and its content. Most elements have an opening tag and a closing tag around the content.

<p>This text is inside a paragraph element.</p>

Here, <p> is the opening tag, the text is the content, and </p> is the closing tag.

Nest elements in the right order

Elements can contain other elements. Close the inner element before closing its parent.

<p>Read the <strong>important</strong> instructions.</p>

Void elements

Void elements cannot contain child content and do not have closing tags. Common examples include <br>, <img>, <input> and <meta>.

Write explicit closing tags

Some elements have optional end tags under specific rules, but browsers may recover differently from malformed markup. Include the closing tags required for each element and nest them correctly.

Common element mistakes

Close nested elements in the reverse order from which they open. If a paragraph contains emphasized text, close the emphasis before closing the paragraph. Void elements such as <img> and <input> do not get end tags; in HTML, a slash before the final angle bracket is optional and does not turn a void element into an XML-style self-closing element.

<p>A correctly nested <strong>phrase</strong> ends here.</p>
<img src="badge.png" alt="New member">

Practice: build a small profile

  1. Make an <article> element for one profile.
  2. Add a heading and a paragraph inside it.
  3. Add an image with descriptive alt text, then check that every non-void element is closed in the right order.

You can type the example into the online HTML editor and compare the result with the source. For the complete list of HTML elements and their content rules, see the MDN HTML elements reference.

Open the element demo (opens in a new tab)