HTML Elements
An HTML document is a collection of elements. Each element tells the browser what a piece of content is. Understanding how elements are written and how they fit inside each other is the most important skill in HTML.
Anatomy of an element
<tagname>Content goes here</tagname>| Part | Example | Meaning |
|---|---|---|
| Opening tag | <h2> |
Starts the element |
| Content | Daily news |
What the element contains |
| Closing tag | </h2> |
Ends the element (note the /) |
| Element | <h2>Daily news</h2> |
All three parts together |
Nesting elements
Elements can contain other elements. This is called nesting, and it creates a tree: the <html> element contains <head> and <body>, the body contains headings and paragraphs, a paragraph can contain bold text or links, and so on.
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Animal facts</h1>
<p>An octopus has <strong>three</strong> hearts.</p>
<p>A group of flamingos is called a <em>flamboyance</em>.</p>
</body>
</html>In this example:
<html>is the parent of<body>.<body>contains three children:<h1>and two<p>elements.<strong>is nested inside the first paragraph, and<em>inside the second.
Close in the right order
Nested elements must be closed in the reverse order they were opened — like closing boxes inside boxes.
Correct: <p>This is <strong>important</strong>.</p>
Wrong: <p>This is <strong>important.</p></strong>Browsers try to repair overlapping tags, but the result can be surprising. Indenting nested elements makes the structure easy to see.
Empty (void) elements
Some elements never have content, so they have no closing tag:
| Element | Purpose |
|---|---|
<br> |
Line break |
<hr> |
Thematic break |
<img> |
Image |
<input> |
Form field |
<meta> |
Page metadata |
<link> |
Link to an external resource such as a stylesheet |
You may see them written as <br /> with a slash. That style comes from XHTML and is allowed, but in HTML5 the slash is optional and has no effect.
<p>Line one<br>Line two<br>Line three</p>
<hr>
<label>Your name: <input type="text" placeholder="Ada"></label>Don't skip closing tags
Browsers are tolerant. This often renders correctly:
<p>First paragraph
<p>Second paragraph
<p>Third paragraph…but relying on that is risky. Some closing tags are officially optional (like </p> and </li>), yet leaving them out makes your code harder to read and can cause bugs when styles or scripts expect a certain structure. Always close what you open.
Case doesn't matter — but be consistent
<P> and <p> mean the same thing; HTML tag names are case-insensitive. The universal convention is lowercase, and that's what you'll see throughout this course.
Block and inline at a glance
Elements fall into two broad display groups (you'll study them in detail later):
- Block elements such as
<p>,<h1>,<div>,<ul>start on a new line and stretch the full width. - Inline elements such as
<a>,<strong>,<em>,<span>sit inside a line of text.
<p style="background:#fde2d9">I am a block — I take the whole width.</p>
<p>I contain <span style="background:#d9ecfd">an inline span</span> and <a href="#">an inline link</a>.</p>Tip: When a page looks wrong, check your nesting first. An unclosed
<div>or<strong>is the most common cause of mysterious layout problems.
Test yourself
Which of these is correctly nested?
<p><em>Hi</p></em><p><em>Hi</em></p><em><p>Hi</em></p><p>Hi<em></p>
Which element is an empty (void) element?
<p><h1><br><strong>
What is the recommended case for tag names?
- UPPERCASE
- lowercase
- CamelCase
- It must match the file name