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

A tag is a marker written with angle brackets. In <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.

Think of a labelled folder. The label tells you what the folder is for; the folder also holds information. In a similar way, a tag names a role, while the element includes the content that has that role. The comparison helps you remember the distinction, but tags are written markers, not physical objects.

HTML fragment — part of a document
<p>Read the question first.</p>
PartExampleMeaning
Opening tag<p>Begin this paragraph
ContentRead the question first.The paragraph's text
Closing tag</p>End this paragraph
Whole element<p>Read the question first.</p>The complete paragraph

Closing tags use the same name as their opening tags, with a slash before the name. Do not add attributes to a closing tag. Extra information belongs in the start tag, as you will practise in Chapter 7.

Nesting means putting one element inside another

You have already used nesting: head and body are inside html, and the paragraphs are inside body. Nesting means that one element's content includes another element.

An element directly containing another is its parent. The contained element is its child. Elements that share a parent are siblings. These names describe relationships; they do not change when CSS changes the appearance.

HTML fragment — part of a document
<body>
  <h1>Study circle</h1>
  <p>Bring your notes.</p>
  <p>Choose one question to explain.</p>
</body>

The h1 and the two paragraphs are children of body. They are siblings of each other. Head is not shown in this fragment because we are looking only at the body portion.

Close the inner part before the outer part

Suppose a paragraph contains a warning. We can place a strong element inside it. Strong marks content as important; the text-formatting chapter will explain it in detail.

HTML fragment — part of a document
<p>Bring <strong>your own notebook</strong> to the session.</p>

The strong element starts inside the paragraph, so it must finish before the paragraph ends. Read the boundaries from outside to inside and then back out. Like opening a small box inside a larger box, finish the inner box before closing the outer one.

Open outside to inside; close inside to outside
  1. 1Open p
  2. 2Open strong and write the phrase
  3. 3Close strong
  4. 4Close p

These are nested boundaries. Do not cross closing tags or put a heading inside a paragraph.

An incorrect fragment would cross the boundaries:

Example fragment — use inside a complete program

Intentional error — fix it before running.

<p>Bring <strong>your own notebook</p></strong>

This is a deliberate study error. Move </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

nested-reminder.html
<!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>
Browser previewDocument title: Study Circle Reminder

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.

The visible sentence still reads naturally. Only the important words are inside strong. The paragraph is its parent. Notice that indentation helps you see the document structure, but the correct tags determine the actual boundaries.

Try moving the important phrase to a different part of the same paragraph. Keep the entire strong element inside p, save and inspect the result.

Not every element is allowed inside every other element

Nesting is not permission to put anything anywhere. HTML gives each element rules about the content it may contain. Those rules are called its content model. You do not have to memorise every model now, but you should check new elements as you learn them.

A paragraph contains text and suitable text-level elements, such as strong. Do not place another paragraph, an h1 or a table inside p. For example, <p><h1>Notice</h1></p> is not the intended structure. Put the heading and paragraph next to each other inside body instead.

This is why a rule such as all large blocks can contain all small blocks is unreliable. An element's permitted content and its CSS display are different things. We will study display behaviour separately.

Void elements have no closing tag or child content

A void element cannot contain child content in HTML and has no end tag. You already know meta. Other examples include br, hr, img and input. Each has its own job; this chapter is about their shared writing rule.

HTML fragment — part of a document
<meta charset="UTF-8">
<br>
<hr>

Do not write </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.

Do not generalise that trailing slash to every element. <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

Br means a line break within content. Hr marks a change of subject. Chapter 9 will explain when to choose each one.

desk-location.html
<!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>
Browser previewDocument title: Learning Desk Location

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.

The br belongs inside the location paragraph. The hr sits between the paragraphs, not inside a paragraph. Meta, br and hr do not have closing tags. The ordinary p elements do.

An empty element and a void element are not identical ideas

An ordinary element can happen to have no content: <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.

Void describes an element's HTML rules. Empty may simply describe its current content. Keeping these ideas separate prevents the mistake of assuming that every empty-looking element uses the same syntax.

Case, spacing and optional tags

HTML tag names for built-in elements are case-insensitive in ordinary HTML syntax. For example, P and p identify the same paragraph element. We use lowercase consistently because it makes mixed examples easier to read. This does not mean every attribute value or file path ignores case.

Some end tags can legally be omitted under particular rules. Our beginner examples write them explicitly so that the boundaries remain visible. Do not assume the same rule applies to all elements or to XML, which uses different syntax rules.

Source indentation is a reading aid. Do not line up content by adding many spaces or empty tags; that is a separate layout problem for CSS.

Problem and solution: the warning spreads too far

Problem: a student wants only a few words to be important, but the source leaves strong open until after another paragraph.

Solution: decide the exact phrase first. Put <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.

Do not fix this by adding random closing tags at the end of the page. Match each opening tag with its intended closing tag and check which element contains which.

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

  1. Identify the opening tag, content and closing tag in <p>Check one example.</p>.
  2. In the nested reminder, which element is the direct parent of strong?
  3. Repair <p>Read <strong>the question</p></strong>.
  4. Does <p></p> make p a void element? Explain.
  5. Explain why the hr in the desk-location example is outside the paragraphs.
  6. 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
  1. <p> opens the paragraph, Check one example. is the content and </p> closes it.
  2. The p containing the reminder sentence.
  3. Write <p>Read <strong>the question</strong></p>.
  4. 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.
  5. Hr is not permitted as part of p's paragraph content. It separates the surrounding content as a sibling element in body.
  6. 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?

It means one element is contained within another. The outer element is the parent, and a directly contained element is its child. The nesting must follow each element's content rules.

How is a void element different from an ordinary empty element?

A void element, such as br, cannot contain child content and has no end tag in HTML. An ordinary element such as p can be empty now but still supports content and an end tag.

Does a trailing slash close any HTML element?

No. A slash before the final angle bracket does not make ordinary HTML elements self-closing. Void elements need no closing tag, and our examples omit that unnecessary slash.

Why should you care about correct nesting when browsers recover from mistakes?

Recovery can produce a different structure from the one intended. Correct source is easier to maintain, inspect and use with assistive tools. A visible result alone is not a complete correctness check.

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.