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
Splitting up an HTML page and loading it through header?
Splitting HTML pages into separate components like header, footer, and content sections can significantly improve development efficiency and maintainability. This approach allows developers to reuse common elements across multiple pages without duplicating code.
The three main components typically include −
- Header − Contains navigation menu, logo, and site-wide elements
- Content − The main body content that changes between pages
- Footer − Contains copyright information, links, and contact details
Static HTML websites cannot natively include external HTML files, but this functionality can be achieved through server-side languages like PHP or client-side JavaScript techniques.
Method 1 − Using PHP Include
PHP provides built-in functions to include external files, making it the most straightforward approach for splitting HTML pages.
Creating Separate Files
First, create separate files for each component −
<!-- header.php -->
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<meta charset="UTF-8">
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.header { background-color: #333; color: white; padding: 10px; text-align: center; }
.footer { background-color: #333; color: white; padding: 10px; text-align: center; margin-top: 20px; }
.content { padding: 20px; min-height: 300px; }
</style>
</head>
<body>
<div class="header">
<h1>My Website Header</h1>
<nav>
<a href="index.php" style="color: white; margin: 0 10px;">Home</a>
<a href="about.php" style="color: white; margin: 0 10px;">About</a>
<a href="contact.php" style="color: white; margin: 0 10px;">Contact</a>
</nav>
</div>
<!-- footer.php -->
<div class="footer">
<p>© 2024 My Website. All rights reserved.</p>
</div>
</body>
</html>
Main Page with PHP Include
Now create the main page that includes both header and footer −
<?php include 'header.php'; ?>
<div class="content">
<h2>Welcome to My Website</h2>
<p>This is the main content area. This content is unique to this page while the header and footer are shared across all pages.</p>
<p>You can include any HTML content here including images, forms, tables, and other elements.</p>
</div>
<?php include 'footer.php'; ?>
The PHP server processes the includes and delivers a complete HTML page to the browser.
Method 2 − Using JavaScript Fetch API
For client-side loading, JavaScript can fetch HTML content and inject it into the page after the main document loads.
Creating HTML Fragment Files
Create separate HTML files containing only the fragment content −
<!-- header.html -->
<div class="header">
<h1>My Website Header</h1>
<nav>
<a href="index.html">Home</a>
<a href="about.html">About</a>
<a href="contact.html">Contact</a>
</nav>
</div>
<!-- footer.html -->
<div class="footer">
<p>© 2024 My Website. All rights reserved.</p>
</div>
Main Page with JavaScript Loading
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.header { background-color: #333; color: white; padding: 10px; text-align: center; }
.header a { color: white; margin: 0 10px; text-decoration: none; }
.footer { background-color: #333; color: white; padding: 10px; text-align: center; margin-top: 20px; }
.content { padding: 20px; min-height: 300px; }
.loading { text-align: center; color: #666; }
</style>
</head>
<body>
<div id="header-placeholder"><div class="loading">Loading header...</div></div>
<div class="content">
<h2>Welcome to My Website</h2>
<p>This is the main content area loaded with the page.</p>
<p>Header and footer are loaded dynamically using JavaScript.</p>
</div>
<div id="footer-placeholder"><div class="loading">Loading footer...</div></div>
<script>
async function loadComponent(file, placeholder) {
try {
const response = await fetch(file);
const content = await response.text();
document.getElementById(placeholder).innerHTML = content;
} catch (error) {
document.getElementById(placeholder).innerHTML = '<p>Error loading component</p>';
console.error('Error loading ' + file + ':', error);
}
}
// Load components when page loads
window.addEventListener('DOMContentLoaded', function() {
loadComponent('header.html', 'header-placeholder');
loadComponent('footer.html', 'footer-placeholder');
});
</script>
</body>
</html>
The JavaScript approach loads components asynchronously after the main page content is ready, showing loading messages while fetching the external files.
Method 3 − Using Server-Side Includes (SSI)
Many web servers support Server-Side Includes, which allow including external files using special HTML comments.
Example
<!DOCTYPE html>
<html>
<head>
<title>SSI Example</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.header { background-color: #4CAF50; color: white; padding: 15px; text-align: center; }
.content { padding: 20px; }
.footer { background-color: #333; color: white; padding: 10px; text-align: center; }
</style>
</head>
<body>
<!--#include file="header.shtml" -->
<div class="content">
<h2>Main Content</h2>
<p>This content is included using Server-Side Includes.</p>
</div>
<!--#include file="footer.shtml" -->
</body>
</html>
SSI requires server configuration and typically uses .shtml file extensions. The server processes these includes before sending the page to the browser.
Comparison of Methods
| Method | Advantages | Disadvantages |
|---|---|---|
| PHP Include | Server-side processing, SEO-friendly, no JavaScript required | Requires PHP-enabled server, not suitable for static hosting |
| JavaScript Fetch | Works with static hosting, flexible and modern | Client-side dependency, potential SEO issues, loading delays |
| Server-Side Includes | Lightweight, server-processed | Limited server support, requires configuration |
Benefits of Splitting HTML Pages
- Code Reusability − Write header and footer once, use across multiple pages
- Easy Maintenance − Update navigation or footer in one place affects all pages
- Consistency − Ensures uniform appearance and behavior across the website
- Team Development − Different team members can work on different components simultaneously
- Reduced File Size − Eliminates duplicate code across pages
Conclusion
Splitting HTML pages into reusable components improves development efficiency and maintainability. PHP includes offer the most robust server-side solution, while JavaScript provides flexibility for static websites. Choose the method based on your hosting environment and technical requirements.
