Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 122 ยท Getting Started

Elements and Tags

Understand the difference between tags and elements, how to nest elements correctly, and which special elements never need a closing tag.

So far you have used tags without thinking too hard about them. In this lesson you will learn the exact vocabulary, tag, element, nesting, and the few rules that keep your HTML clean and predictable.

These rules are small, but they matter more than they seem. Browsers never show you an error message for broken HTML; they quietly repair it and carry on. Knowing the rules is how you make sure the page the browser builds is the page you meant to write.

Tags versus elements

People often use the two words as if they mean the same thing, but there is a useful difference:

  • A tag is the label itself: <p> is an opening tag and </p> is a closing tag.
  • An element is the complete package: the opening tag, the content and the closing tag together.
HTML
<p>Learning HTML is fun.</p>

In this line, <p> and </p> are tags, and the whole line is a paragraph element. The text between the tags is the element's content. The opening tag can also carry attributes, which you will meet in the next lesson.

Why does the difference matter? Because the browser does not keep your tags. It reads them, creates element objects and throws the tags away. Everything you style with CSS or change with JavaScript is an element, never a tag. When you open the Elements panel in DevTools, you are looking at elements that the browser built from your tags.

Nesting elements

Elements can contain other elements. This is called nesting, and it is how every page is built: the body contains a heading and paragraphs, a paragraph contains some emphasized words, and so on.

Nested elements
  • <strong> marks text as important. Browsers show it in bold.
  • <em> marks text that should be stressed when spoken. Browsers show it in italics.

Notice that the browser chooses the bold and italic look for you. The element says what the text means; the appearance is just a default that CSS can change. Text Formatting covers these and similar elements in detail.

Close in reverse order

When elements are nested, the one opened last must be closed first. Think of it like stacking boxes inside boxes: you have to close the inner box before you can close the outer one.

HTML
<!-- Correct: em is opened inside strong, so it closes first -->
<p><strong>Read <em>this</em></strong></p>

<!-- Wrong: the closing tags overlap -->
<p><strong>Read <em>this</strong></em></p>

Browsers will try to repair overlapping tags, but the repair can create extra elements you never wrote, and those extra elements then pick up your CSS in surprising ways. Clean nesting guarantees the same result everywhere.

Tip: let your editor help. Most code editors highlight the matching tag when you click on one, and many can insert the closing tag automatically as soon as you type the opening one. Together with consistent indentation, that makes overlapping tags very hard to write.

Not everything can go everywhere

Each element has rules about what it may contain, called its content model. Most of the time common sense is enough: a list contains list items, a table contains rows. One rule catches nearly every beginner, though: a paragraph cannot contain block-level structure such as another paragraph, a list, a heading or a <div>.

When the browser meets one of those inside a <p>, it does not nest it. It closes the paragraph early instead. Watch it happen:

A paragraph closes itself

The dashed border shows the real paragraph: it ends just before the list, and "See you there!" ends up outside it. The browser even turns the stray </p> into a second, empty paragraph, which is why the console counts two. The fix is to keep the list outside the paragraph:

HTML
<p>Things to bring:</p>
<ul>
  <li>Water</li>
  <li>Snacks</li>
</ul>
<p>See you there!</p>

You will learn more about which elements stack as blocks and which flow inside a line in Block and Inline Elements.

Parents, children and siblings

Developers describe nested elements with family words:

  • The element that contains another is its parent.
  • An element directly inside another is a child.
  • Elements that share the same parent are siblings.
  • Everything inside an element, at any depth, is a descendant; everything above it is an ancestor.
HTML
<body>
  <h1>Pets</h1>
  <p>Cats are <em>excellent</em> companions.</p>
</body>

Here <body> is the parent of <h1> and <p>, which are siblings. The <em> is a child of the paragraph and a descendant of the body. You will use these words constantly once you start styling pages with CSS, where a selector such as p em means "every em that is a descendant of a p".

Void elements

A few elements have no content at all, so they have no closing tag. These are called void elements. The most common ones are:

Element Purpose
<br> A line break inside text
<hr> A thematic break between sections
<img> An image
<input> A form field
<meta> Information about the page (in the head)
<link> A connection to another file, such as CSS
Void elements

Void elements get everything they need from their attributes. An image has no content between tags; its src says which file to show and its alt gives a text alternative.

You might see void elements written as <br /> with a slash at the end. That older style comes from XHTML. It still works, but in modern HTML the slash is optional and has no effect. Never write a closing tag such as </br> or </img>: void elements cannot have one, and browsers handle the mistake inconsistently.

Don't use <br> for spacing. A line break belongs inside text where the break is part of the content, such as lines of a poem or an address. To separate paragraphs, use separate <p> elements. To add space, use CSS margins.

Optional closing tags

You may come across HTML where some closing tags are missing on purpose, such as list items without </li>. The standard does allow a few closing tags to be left out because the browser can always infer them. It is legal, but it makes code harder to read and edit. In this course, always write closing tags for elements that have content.

Case and whitespace

  • Tag names are not case-sensitive: <P> and <p> mean the same thing. Lowercase is the universal convention, so stick to it.
  • Browsers collapse whitespace. Several spaces, tabs or line breaks in your text show up as a single space. Your indentation never reaches the page.
Whitespace collapses

The <pre> element is the exception: it keeps whitespace exactly as typed, which is useful for code samples and text art. You will meet it again in Computer Code.

Unknown elements

What happens if you misspell a tag, such as <paragraf>? The browser does not complain. It creates an unknown element with no special meaning, shows its content as plain inline text and moves on. That is why typos in tag names can go unnoticed for a long time. Your editor's syntax coloring and an HTML validator are your best defenses.

Comments

Anything between <!-- and --> is a comment. The browser ignores it completely, so comments are perfect for notes to yourself or your team, or for temporarily switching off a piece of markup while you test something.

HTML
<!-- TODO: add a photo of the team here -->
<p>Meet the team.</p>

Comments cannot be nested, so you cannot comment out a block that already contains a comment.

Remember: comments are hidden on the page but not secret. Anyone can read them using View page source, so never put passwords or private information in them.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In <em>really</em>, which part is the element?

    A tag is just the label; the element is the whole package from the opening tag to the closing tag.
  2. 2. Which line is nested correctly?

    The element opened last must close first, like boxes inside boxes.
  3. 3. What does the browser do when it finds a <ul> inside a <p>?

    A paragraph can't contain block structure like lists, so the browser ends the paragraph early. Keep lists outside paragraphs.
  4. 4. Which of these is a void element (it has no closing tag)?

    <br> has no content, so it has no closing tag. <img>, <hr> and <input> are other common void elements.
  5. 5. Is an HTML comment a safe place to keep a password?

    Comments are hidden on the page but are part of the file every visitor downloads.

Summary

  • A tag is a label such as <p>; an element is the opening tag, content and closing tag together.
  • Elements can be nested; always close the innermost element first.
  • Each element has a content model: for example, a paragraph closes itself when it meets a list or a heading.
  • Nested elements are described as parents, children, siblings, descendants and ancestors.
  • Void elements like <br>, <hr> and <img> have no closing tag.
  • Write tag names in lowercase, remember that whitespace collapses (except in <pre>), and use comments for notes.

Next: Attributes, which give elements extra information such as where a link goes or which image to show.