# Tags, Elements & Attributes

> The exact parts every piece of HTML is made of: an opening tag, content, a closing tag, and the attributes that ride inside the tag.

Subject: HTML | Lesson 3 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/elements-tags-attributes
Previous: [The Head: charset, viewport, title](https://codewithanimation.in/html/the-head-essentials.md) | Next: [HTML Entities & Special Characters](https://codewithanimation.in/html/html-entities.md)

## What it is

People use the words tag, element, and attribute interchangeably, but they are three different things. A `tag` is a single piece of markup like `<p>` or `</p>`. An `element` is the whole unit: the opening tag, the content inside, and the closing tag together. An `attribute` is extra information that rides inside the opening tag, written as `name="value"`.

So `<p class="intro">Hello</p>` is one element. `<p ...>` and `</p>` are its tags, `Hello` is its content, and `class="intro"` is an attribute on it. Once you can name those parts, every other HTML lesson is just learning which elements exist and what they mean.

## Why it matters

Almost every HTML bug a beginner hits is really a parts problem: a closing tag that was forgotten, an attribute value missing its quotes, or two elements that overlap instead of nesting. Naming the parts is what lets you read an error and know which one is wrong.

It is also the vocabulary the rest of the web speaks. Documentation, error messages, CSS selectors, and teammates all assume you know the difference between an element and an attribute, so this is the grammar the whole subject is built on.

## How it works

A normal element has three parts: an opening tag that names it, the content, and a closing tag with a slash, like `<h1>Title</h1>`. Tags are not case sensitive, but write them lowercase by habit so your markup stays consistent and readable.

Attributes go inside the opening tag, each separated by a space, written as `name="value"` with the value in quotes. Always quote the value: `title="My homepage"` is one attribute, but unquoted `title=My homepage` is read as three. Some attributes are boolean, like `disabled` or `required`, where simply being present means true and leaving it out means false.

A few elements are `void elements`: they hold no content, so they have just one tag and no closing tag, such as `<br>`, `<hr>`, `<img>`, `<input>`, `<meta>`, and `<link>`. You may see a trailing slash like `<br />`, which is allowed but not required. Elements also nest inside each other, and the rule is to close them in the reverse order you opened them. Finally, anything wrapped in `<!-- ... -->` is a comment: a note the browser ignores and never shows.

## Common mistake: Overlapping tags instead of nesting them

When you nest elements you must close them in the reverse order you opened them. Writing `<p>This is <strong>bold</p></strong>` overlaps the tags, so the browser has to guess what you meant and can render something unexpected. The correct version closes the inner element first: `<p>This is <strong>bold</strong></p>`. Think of it as last opened, first closed.

## Tip: Always quote attribute values, even when you could skip them

HTML lets you drop the quotes around a value that has no spaces, but the moment a value contains a space it breaks in confusing ways, so just quote every value all the time. Pick double quotes and stay consistent. This one habit removes a whole category of silent attribute bugs before they start.

## Try it: The anatomy of an element

Here is one element on the left. Each part lights up and names itself on the right, so you can see exactly where the tag ends, the content sits, and the attribute lives.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/elements-tags-attributes

## Check yourself

### Question 1

In <p class="intro">Hello</p>, what is the element?

- A. The whole thing: the opening tag, the content, and the closing tag together
- B. Just <p> and </p>
- C. Just the word Hello

Correct answer: A

An element is the complete unit: opening tag, content, and closing tag. A tag is a single piece of markup like `<p>`, and `class="intro"` is an attribute on the element.

- Why not B: Those are the tags. The element is the complete unit including the content.
- Why not C: That is the content. The element is the tags plus the content together.

### Question 2

Which of these is a void element with no closing tag?

- A. <img>
- B. <p>
- C. <strong>

Correct answer: A

Void elements hold no content and have a single tag with no closing tag: `<img>`, `<br>`, `<hr>`, `<input>`, `<meta>`, and `<link>`. A trailing slash like `<img />` is allowed but not required.

- Why not B: `<p>` holds content and needs a closing `</p>`.
- Why not C: `<strong>` wraps content, so it needs a closing `</strong>`.

### Question 3

Why should you wrap attribute values in quotes?

- A. An unquoted value with a space is read as several attributes instead of one
- B. Quotes change the value to uppercase
- C. Browsers refuse to load a page with unquoted values

Correct answer: A

Quotes mark where a value begins and ends. Without them, `title=My homepage` is parsed as a `title` of `My` plus two stray boolean attributes. Always quoting every value avoids this silent bug.

- Why not B: Quotes do not transform the value at all; they just delimit it.
- Why not C: The page still loads; the value is just parsed incorrectly, which is the trap.

## Remember this

- A tag is one piece of markup; an element is the opening tag, content, and closing tag together.
- Attributes ride inside the opening tag as `name="value"`; boolean ones like `disabled` are true just by being present.
- Void elements (`<img>`, `<br>`, `<hr>`, `<input>`, `<meta>`, `<link>`) have one tag and no closing tag.
- Nest by closing in reverse order (last opened, first closed); `<!-- ... -->` is an ignored comment.
