Lesson 7 of 51 · Module 2, lesson 1 of 9 · 4 min

Elements, tags and nesting

Read and write any HTML element, and put elements inside each other correctly.

What you'll learn

  • The parts of an element: tags, content and attributes
  • How elements go inside other elements (nesting)
  • Which elements have no closing tag

Why this matters

Everything on a web page is an element. If you can read one element, you can read any HTML page. And most beginner mistakes — text in the wrong place, a page that suddenly looks strange — come from one of two things: a missing closing tag, or elements that overlap instead of nesting.

The parts of an element

An element written as: opening tag p with the attribute lang equals "fr", the content "Bonjour!", and the closing tag slash p. The opening and closing tags are marked in blue, the attribute name in yellow, the attribute value in green, and the content in purple. The whole thing together is one element.

  • The opening tag starts the element: <p>.
  • The content is what appears on the page.
  • The closing tag ends the element. It is the same word with a slash: </p>.
  • An attribute adds extra information inside the opening tag. You will meet attributes properly in the next lesson.

Together, opening tag + content + closing tag make one element. People often say "tag" when they mean "element". That is fine in conversation, but in this course tag means only the part in angle brackets.

Elements inside elements

An element can contain other elements. This is called nesting. Here is a small recipe page:

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Soup recipe</title>
</head>
<body>
  <h1>Tomato soup</h1>
  <p>Ready in <strong>30 minutes</strong>. Serves four people.</p>
</body>
</html>

What you'll see: a large heading and a sentence. The words "30 minutes" are bold.

A browser tab titled "Soup recipe". The page shows the heading "Tomato soup" and the sentence "Ready in 30 minutes. Serves four people.", with "30 minutes" in bold.

The <strong> element (important text — you will learn it later in this module) is inside the paragraph. The paragraph is inside <body>. <body> is inside <html>. You can draw this as a tree:

A tree diagram. At the top is html. It has two children: head and body. Head contains title, which contains the text "Soup recipe". Body contains h1 and p. The h1 contains the text "Tomato soup" and the p contains the text "Ready in…". Elements are blue and text is purple.

When one element is inside another, the outer one is the parent and the inner one is the child. <body> is the parent of <h1> and <p>.

The one rule of nesting

Close the inner element before the outer one. Elements must fit inside each other like boxes — they can never overlap.

html
<!-- Correct: strong opens and closes inside p -->
<p>Ready in <strong>30 minutes</strong>.</p>

<!-- Wrong: strong is still open when p closes -->
<p>Ready in <strong>30 minutes</p></strong>

(The lines that start with <!-- are comments: notes that the browser ignores. You will learn them later in this module.)

Elements with no closing tag

A few elements have no content, so they have no closing tag. They are called void elements. You have already met one:

html
<meta charset="UTF-8">

Others you will meet soon: <br> (a line break), <hr> (a dividing line), <img> (an image) and <input> (a form field).

You may see them written with a slash at the end, like <br />. That is an older style. It still works, but you do not need the slash.

Lower case, please

<P> and <p> mean the same thing to the browser. But everyone writes tags in lower case today, and so should you. Mixed styles make code harder to read.

Try it yourself

  1. Select the recipe example above and choose Try it (or open the Try code tab).
  2. Add a second paragraph: <p>You need <strong>six tomatoes</strong>.</p>
  3. Now break it on purpose: delete </strong> in your new paragraph. What happens to the text after it?

The rest of the paragraph turns bold too, because the browser does not know where "important" ends. Put </strong> back.

Common mistakes

  • Forgetting a closing tag. Text after it takes on the wrong style or ends up in the wrong place. How to spot it: something on the page looks bold, big or indented when it should not. Fix: find the element that starts there and close it.
  • Overlapping elements, such as <p><strong>…</p></strong>. Browsers try to repair it, and the result is hard to predict. Fix: close the inner element first.
  • Forgetting the slash in the closing tag, writing <p> instead of </p>. The browser sees a second opening tag. Fix: every closing tag starts with </.

Check yourself

  1. In <h1>Tomato soup</h1>, what is the content?
  2. Is this correct: <p>Hello <strong>friend</p></strong>? Why?
  3. Name one void element.

Answers

  1. Tomato soup.
  2. No. The elements overlap. </strong> must come before </p>.
  3. For example <meta>, <br>, <hr>, <img> or <input>.

Next: you have seen lang="en" and charset="UTF-8". The next lesson explains attributes: what they are and how to write them.

Chat with us on WhatsApp