🎉
Lesson Complete!
You understand HTML elements and nesting. Now let's explore attributes!
🌐 Phase 1 · Web Foundations 🟢 Beginner Module 02

HTML Elements

⏱ 20 min read 📖 6 sections 🧩 5-question quiz 🏆 1 challenge
Course Progress14% complete
Elements are the building blocks of every webpage. An element is the complete unit: opening tag + content + closing tag. Understanding element types, nesting rules, and block vs inline behaviour is fundamental to writing valid HTML.

What is an HTML Element?

Beginners often use the words "tag" and "element" interchangeably — but they mean different things. A tag is just the angle-bracket notation: <p> or </p>. An element is the full structure: the opening tag, the content inside, and the closing tag together.

HTMLElement anatomy
<!-- This is a TAG (opening) -->
<p>

<!-- This is a complete ELEMENT -->
<p>Hello, world!</p>

Every element has a content model — a set of rules about what it is allowed to contain. Some elements can hold text and other elements. Some can only hold specific child elements. Some hold nothing at all. Understanding content models is what separates valid HTML from broken HTML.

💡
Tag vs Element
When someone says "add a paragraph tag" they usually mean add a paragraph element — both tags plus the content. The distinction matters when you're debugging: a missing closing tag breaks the element, not the opening tag alone.

Block vs Inline Elements

Every HTML element has a default display behaviour. The two most important categories are block-level and inline.

📦
Block-level
Takes up the full width of its parent. Always starts on a new line. Can contain other block elements and inline elements. Example: div, p, h1.
↔️
Inline
Takes only as much width as its content. Flows within surrounding text — no new line. Example: span, a, strong, em.
Block ElementsInline Elements
div — generic block containerspan — generic inline container
p — paragrapha — anchor / link
h1h6 — headingsstrong — bold importance
ul, ol — listsem — italic emphasis
section, article — semanticimg — image (inline by default)
🔍
CSS can override display type
These are default behaviours. You can change any element to display as block, inline, or flex with CSS. But always choose the semantically correct element first, then style it — not the other way round.

Void (Self-Closing) Elements

Some elements have no content and no closing tag. These are called void elements (or self-closing elements). They cannot wrap text or child elements — they represent a single piece of content or metadata on their own.

🖼️
<img>
Embeds an image from a src URL
<br>
Line break within text content
<hr>
Thematic horizontal divider
⌨️
<input>
Form input field (text, checkbox, etc.)
🔗
<link>
Links external resources (CSS files)
📋
<meta>
Metadata about the document
HTMLVoid element syntax
<!-- HTML5 syntax — no closing slash needed -->
<img src="photo.jpg" alt="A scenic view">
<br>
<hr>
<input type="text" name="email">

<!-- XHTML style (trailing slash) — valid but unnecessary in HTML5 -->
<img src="photo.jpg" alt="A scenic view" />
⚠️
Don't add a closing tag to void elements
Writing <br></br> or <img></img> is invalid HTML. Void elements genuinely cannot have closing tags — the browser will treat the </br> as an unknown tag and produce unexpected results.

Nesting Rules

HTML elements can be placed inside other elements — this creates parent, child, and sibling relationships. The element that contains another is the parent. The element inside is the child. Two elements at the same level sharing a parent are siblings.

The golden rules of nesting:

Block inside block — OK
A div can contain a p. A section can contain a div. Block containers are designed for this.
Inline inside block — OK
A p can contain a strong or em. A div can contain a span. This is normal and expected.
Block inside inline — INVALID
You cannot put a div or p inside a span or a. Browsers will try to auto-fix this but the results are unpredictable.
HTMLValid vs invalid nesting
<!-- WRONG — block inside inline -->
<p>
  <div>This is invalid HTML.</div>
</p>

<!-- CORRECT — inline inside block -->
<p>
  This paragraph has <strong>important text</strong> inside it.
</p>

<!-- CORRECT — block inside block -->
<div>
  <p>First paragraph.</p>
  <p>Second paragraph.</p>
</div>
💡
Tags must close in the right order
If you open <div><p> you must close them as </p></div> — innermost first. Crossing tags like <div><p></div></p> is invalid and will confuse the browser's parser.

The Most Common HTML Elements

HTML has over 100 elements, but you'll use about 30 of them for 95% of all web pages. Here's a quick reference:

ElementCategoryPurpose
h1h6HeadingSix levels of headings for document structure
pTextParagraph of text
divContainerGeneric block-level container
spanContainerGeneric inline container
aLinkHyperlink to another page or anchor
imgMediaEmbed an image (void element)
ul / olListUnordered / ordered list
liListIndividual list item
formFormContainer for user input fields
inputFormText field, checkbox, button, etc. (void)
buttonFormClickable button
tableTableTabular data container
headerSemanticIntroductory content or navigation area
navSemanticNavigation links block
mainSemanticPrimary content of the page
footerSemanticFooter content, links, copyright
sectionSemanticThematic grouping of content
articleSemanticSelf-contained content (blog post, card)
asideSemanticTangentially related content (sidebar)

Semantic vs Generic Elements

Generic elements like <div> and <span> are just boxes with no built-in meaning. They're perfectly valid for layout and styling hooks, but they tell browsers, screen readers, and search engines nothing about what the content actually is.

Semantic elements carry meaning. When you write <nav>, you're declaring "this block contains navigation links." When you write <article>, you're saying "this block is a self-contained piece of content." That meaning matters in three important ways:

Accessibility
Screen readers navigate by landmarks like nav, main, and footer — giving blind users a page map.
🔍
SEO
Search engines extract meaning from article, h1, and section to rank and display content accurately.
🛠️
Maintainability
Code with semantic elements is easier for other developers (and future you) to read and understand at a glance.
HTMLGeneric vs Semantic
<!-- Generic — technically works, but meaningless -->
<div class="nav">
  <div class="nav-item">Home</div>
</div>

<!-- Semantic — immediately clear what this is -->
<nav>
  <a href="/">Home</a>
</nav>
🎯
Rule of thumb
Ask yourself: "Is there a semantic element that describes what this content IS?" If yes, use it. Only fall back to div or span when no semantic element fits — for example, a pure styling wrapper with no inherent meaning.
🧩 Quick Check — Lesson 5
5 questions · instant feedback · no penalty for wrong answers
1. Which of these is a block-level element?
2. What is a void element?
3. Which nesting is INVALID HTML?
4. What is the difference between an element and a tag?
5. Which element is most appropriate for navigation links?
🏆
Quiz Complete!
Great work on HTML elements. Now tackle attributes in Lesson 6!
🏆
Lesson 5 Challenge
Code exercise · 15 min
Create an HTML page with proper nesting.

Build a <div> container that holds:

• An <h2> heading
• Two <p> paragraphs
• An <ul> unordered list with exactly 3 <li> items

Inside one of the paragraphs, use <strong> and <em> for emphasis within the text. Keep all block-level elements as containers and inline elements only inside paragraph text.
Show hints
  • Start with <div> as the outer wrapper
  • Add the <h2> as the first child of the div
  • Place both <p> tags after the heading, still inside the div
  • Add <ul> last — each list item must be wrapped in <li></li>
  • Remember: <strong> and <em> go inside <p>, not outside it
← HTML Structure
Lesson 5 of 35HTML Fundamentals · Module 2