CHAPTER 06 · HTML
Tags, elements and nesting: put each part in the right place
You can already write a complete page. Now look more closely at its building blocks. You will learn which parts need closing tags, which parts cannot contain content and how to keep one element correctly inside another.
Download example files (ZIP)What you will learn
Tell opening tags, closing tags, content and whole elements apart. Read a parent, child and sibling relationship. Nest small text elements without crossing their boundaries. Recognise void elements and distinguish them from empty containers. Understand why browser recovery does not prove the source is correct.
A tag marks a boundary
<p>Read the question first.</p>, <p> starts a paragraph and </p> ends it. The sentence between them is the content. The whole marked paragraph is an element.
<p>Read the question first.</p><p> | ||
</p> | ||
<p>Read the question first.</p> |
Nesting means putting one element inside another
<body>
<h1>Study circle</h1>
<p>Bring your notes.</p>
<p>Choose one question to explain.</p>
</body>Close the inner part before the outer part
<p>Bring <strong>your own notebook</strong> to the session.</p>- 1Open p
- 2Open strong and write the phrase
- 3Close strong
- 4Close p
These are nested boundaries. Do not cross closing tags or put a heading inside a paragraph.
Intentional error — fix it before running.
<p>Bring <strong>your own notebook</p></strong></strong> before </p>. A browser may repair the crossed structure, but its recovered result is not the source pattern you should write.
Complete example: a study-circle reminder
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Study Circle Reminder</title>
</head>
<body>
<h1>Before the study circle</h1>
<p>Bring <strong>your own notebook</strong> to the session.</p>
<p>Choose one question to explain to your partner.</p>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Not every element is allowed inside every other element
<p><h1>Notice</h1></p> is not the intended structure. Put the heading and paragraph next to each other inside body instead.
Void elements have no closing tag or child content
<meta charset="UTF-8">
<br>
<hr></br>, </hr> or </meta>. You may see <br /> in other code. For an HTML void element, that trailing slash is unnecessary and does not work like a separate closing tag. We will use <br> consistently.
<p /> does not make an ordinary self-contained empty paragraph in HTML. Use <p></p> if you really intend an empty paragraph, though repeated empty paragraphs are not a good way to create spacing.
A second complete example: breaks between parts
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Learning Desk Location</title>
</head>
<body>
<h1>Learning desk</h1>
<p>Room 8<br>Ground floor</p>
<hr>
<p>Bring one topic you would like to revise.</p>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
An empty element and a void element are not identical ideas
<p></p>. That does not make p a void element. You can still put a sentence inside p later, and its start and end tags have normal paragraph meaning.
Case, spacing and optional tags
Problem and solution: the warning spreads too far
<strong> immediately before it and </strong> immediately after it, within the same paragraph. Then read the complete sentence to check that the message is still clear.
Common mistakes and useful tips
Calling only <p>a complete paragraph element: distinguish the marker from the whole marked content.Crossing closing tags: close the inner element before the outer element. Putting a heading or another paragraph inside p: write them as separate sibling elements. Adding end tags to void elements: check the element's actual rules. Treating <p />as a self-closing HTML paragraph: do not apply void-element syntax to ordinary elements.Trusting a repaired browser view as proof of valid source: inspect both the source and the result.
Practice questions
Identify the opening tag, content and closing tag in <p>Check one example.</p>.In the nested reminder, which element is the direct parent of strong? Repair <p>Read <strong>the question</p></strong>.Does <p></p>make p a void element? Explain.Explain why the hr in the desk-location example is outside the paragraphs. Write a complete reminder page with one heading and two paragraphs. Mark only one phrase as important, then draw the parent-and-child relationships.
Check your practice answers
<p>opens the paragraph, Check one example. is the content and</p>closes it.The p containing the reminder sentence. Write <p>Read <strong>the question</strong></p>.No. It is an ordinary paragraph that currently contains no content. Void is a rule of an element type, not just an observation that it is empty. Hr is not permitted as part of p's paragraph content. It separates the surrounding content as a sibling element in body. Use the complete document pattern, place both paragraphs inside body and put strong inside one paragraph. Your drawing should show strong as that paragraph's child.
Interview questions
What does nesting mean in HTML?
How is a void element different from an ordinary empty element?
Does a trailing slash close any HTML element?
Why should you care about correct nesting when browsers recover from mistakes?
Quick revision
A tag is a marker; an element includes its content and structure. Parent, child and sibling describe containment relationships. Close inner elements before outer elements. Check permitted content instead of nesting by appearance alone. Void elements have no end tag or child content in HTML. Write lowercase names and explicit boundaries consistently while learning.