Semantic Layout


This section delves into HTML5 semantic elements, essential for building accessible and SEO-friendly webpages. These tags provide meaning to different parts of your content, unlike generic <div> elements.

The Importance of a Semantic Layout

Semantic HTML tags clearly define the purpose of different parts of a webpage. This improves accessibility for screen readers and helps search engines understand your content's structure, boosting your SEO.


The Main Content Area (<main>)

The <main> element represents the dominant content of the <body> of a document. There should only be one <main> element per document, and it should not be a descendant of an <article>, <aside>, <footer>, <header>, or <nav> element.

Example 1: Basic <main> usage

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Semantic Main Example</title>
</head>
<body>
    <header>
        <h1>Website Header</h1>
    </header>
    <main>
        <p>Welcome to our amazing website! Discover great information here.</p>
        <section>
            <h2>About Us</h2>
            <p>Learn more about our mission and values.</p>
        </section>
    </main>
    <footer>
        <p>&copy; 2025 My Website</p>
    </footer>
</body>
</html>

Explanation This code demonstrates a basic HTML document with a <main> element encapsulating the core content, separate from the header and footer. Search engines and assistive technologies recognize this as the most important part of the page.


Example 2: <main> with multiple sections

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Main with Sections</title>
</head>
<body>
    <main>
        <section id="introduction">
            <h2>Introduction to HTML5</h2>
            <p>HTML5 brought many new semantic elements for better web structuring.</p>
        </section>
        <section id="features">
            <h2>Key Features</h2>
            <ul>
                <li>Improved semantics</li>
                <li>Multimedia support</li>
                <li>Offline capabilities</li>
            </ul>
        </section>
    </main>
</body>
</html>

Explanation Here, the <main> element contains multiple <section> elements, each focusing on a different aspect of the primary content. This shows how to further organize content within the main area.


Example 3: Incorrect <main> usage (for demonstration)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Incorrect Main Example</title>
</head>
<body>
    <header>
        <h1>Page Title</h1>
        <main>
            <p>This content should be outside the header.</p>
        </main>
    </header>
    <main>
        <p>This is the actual main content of the document.</p>
    </main>
</body>
</html>

Explanation This example highlights a common mistake: placing <main> within a <header> element. The browser will still render it, but it violates semantic rules and can negatively impact accessibility and SEO.


The Header (<header>) and Footer (<footer>)

The <header> element typically contains introductory content or navigational links, while the <footer> element usually contains authorship information, copyright data, or contact information.

Example 1: Basic Header and Footer

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Header and Footer Example</title>
</head>
<body>
    <header>
        <h1>My Awesome Blog</h1>
        <nav>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>
    </header>
    <main>
        <p>Blog post content goes here.</p>
    </main>
    <footer>
        <p>&copy; 2025 All Rights Reserved.</p>
        <address>
            <a href="mailto:info@example.com">info@example.com</a>
        </address>
    </footer>
</body>
</html>

Explanation This code demonstrates a standard webpage structure using <header> for site navigation and branding, and <footer> for copyright and contact details. This provides clear structural cues to browsers and users.


Example 2: Multiple Headers (within articles)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multiple Headers Example</title>
</head>
<body>
    <article>
        <header>
            <h2>First Blog Post Title</h2>
            <p>Published on: <time datetime="2025-06-15">June 15, 2025</time></p>
        </header>
        <p>Content of the first blog post.</p>
    </article>
    <article>
        <header>
            <h2>Second Blog Post Title</h2>
            <p>Published on: <time datetime="2025-06-16">June 16, 2025</time></p>
        </header>
        <p>Content of the second blog post.</p>
    </article>
</body>
</html>

Explanation While there's typically one main <header> for the document, individual <article> or <section> elements can also have their own <header> elements. This is useful for providing specific introductory information for that content block.


Example 3: Footer with additional links

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Footer Links Example</title>
</head>
<body>
    <main>
        <p>Page content.</p>
    </main>
    <footer>
        <nav>
            <ul>
                <li><a href="#">Privacy Policy</a></li>
                <li><a href="#">Terms of Service</a></li>
                <li><a href="#">Sitemap</a></li>
            </ul>
        </nav>
        <p>Contact us at <a href="mailto:support@example.com">support@example.com</a></p>
    </footer>
</body>
</html>

Explanation This example shows a <footer> containing a navigation menu specifically for utility links like privacy policy or sitemap. This is a common practice for organizing less prominent but important navigation.


Navigation (<nav>)

The <nav> element is intended for major navigation blocks. It should contain links to other pages or to parts within the current page.

Example 1: Top navigation menu

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Navigation Menu</title>
    <style>
        nav ul { list-style: none; padding: 0; }
        nav ul li { display: inline; margin-right: 20px; }
    </style>
</head>
<body>
    <header>
        <nav>
            <ul>
                <li><a href="/">Home</a></li>
                <li><a href="/products">Products</a></li>
                <li><a href="/services">Services</a></li>
                <li><a href="/contact">Contact</a></li>
            </ul>
        </nav>
    </header>
    <main>
        <p>Welcome to our site!</p>
    </main>
</body>
</html>

Explanation This is a typical website navigation bar, wrapped in a <nav> element. It clearly indicates to search engines and screen readers that these are primary navigation links.


Example 2: In-page navigation (Table of Contents)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>In-page Navigation</title>
</head>
<body>
    <main>
        <nav>
            <h3>Table of Contents</h3>
            <ul>
                <li><a href="#section1">Section One</a></li>
                <li><a href="#section2">Section Two</a></li>
                <li><a href="#section3">Section Three</a></li>
            </ul>
        </nav>
        <section id="section1">
            <h2>Section One</h2>
            <p>Content for the first section.</p>
        </section>
        <section id="section2">
            <h2>Section Two</h2>
            <p>Content for the second section.</p>
        </section>
        <section id="section3">
            <h2>Section Three</h2>
            <p>Content for the third section.</p>
        </section>
    </main>
</body>
</html>

Explanation This example demonstrates a <nav> element used for an internal table of contents, allowing users to jump to different sections of a long page. This improves user experience for lengthy content.


Example 3: Secondary navigation (e.g., sidebar)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Secondary Navigation</title>
</head>
<body>
    <aside>
        <nav>
            <h3>Related Topics</h3>
            <ul>
                <li><a href="#">Topic A</a></li>
                <li><a href="#">Topic B</a></li>
                <li><a href="#">Topic C</a></li>
            </ul>
        </nav>
    </aside>
    <main>
        <p>Main content area.</p>
    </main>
</body>
</html>

Explanation Here, a <nav> element is placed within an <aside> to provide a secondary navigation block, such as related topics or sub-sections, often seen in sidebars. This clarifies its role as supportive navigation.


Articles and Sections (<article> and <section>)

The <article> element represents a self-contained composition in a document, page, application, or site, independently distributable or reusable. The <section> element represents a standalone section of a document, intended to be grouped logically.

Example 1: Blog post using <article> and <section>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Article and Section Example</title>
</head>
<body>
    <main>
        <article>
            <h2>My Awesome Blog Post</h2>
            <p>By John Doe, <time datetime="2025-06-16">June 16, 2025</time></p>
            <section>
                <h3>Introduction</h3>
                <p>This is the introductory part of the blog post.</p>
            </section>
            <section>
                <h3>Main Content</h3>
                <p>Here you'll find the detailed content of the post.</p>
            </section>
            <footer>
                <p>Tags: HTML, Semantics, Web Development</p>
            </footer>
        </article>
    </main>
</body>
</html>

Explanation This code structures a blog post using <article> for the entire post and <section> for distinct parts within it, like introduction and main content. This semantic structure is ideal for syndication.


Example 2: News feed with multiple articles

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multiple Articles</title>
</head>
<body>
    <main>
        <h1>Latest News</h1>
        <article>
            <h2>Breaking News Headline 1</h2>
            <p>Summary of the first news story.</p>
            <a href="#">Read More</a>
        </article>
        <article>
            <h2>Local News Update 2</h2>
            <p>Summary of the second news story.</p>
            <a href="#">Read More</a>
        </article>
    </main>
</body>
</html>

Explanation This demonstrates using multiple <article> elements for a news feed, where each article is a self-contained news item. Each article could theoretically be distributed independently.


Example 3: A <section> that isn't an <article>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Section Not Article</title>
</head>
<body>
    <main>
        <h1>Product Page</h1>
        <section>
            <h2>Product Description</h2>
            <p>This is a detailed description of our amazing product.</p>
        </section>
        <section>
            <h2>Customer Reviews</h2>
            <article>
                <h3>Review by Jane Doe</h3>
                <p>Great product, highly recommend!</p>
            </article>
            <article>
                <h3>Review by John Smith</h3>
                <p>Very satisfied with my purchase.</p>
            </article>
        </section>
    </main>
</body>
</html>

Explanation Here, a product page uses <section> to group "Product Description" and "Customer Reviews." Within "Customer Reviews," individual reviews are <article> elements because they are self-contained.


Asides (<aside>)

The <aside> element represents a portion of a document whose content is only tangentially related to the content around it, often presented as a sidebar or pull quote.

Example 1: Simple sidebar

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Aside Sidebar</title>
</head>
<body>
    <main>
        <h1>Main Content</h1>
        <p>This is the primary content of the page.</p>
    </main>
    <aside>
        <h3>Related Links</h3>
        <ul>
            <li><a href="#">Link 1</a></li>
            <li><a href="#">Link 2</a></li>
        </ul>
    </aside>
</body>
</html>

Explanation This code uses <aside> to create a sidebar containing "Related Links," content that is relevant but not core to the main topic. This is a common layout pattern.


Example 2: Pull quote using <aside>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Aside Pull Quote</title>
    <style>
        aside { float: right; width: 30%; margin-left: 20px; font-style: italic; }
    </style>
</head>
<body>
    <main>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
        <aside>
            <p>"The best way to predict the future is to create it."</p>
            <cite>- Peter Drucker</cite>
        </aside>
        <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    </main>
</body>
</html>

Explanation Here, <aside> is used for a "pull quote," a stylistic element that highlights a key phrase from the main text. While visually integrated, its content is tangential to the main flow.


Example 3: Advertisement block in <aside>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Aside Advertisement</title>
</head>
<body>
    <main>
        <p>Enjoy our fantastic content!</p>
    </main>
    <aside>
        <h3>Sponsored Content</h3>
        <p>Check out our partner's amazing new product!</p>
        <a href="#">Click Here</a>
    </aside>
</body>
</html>

Explanation This example illustrates using an <aside> for an advertisement or sponsored content. This type of content is related to the page but not part of its primary narrative.