Tags, elements, and correct nesting
Understanding the difference between a tag and an element makes it easier to read source and diagnose malformed markup.
✓ What you will learn in this topic
- Explain the purpose of Tags, elements, and correct nesting in a real HTML document.
- Choose and apply the elements shown in both examples.
- Recognize the specific mistake described in this lesson.
Concept explanation
A tag is the markup between angle brackets. An element includes its start tag, content, and usually an end tag. Elements can contain other elements, forming a tree. When an element is nested, close the inner element before its parent. Browsers try to recover from mistakes, but the repaired tree may not match your intention and can confuse scripts or assistive technology.
Syntax
Open the parent first, then children; close children first, then their parent.
Example 1: a small page
The strong element is nested inside the paragraph and closes before p.
<p>Bring a <strong>water bottle</strong>.</p>Expected result: The browser displays one sentence with water bottle emphasized as important.
Example 2: a practical page
The article contains its heading and paragraph; em marks vocal emphasis on Saturday.
<article>
<h2>Workshop update</h2>
<p>The <em>Saturday</em> session starts at 10:00.</p>
</article>Expected result: A small announcement shows a heading and a sentence with Saturday emphasized.
Common mistakes
Keep nesting properly ordered and do not write closing tags for void elements such as img, input, and meta.
Best practices
Quote attribute values, use unique ids, prefer classes for reusable styling hooks, and keep comments useful rather than leaving old content in the source.
Practice exercise
Build a short announcement with a class, a unique id, one nested emphasis element, one image with alt text, and a concise source comment.
Lesson summary
- Elements form a nested document tree; close each child before its parent.
- Quote attribute values, use unique ids, prefer classes for reusable styling hooks, and keep comments useful rather than leaving old content in the source.
Knowledge check
In what order should nested elements be closed?
Show the explanation
Close the innermost child first, then close each parent in reverse opening order.
Practical example
html<p>Bring a <strong>water bottle</strong>.</p>
Expected result:
The browser displays one sentence with water bottle emphasized as important.
Hands-on practice task
Build a short announcement with a class, a unique id, one nested emphasis element, one image with alt text, and a concise source comment.
In this section
Other lessons in HTML Elements and Attributes
Broaden your knowledge