Essential HTML Elements and Semantic Markup

Week 1, Day 2 - Lecture 2

What is Semantic HTML?

Imagine reading a book without chapters, headings, or paragraphs - just one long stream of text. Semantic HTML gives meaning and structure to your content, making it understandable for both humans and machines (like search engines and screen readers).

graph TD A[Non-Semantic HTML] --> B[div soup] A --> C[Generic elements] A --> D[No meaning] E[Semantic HTML] --> F[Meaningful tags] E --> G[Clear structure] E --> H[Better accessibility] style A fill:#fdd,stroke:#333,stroke-width:2px style E fill:#dfd,stroke:#333,stroke-width:2px

Why Semantic HTML Matters

Document Structure Elements

Think of these elements as the main sections of a newspaper or magazine:

graph TD A[body] --> B[header] A --> C[nav] A --> D[main] A --> E[aside] A --> F[footer] D --> G[article] D --> H[section] style A fill:#f9f,stroke:#333,stroke-width:2px style B fill:#9cf,stroke:#333,stroke-width:2px style C fill:#fc9,stroke:#333,stroke-width:2px style D fill:#cf9,stroke:#333,stroke-width:2px style E fill:#c9f,stroke:#333,stroke-width:2px style F fill:#9fc,stroke:#333,stroke-width:2px

Header Element

<header>
    <h1>Company Name</h1>
    <nav>
        <!-- Navigation menu -->
    </nav>
</header>

Used for introductory content, typically containing site logo, navigation, and search.

Nav Element

<nav>
    <ul>
        <li><a href="#home">Home</a></li>
        <li><a href="#about">About</a></li>
        <li><a href="#contact">Contact</a></li>
    </ul>
</nav>

Defines a section of navigation links. Not all links need to be in a nav element!

Main Element

<main>
    <h1>Page Title</h1>
    <article>
        <!-- Main content -->
    </article>
</main>

Contains the main content of the page. There should only be one <main> per page.

Article Element

<article>
    <h2>Blog Post Title</h2>
    <p>Post content...</p>
    <footer>
        <p>Posted on <time datetime="2024-01-01">January 1, 2024</time></p>
    </footer>
</article>

Self-contained content that could be distributed independently (blog posts, news articles, forum posts).

Section Element

<section>
    <h2>Our Services</h2>
    <p>We offer a variety of services...</p>
</section>

Groups related content together. Always include a heading!

Aside Element

<aside>
    <h3>Related Articles</h3>
    <ul>
        <li><a href="#">Article 1</a></li>
        <li><a href="#">Article 2</a></li>
    </ul>
</aside>

Content tangentially related to the main content (sidebars, pull quotes, ads).

Footer Element

<footer>
    <p>&copy; 2025 Company Name</p>
    <nav>
        <a href="#privacy">Privacy Policy</a>
        <a href="#terms">Terms of Service</a>
    </nav>
</footer>

Typically contains copyright, contact info, and secondary navigation.

Text Content Elements

Headings (h1-h6)

Headings create a document outline, like chapters in a book:

<h1>Main Title (only one per page)</h1>
<h2>Major Section</h2>
<h3>Subsection</h3>
<h4>Sub-subsection</h4>
<h5>Minor heading</h5>
<h6>Smallest heading</h6>
graph TD H1[h1: Page Title] --> H2A[h2: Section 1] H1 --> H2B[h2: Section 2] H2A --> H3A[h3: Subsection 1.1] H2A --> H3B[h3: Subsection 1.2] H2B --> H3C[h3: Subsection 2.1] H3A --> H4A[h4: Detail 1.1.1] style H1 fill:#f9f,stroke:#333,stroke-width:2px style H2A fill:#9ff,stroke:#333,stroke-width:2px style H2B fill:#9ff,stroke:#333,stroke-width:2px

Paragraph

<p>This is a paragraph of text. It represents a block of content.</p>

Lists

<!-- Unordered List -->
<ul>
    <li>Coffee</li>
    <li>Tea</li>
    <li>Milk</li>
</ul>

<!-- Ordered List -->
<ol>
    <li>First step</li>
    <li>Second step</li>
    <li>Third step</li>
</ol>

<!-- Description List -->
<dl>
    <dt>HTML</dt>
    <dd>HyperText Markup Language</dd>
    <dt>CSS</dt>
    <dd>Cascading Style Sheets</dd>
</dl>

Blockquote and Cite

<blockquote cite="https://example.com/quote-source">
    <p>"The Web is for everyone."</p>
    <footer>— <cite>Tim Berners-Lee</cite></footer>
</blockquote>

Figure and Figcaption

<figure>
    <img src="chart.png" alt="Sales chart for 2024">
    <figcaption>Annual sales performance showing 25% growth</figcaption>
</figure>

Inline Text Semantics

These elements add meaning to specific parts of text:

Emphasis and Strong

<p>This is <em>emphasized</em> text (usually italic).</p>
<p>This is <strong>strong</strong> text (usually bold).</p>

Links

<!-- External link -->
<a href="https://example.com" target="_blank" rel="noopener">Visit Example</a>

<!-- Internal link -->
<a href="#section1">Jump to Section 1</a>

<!-- Email link -->
<a href="mailto:contact@example.com">Email us</a>

<!-- Phone link -->
<a href="tel:+1234567890">Call us</a>

Code and Pre

<!-- Inline code -->
<p>Use the <code>console.log()</code> function to debug.</p>

<!-- Code block -->
<pre><code>function greet(name) {
    return `Hello, ${name}!`;
}</code></pre>

Time Element

<time datetime="2024-01-01T09:00">January 1, 2024 at 9:00 AM</time>
<time datetime="2024-01-01">New Year's Day</time>
<time datetime="PT2H30M">2 hours and 30 minutes</time>

Other Useful Inline Elements

<!-- Abbreviation -->
<abbr title="HyperText Markup Language">HTML</abbr>

<!-- Mark highlighted text -->
<p>Search results for: <mark>semantic HTML</mark></p>

<!-- Small print -->
<small>Copyright © 2024</small>

<!-- Subscript and superscript -->
<p>H<sub>2</sub>O</p>
<p>E = mc<sup>2</sup></p>

<!-- Deleted and inserted text -->
<del>Old price: $100</del>
<ins>New price: $80</ins>

Multimedia Elements

Images

<!-- Basic image -->
<img src="photo.jpg" alt="Description of the image" width="800" height="600">

<!-- Responsive image with multiple sources -->
<picture>
    <source media="(min-width: 800px)" srcset="large.jpg">
    <source media="(min-width: 400px)" srcset="medium.jpg">
    <img src="small.jpg" alt="Responsive image example">
</picture>

Audio

<audio controls>
    <source src="audio.mp3" type="audio/mpeg">
    <source src="audio.ogg" type="audio/ogg">
    Your browser doesn't support audio.
</audio>

Video

<video controls width="640" height="360" poster="thumbnail.jpg">
    <source src="video.mp4" type="video/mp4">
    <source src="video.webm" type="video/webm">
    <track kind="subtitles" src="subtitles_en.vtt" srclang="en" label="English">
    Your browser doesn't support video.
</video>

Iframe

<!-- Embed YouTube video -->
<iframe width="560" height="315" 
        src="https://www.youtube.com/embed/VIDEO_ID"
        title="YouTube video player"
        frameborder="0" 
        allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
        allowfullscreen>
</iframe>

Interactive Elements

Details and Summary

<details>
    <summary>Click to expand</summary>
    <p>This content is hidden by default and revealed when clicked.</p>
</details>

Dialog

<dialog id="myDialog">
    <h2>Dialog Title</h2>
    <p>This is a native dialog element.</p>
    <button onclick="this.closest('dialog').close()">Close</button>
</dialog>

<button onclick="document.getElementById('myDialog').showModal()">
    Open Dialog
</button>

Best Practices for Semantic HTML

mindmap root((Semantic HTML Best Practices)) Structure Use proper heading hierarchy One main element per page Header and footer for page structure Content Meaningful element names Avoid div soup Use article for standalone content Accessibility Alt text for images Labels for forms ARIA when needed SEO Descriptive headings Semantic structure Proper metadata

Do's and Don'ts

Real-World Example: Blog Post

<article>
    <header>
        <h1>Understanding Semantic HTML</h1>
        <p>Published on <time datetime="2024-01-15">January 15, 2024</time></p>
        <p>By <a href="/authors/jane-doe" rel="author">Jane Doe</a></p>
    </header>
    
    <main>
        <p>Semantic HTML is the foundation of accessible web development...</p>
        
        <section>
            <h2>Why It Matters</h2>
            <p>Using semantic elements improves:</p>
            <ul>
                <li>Accessibility</li>
                <li>SEO</li>
                <li>Maintainability</li>
            </ul>
        </section>
        
        <figure>
            <img src="semantic-diagram.png" alt="Diagram showing semantic HTML structure">
            <figcaption>Visual representation of semantic HTML elements</figcaption>
        </figure>
        
        <blockquote>
            <p>"The power of the Web is in its universality."</p>
            <footer>— <cite>Tim Berners-Lee</cite></footer>
        </blockquote>
    </main>
    
    <footer>
        <p>Tags: <a href="/tags/html">HTML</a>, <a href="/tags/web-development">Web Development</a></p>
    </footer>
</article>

Assignment: Semantic HTML Page

  1. Create a semantic HTML page for a blog with:
    • Header with site title and navigation
    • Main content area with 2-3 articles
    • Sidebar with related links
    • Footer with copyright and links
  2. Each article should include:
    • Proper heading hierarchy
    • Author and date information
    • At least one image with figure/figcaption
    • A blockquote
    • Multiple paragraphs and a list
  3. Use at least 5 different inline semantic elements
  4. Add a details/summary element for expandable content
  5. Validate your HTML using W3C validator

Bonus: Add an embedded video and audio element!