Build a Reusable Navbar Component for Multiple Pages Using HTML, CSS, and JavaScript

In this tutorial, we learned how to create a reusable navbar component using HTML, CSS, and JavaScript by dynamically loading the navbar into multiple pages, ensuring consistency and simplifying maintenance across the website.

Algogenz logo

2m · 4min read

Creating a consistent and reusable navigation bar (navbar) across multiple pages of a website can significantly enhance the user experience.


This tutorial will guide you through building a navbar component that can be used on different pages with minimal repetition of code, ensuring maintainability and scalability of your project. We will use HTML, CSS, and JavaScript to achieve this.


Introduction

When developing a website, it is common to have a navigation bar that remains consistent across various pages. Manually including the same HTML code for the navbar on every page can be cumbersome and prone to errors. Instead, we can create a single HTML file for the navbar and dynamically load it into each page using JavaScript. This approach simplifies updates and maintenance since changes only need to be made in one place.


Prerequisites

Before we begin, ensure you have a basic understanding of HTML, CSS, and JavaScript. You will also need a code editor and a way to serve your HTML files, such as the "Live Server" extension in Visual Studio Code. Open with Live Server by pressing Alt+L O and stop it by using Alt+L C.


Step 1: Creating the HTML Pages

First, we will create the HTML files for our website: index.html, about.html, and contact.html. Each file will have a placeholder for the navbar, which we will load dynamically.


index.html

This is the homepage of our website. It includes a div with the id navbar-container where the navbar will be loaded.


<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Home</title><link rel="stylesheet" href="styles.css">
</head>
<body><div id="navbar-container"></div><div><h1>Welcome to My Website!</h1><p>This is my homepage.</p></div><script src="navbar.js"></script>
</body>
</html>

about.html

This is the about page. It has a similar structure to the homepage but with different content.


<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>About</title><link rel="stylesheet" href="styles.css">
</head>
<body><div id="navbar-container"></div><div><h1>About Me</h1><p>This is my about page.</p></div><script src="navbar.js"></script>
</body>
</html>

contact.html

This is the contact page. It follows the same structure as the previous pages but with content specific to contacting.


<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Contact</title><link rel="stylesheet" href="styles.css">
</head>
<body><div id="navbar-container"></div><div><h1>Contact Me</h1><p>This is my contact page.</p></div><script src="navbar.js"></script>
</body>
</html>

Step 2: Creating the Navbar Component

Next, create a file named navbar.html that contains the HTML structure of the navbar. This file will be loaded into the navbar-container div on each page.


<!-- navbar.html -->
<nav><ul><li><a href="index.html">Home</a></li><li><a href="about.html">About</a></li><li><a href="contact.html">Contact</a></li></ul>
</nav>

Step 3: Styling the Navbar

We will use a CSS file (styles.css) to style our navbar and page content. Create styles.css with the following content:


/* styles.css */
/* Navbar styles */
nav {
    background-color: #333;
    color: #fff;
    padding: 10px;
}

nav ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}

nav ul li {
    display: inline;
    margin-right: 20px;
}

nav ul li a {
    color: #fff;
    text-decoration: none;
}

nav ul li a:hover {
    text-decoration: underline;
}

/* Page content styles */
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 20px;
}

h1 {
    color: #333;
}

p {
    color: #666;
}

Step 4: Loading the Navbar Dynamically

We will use JavaScript to load the navbar into each page. Create a file named navbar.js and add the following code:


// navbar.js
document.addEventListener("DOMContentLoaded", function() {
    fetch('navbar.html')
       .then(response => response.text())
       .then(data => {
            document.getElementById('navbar-container').innerHTML = data;
        });
});

Step 5: Testing the Implementation

Ensure all your files (index.html, about.html, contact.html, navbar.html, styles.css, navbar.js) are in the same directory. Open each HTML file in a browser to verify that the navbar is correctly loaded and styled on all pages.


  • Open the HTML files: Open index.html, about.html, and contact.html in your browser to check if the navbar appears correctly on each page.
  • Verify the navbar content: Ensure the navbar contains links to Home, About, and Contact, and clicking these links navigates to the respective pages.
  • Check the styling: Verify that the navbar and page content are styled as expected according to styles.css.


Conclusion

By creating a reusable navbar component and dynamically loading it into multiple pages, we have streamlined the development process and ensured consistency across our website. This method makes it easier to maintain and update the navbar, as changes only need to be made in one file.

Recommended

TypeScript Types vs Interfaces

4m · 5min read

Web Development

TypeScript Types vs Interfaces

Types vs Interfaces: Types in TypeScript are more flexible and can define a wider range of data types, including primitives, unions, intersections, tuples, and more, while interfaces are primarily used to describe the shape of objects and support declaration merging and extending, making them ideal for object-oriented programming and complex data structures

The this Keyword in JavaScript

5m · 3min read

Web Development

The this Keyword in JavaScript

The this keyword in JavaScript is a reference to the object that a function is a property of. It's a fundamental concept in JavaScript, especially in object-oriented programming. Unlike in languages like Java, C#, or PHP, where this typically refers to the current instance of the class, JavaScript's this behaves differently and can be quite versatile.

Next pages: