HTML elements, tags, and attributes
Your roadmap for this lesson
- The anatomy of an HTML element
- Opening tags, closing tags, and void elements
- How attributes add extra information
- Nesting elements in the right order
Short answer
A tag is the label in angle brackets, like <p>. An element is the full package: the opening tag, the content, and the closing tag. An attribute is extra information written inside the opening tag, like href on a link. Learn these three words and you can read almost any HTML.
Anatomy of an element
<p class="intro">Welcome to our shop.</p>
<p class="intro">is the opening tag.class="intro"is an attribute: name, equals sign, value in quotes.Welcome to our shop.is the content.</p>is the closing tag. It has a forward slash.
Together, all four parts make one element.
Void elements
Some elements have no content, so they have no closing tag. These are called void elements or empty elements.
<img src="logo.png" alt="Shop logo">
<br>
<hr>
<input type="email">
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
You may see <br /> with a slash in older code. In modern HTML the slash is optional.
Attributes add details
Attributes always go in the opening tag. Some common ones:
| Attribute | Used on | What it does |
|---|---|---|
href | <a> | The link destination |
src | <img> | The image file |
alt | <img> | Text that describes the image |
id | Any element | A unique name for one element |
class | Any element | A group name for styling |
lang | <html> | The page language |
Some attributes are boolean. Writing the name alone switches them on, like required or disabled.
Nesting: elements inside elements
Elements can sit inside other elements. This is called nesting. The rule: close the inner element before the outer one.
<!-- Correct -->
<p>This is <strong>very</strong> important.</p>
<!-- Wrong -->
<p>This is <strong>very</p> important.</strong>
The outer element is the parent. The inner one is the child. This family tree is called the DOM, the Document Object Model.
Try it: fix the nesting
One element below is nested wrong and one attribute is missing quotes. Fix both, then add a title attribute to the link.
Live preview
Quick quiz
Common mistakes
- Forgetting the closing tag. A missing </p> or </a> can make the rest of the page behave strangely.
- Using the same id twice. An id must be unique on the page. Use class for groups.
- Skipping quotes on attribute values. Values with spaces break without quotes. Always quote them.
- Putting attributes in the closing tag. Attributes only work in the opening tag.
Your action
Tick each item when you can do it. Your progress saves on this device.
Key terms
- Element
- An opening tag, its content, and its closing tag together.
- Opening tag
- The tag that starts an element, like <p>.
- Closing tag
- The tag that ends an element, like </p>.
- Void element
- An element with no content or closing tag, like <img>.
- Attribute
- A name and value in the opening tag that adds information.
- Boolean attribute
- An attribute that is on when present, like required.
- Nesting
- Placing elements inside other elements.
- DOM
- Document Object Model, the tree of elements the browser builds.
Questions beginners ask
Are HTML tags case sensitive?
No, but lowercase is the standard. Write <p>, not <P>.
Single or double quotes for attributes?
Both work. Double quotes are the most common choice. Stay consistent.
What is the difference between id and class?
An id names one unique element. A class can be shared by many elements.
What happens if I make a mistake?
Browsers are forgiving and try to guess. The page may still show, but it can break in odd ways. A validator catches these errors, as you will learn in Module 10.
Related lessons
Need a website built or fixed while you practice? The team at Remar Digital can help.