Web Technology IUnit 311 min read
HTML5 Semantics & Sectioning: <header>, <footer>, <nav>, <article>, <section>
Unit 3 of Web Technology I covers HTML5’s semantic elements—<header>, <footer>, <nav>, <article>, <section>—and how they improve accessibility, SEO, and document structure. Learn their definitions, use cases, and differences from generic <div>s, with real-world examples from eSewa, Daraz, and NEPSE.
TAKEAWAYS:
- Semantic HTML5 elements (
<header>,<footer>,<nav>,<article>,<section>) replace generic<div>s to describe content meaning, improving accessibility and SEO. <article>is for self-contained content (e.g., a blog post or news item), while<section>groups related content (e.g., chapters in a book).- Screen readers and search engines rely on semantic markup to interpret page structure, making websites more inclusive.
- Misusing semantic elements (e.g.,
<article>for a navigation menu) harms accessibility and SEO. - Real-world apps like eSewa’s transaction history use
<article>for each payment record, while Daraz’s product categories use<section>for grouping items. - Always validate semantic markup using tools like the W3C Validator.
Why Semantic HTML Matters
Before HTML5, developers used <div> and <span> for everything, leading to poorly structured pages. Semantic elements solve this by describing the purpose of content, not just its appearance. This helps:
- Accessibility: Screen readers announce
<nav>as a navigation menu, not just "a div." - SEO: Search engines prioritize pages with clear structure (e.g., Google favors
<article>for blog posts). - Maintainability: Code is self-documenting (e.g.,
<footer>clearly marks the page footer).
Real-World Example: eSewa’s Transaction History
When you view your eSewa transactions, each payment record is wrapped in an <article> element. This tells screen readers: "This is a standalone transaction entry"—critical for users who rely on assistive tech. Without semantics, the page would be a wall of <div>s, making navigation impossible for visually impaired users.
Core Semantic Elements
Here’s a table comparing the most important HTML5 semantic elements:
| Element | Purpose | Example Use Case | Non-Semantic Alternative |
|---|---|---|---|
<header> |
Introductory content (page title, logo, navigation) | Website header with logo and main menu | <div class="header"> |
<footer> |
Closing content (copyright, contact info, sitemap) | Footer with "© 2024 Daraz" and social media links | <div class="footer"> |
<nav> |
Major navigation block (links to other pages) | Top menu with "Home," "Products," "Cart" links | <div class="menu"> |
<article> |
Self-contained content (blog post, news item, forum post) | A single news article on NEPSE’s website | <div class="post"> |
<section> |
Thematic grouping of content (chapters, tabs, or related items) | "Electronics" and "Fashion" categories on Daraz | <div class="category"> |
<aside> |
Tangential content (sidebars, ads, related links) | "Recommended Products" sidebar on Daraz | <div class="sidebar"> |
<main> |
Primary content (excluding headers/footers) | The main body of a Pathao ride booking page | <div class="content"> |
How Semantic Elements Work: A Trace
Let’s trace how semantic markup improves a Daraz product page. Assume this structure:
sequenceDiagram
participant User
participant ScreenReader
participant Browser
User->>Browser: Loads Daraz Product Page
Browser-->>ScreenReader: Announces `<header>` as "Header: Daraz Logo + Menu"
Browser-->>ScreenReader: Announces `<nav>` as "Navigation: Electronics, Fashion"
Browser-->>ScreenReader: Announces `<article>` as "Article: Smartphone X"
Browser-->>ScreenReader: Announces `<section>` as "Section: Specifications"
Browser-->>ScreenReader: Announces `<aside>` as "Aside: Recommended Products"
Browser-->>ScreenReader: Announces `<footer>` as "Footer: Copyright"
ScreenReader-->>User: Navigates content logicallyScreen Reader Interaction with Semantic HTML (Daraz Page)<body>
<header>
<h1>Daraz</h1>
<nav>
<a href="/electronics">Electronics</a>
<a href="/fashion">Fashion</a>
</nav>
</header>
<main>
<article>
<h2>Smartphone X</h2>
<p>Price: NPR 25,000</p>
<section id="specs">
<h3>Specifications</h3>
<p>RAM: 8GB, Storage: 128GB</p>
</section>
</article>
<aside>
<h3>You May Also Like</h3>
<ul>
<li>Headphones Y</li>
<li>Tablet Z</li>
</ul>
</aside>
</main>
<footer>
<p>© 2024 Daraz. All rights reserved.</p>
</footer>
</body>
Visual: Semantic Structure of a Daraz Page
graph TD
A["Header: Logo + Navigation"] --> B["Main"]
B --> C["Article: Smartphone X"]
B --> D["Section: Specifications"]
B --> E["Aside: Recommended Products"]
B --> F["Footer: Copyright"]
caption How Semantic Markup Structures a Daraz PageKey Observations:
<header>and<footer>clearly mark the start/end of the page.<nav>explicitly labels the navigation menu for screen readers.<article>wraps the product details, while<section>groups specs.<aside>separates recommendations from the main content.
Common Pitfalls and Fixes
| Mistake | Why It’s Wrong | Correct Approach |
|---|---|---|
Using <article> for navigation |
Screen readers announce it as content. | Use <nav> for menus. |
Nesting <header> inside <article> |
Breaks semantic hierarchy. | <header> should only appear at page/article level. |
Using <section> without a heading |
Screen readers skip it. | Always include <h2>–<h6> in <section>. |
Overusing <div> for semantics |
Loses accessibility benefits. | Replace with <aside>, <figure>, etc. |
In the Real World
eSewa’s Transaction History
- Element Used:
<article>for each transaction. - Why? Each payment is self-contained (date, amount, recipient). Screen readers announce: "Article: Transaction on 2024-05-20 to Ncell."
- Impact: Users with screen readers can navigate transactions efficiently.
- Element Used:
Daraz’s Product Categories
- Element Used:
<section>for "Electronics," "Fashion," etc. - Why? Groups related products under thematic headings. Search engines index these sections separately.
- Impact: Better SEO and faster loading (browsers can lazy-load sections).
- Element Used:
NEPSE Stock Quotes
- Element Used:
<article>for each stock ticker (e.g., NABIL, NMB). - Why? Each ticker is independent data. Screen readers can focus on one at a time.
- Impact: Investors using assistive tech can track specific stocks easily.
- Element Used:
Worked Example: Kathmandu Traffic Routes
Imagine designing a webpage for Kathmandu’s traffic updates (like NTC’s real-time alerts). Use semantic HTML to structure it:
<article>
<h2>Traffic Alert: Thamel to Kantipath</h2>
<p>Heavy congestion due to roadworks. Expected delay: 45 minutes.</p>
<section id="alternative-routes">
<h3>Alternative Routes</h3>
<ul>
<li>Take Ring Road East via Putalisadak.</li>
<li>Use Bus Park to Thapathali.</li>
</ul>
</section>
</article>
Visual: Semantic Traffic Alert Structure
Why This Works:
<article>marks the alert as standalone content.<section>groups alternative routes under a heading.- Screen readers announce: "Article: Traffic Alert. Section: Alternative Routes."
Comparing Semantic vs. Non-Semantic HTML
| Feature | Semantic HTML (<article>, <nav>) |
Non-Semantic HTML (<div>) |
|---|---|---|
| Accessibility | Screen readers interpret meaning. | Treated as generic blocks. |
| SEO | Search engines index content better. | Lower ranking. |
| Maintainability | Self-documenting code. | Requires comments/classes. |
| Example | <nav><a href="/login">Login</a></nav> |
<div class="menu"><a href="/login">Login</a></div> |
Exam Tip
- Memorize the 5 Key Elements: Focus on
<header>,<footer>,<nav>,<article>, and<section>. These appear in 60% of exam questions. - Practical Questions: Expect code snippets where you must:
- Identify semantic vs. non-semantic markup.
- Fix incorrect usage (e.g.,
<article>for a menu). - Explain accessibility benefits (e.g., "Why use
<nav>for menus?").
- Real-World Scenarios: Be ready to apply semantics to:
- A bank’s loan calculator (use
<article>for each loan type). - A Pathao ride booking page (use
<nav>for "Track Ride" button). - A NEPSE stock dashboard (use
<section>for "Top Gainers/Losers").
- A bank’s loan calculator (use
- Validation Tools: Know that the W3C Validator flags missing or misused semantic elements.
- Common Exam Tricks:
- Questions may ask: "Which element would you use for a blog post’s sidebar?" → Answer:
<aside>. - "Why is
<div class="header">worse than<header>?" → Answer: Loses semantic meaning for screen readers/SEO.
- Questions may ask: "Which element would you use for a blog post’s sidebar?" → Answer:
In the real world
eSewa Transaction History: Uses
<article>for each payment record to ensure screen readers announce each transaction as a standalone entry (e.g., "Article: Payment to Ncell on 2024-05-20"). This helps visually impaired users navigate their transaction history efficiently.Daraz Product Categories: Employs
<section>for thematic groupings like "Electronics" and "Fashion". Search engines like Google index these sections separately, improving SEO and allowing lazy-loading of category pages for faster performance.NEPSE Stock Quotes: Wraps each stock ticker (e.g., NABIL, NMB) in
<article>tags. This allows investors using assistive technology to focus on individual stock updates without confusion, mirroring how<article>isolates self-contained content in semantic HTML.
Based on the TU BIM syllabus for Web Technology I (IT237), unit 3.
Discussion
Loading…