Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Separate Header from Body in HTML?
To separate header from body in HTML, you can create visual and structural distinction between the header section and the main content. The header typically contains navigation, titles, and introductory elements, while the body holds the primary content of the webpage.
In this article, we will explore different approaches to effectively separate the header from the body content using HTML structure and CSS styling.
Approaches to Separate Header from Body
- Using Semantic HTML Tags
- Using CSS Styling
- Using Multiple Headers
Method 1: Using Semantic HTML Tags
HTML5 provides semantic tags that help create a clear structural separation between different page sections. The <header> and <main> tags provide meaningful structure
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Header Separation Example</title>
</head>
<body>
<header>
<h1>Welcome to Tutorialspoint</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>
<p>This is the main content section, clearly separated from the header above.</p>
</main>
</body>
</html>
A webpage with a header containing "Welcome to Tutorialspoint" and navigation links, followed by main content below. The semantic structure provides clear separation between header and body content.
Method 2: Using CSS Styling
CSS styling provides visual separation through background colors, borders, padding, and other styling properties
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Header Separation</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.site-header {
background-color: #2c3e50;
color: white;
padding: 20px;
text-align: center;
border-bottom: 3px solid #34495e;
}
.site-header h1 {
margin: 0;
font-size: 2rem;
}
.nav {
margin-top: 15px;
}
.nav a {
margin: 0 15px;
color: white;
text-decoration: none;
font-weight: bold;
}
.nav a:hover {
color: #3498db;
}
.content-area {
padding: 30px;
background-color: white;
margin: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<header class="site-header">
<h1>Tutorialspoint</h1>
<nav class="nav">
<a href="#home">Home</a>
<a href="#tutorials">Tutorials</a>
<a href="#about">About</a>
</nav>
</header>
<main class="content-area">
<h2>Main Content</h2>
<p>This content section is visually separated from the header using CSS styling, creating a clear distinction between the two areas.</p>
</main>
</body>
</html>
A webpage with a dark blue header containing the site title and navigation, clearly separated from a white content area below with padding and shadow effects. The visual styling creates a distinct separation between header and body.
Method 3: Using Multiple Headers
Complex websites often use multiple headers to organize content into different sections. This approach uses heading tags (h1, h2, h3) to create hierarchical content structure
<!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>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
background-color: #f8f9fa;
}
.main-header {
background-color: #007bff;
color: white;
padding: 20px;
text-align: center;
}
.section {
margin: 20px;
padding: 20px;
background-color: white;
border-radius: 5px;
border-left: 4px solid #007bff;
}
.section h2 {
background-color: #e9ecef;
padding: 10px;
margin: -20px -20px 15px -20px;
border-radius: 5px 5px 0 0;
color: #495057;
}
.section h3 {
color: #007bff;
border-bottom: 2px solid #dee2e6;
padding-bottom: 5px;
}
</style>
</head>
<body>
<header class="main-header">
<h1>Multiple Headers Tutorial</h1>
</header>
<section class="section">
<h2>Introduction Section</h2>
<h3>Getting Started</h3>
<p>This section introduces the concept using multiple headers to organize content effectively.</p>
</section>
<section class="section">
<h2>Content Section</h2>
<h3>Main Topics</h3>
<p>Each section has its own header structure, making the content easy to navigate and understand.</p>
</section>
</body>
</html>
A webpage with a main blue header at the top, followed by multiple content sections, each with their own styled headers. Each section has a gray header bar and blue subheadings, creating a clear hierarchical structure.
Conclusion
Separating headers from body content can be achieved through semantic HTML tags, CSS styling, or multiple header structures. Choose the method that best fits your webpage's complexity and design requirements for optimal user experience and content organization.
