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
- 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:
<!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.
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:
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.
<!-- 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:
<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
- Select the recipe example above and choose Try it (or open the Try code tab).
- Add a second paragraph:
<p>You need <strong>six tomatoes</strong>.</p> - 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
- In
<h1>Tomato soup</h1>, what is the content? - Is this correct:
<p>Hello <strong>friend</p></strong>? Why? - Name one void element.
Answers
Tomato soup.- No. The elements overlap.
</strong>must come before</p>. - 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.



