close
shape shape

How to Separate Header from Body in HTML?

Separating the header from the body in HTML is essential for creating organized, maintainable web pages. It’s crucial to know how to define and style the header and body elements independently. With practical tips, you can enhance your website's structure and usability.

How to Separate Header from Body in HTML
author image
admin
Technical Tutorial Nov 28, 2024
Table of Content icon image

An organized HTML file is essential for every WordPress website. Although most WordPress themes have a separate header and body, some themes don’t have the facility. In this case, properly separating the header and body is an essential aspect of a well-structured HTML. 

In this detailed guide, we will cover everything you need to know about separating the header from the body in HTML. We will provide practical examples, tips, and best practices.

What Does Separating the Header from the Body Mean?

In web design, the header and the body are two distinct sections of an HTML document. The header typically includes the website's branding elements, navigation menus, and other introductory content.

The body houses the main content, which varies depending on the page's purpose. By clearly defining these sections, you can ensure improved code organization. You can make easy styling and updates with CSS by separating the header from the body in HTML.

What Are the HTML Structural Tags:

HTML provides Structural Tags specifically designed for creating these sections:

<header>:

The header tag represents the top section of a page or a section within it. It often includes a logo, navigation, the site title, the page title, and introductory information.

<body>:

The body tag contains all visible content of the web page. Additionally, it includes text, images, videos, forms, and more.

<main>:

The main tag generally marks the primary content area of the page. It’s used to ensure search engines and assistive technologies understand the structure of your site.

<nav>: 

Navigation tag used within the header for navigation menus.

<footer>: 

The footer tag represents the bottom section of the page.

Step-by-Step Process to Separate Header and Body in HTML:

Step 1: Building the Basic HTML Structure

Start by creating a simple HTML document. Use the <header> tag for the top section and the <main> tag for the main content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Separate Header and Body</title>
</head>
<body>
    <header>
        <h1>My Website</h1>
        <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>
    </header>
    <main>
        <h2>Welcome to My Website</h2>
        <p>This is the main content of the page.</p>
    </main>
</body>
</html>

In this code, the <header> contains the site’s title and a simple navigation bar. The <main> holds the primary content.

Step 2: Styling the Header and Body with CSS

To visually separate the header from the body, apply CSS styles. This helps create distinct areas for these sections:

<style>
    
    body {
        font-family: Arial, sans-serif;
        margin: 0;
        padding: 0;
    }
 
    header {
        background-color: #007BFF;
        color: white;
        padding: 15px;
        text-align: center;
    }
 
    header nav ul {
        list-style: none;
        padding: 0;
    }
 
    header nav ul li {
        display: inline;
        margin: 0 10px;
    }
 
    header nav ul li a {
        color: white;
        text-decoration: none;
    }



    main {
        padding: 20px;
        margin: 20px;
        background-color: #f8f9fa;
    }
</style>

Step 3: Make the Header Fixed

If you want the header to remain visible while scrolling, use position: fixed in your CSS. This ensures the header stays at the top of the page even as the user scrolls:

header {
    position: fixed;
    top: 0;
    width: 100%;
    z-index: 1000;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
 
main {
    margin-top: 70px;
}

Step 4: Adding Responsiveness

For a mobile-friendly layout, use CSS media queries to adjust the design for smaller screens:

@media (max-width: 600px) {
    header nav ul li {
        display: block;
        margin: 10px 0;
    }
}

Step 5: Add Dynamic Behavior to Navigation Links

You can make navigation links more interactive using JavaScript. For example, enable smooth scrolling when a user clicks a link with the code below:

document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        document.querySelector(this.getAttribute('href')).scrollIntoView({
            behavior: 'smooth'
        });
    });
});

Best Practices for Header and Body Separation:

  • Always use <header>, <main>, and <footer> for better readability and accessibility.
  • Avoid overloading the header with heavy scripts, as this can slow down page loading.
  • Use proper HTML hierarchy and add aria-labels to navigation links for screen readers.
  • Use headings like <h1> for the page title and <h2> or <h3> for subheadings in the main content.
  • Include descriptive meta tags and titles in the header section.

Summary

Separating the header from the body in HTML is more than a coding convention. It is essential for creating professional, user-friendly websites. By using semantic tags, CSS styling, and JavaScript enhancements, you can achieve this by offering an excellent user experience.

No matter whether you’re building a simple static page or a complex website, these practices will ensure your code remains organized and easy to maintain. So, apply these methods in your next project to make your website stand out.

Related: What Image Does WordPress Show on Preview?

You might also like

icon