Welcome to The Coding College! This tutorial will guide you through creating a CSS Horizontal Navigation Bar. Horizontal navigation bars are a standard feature of websites, offering users an intuitive way to browse your site by displaying links side by side.
What is a Horizontal Navigation Bar?
A horizontal navigation bar is a menu that arranges navigation links in a row. It is usually positioned at the top of a webpage and is commonly used in website headers. Using CSS, you can style the navigation bar to create a visually appealing and user-friendly design.
Basic HTML Structure for a Horizontal Navigation Bar
Here’s how to set up a basic horizontal navigation bar using HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Horizontal Navigation Bar</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<nav class="horizontal-nav">
<ul class="nav-links">
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</body>
</html>
Styling the Horizontal Navigation Bar with CSS
Now, let’s style the navigation bar using CSS.
Example CSS:
/* General Styles */
body {
margin: 0;
font-family: Arial, sans-serif;
}
/* Navigation Bar Styles */
.horizontal-nav {
background-color: #333; /* Dark background */
overflow: hidden; /* Ensures proper alignment of elements */
}
.nav-links {
list-style: none; /* Removes bullets from list items */
margin: 0;
padding: 0;
display: flex; /* Aligns items horizontally */
}
.nav-links li {
margin: 0;
}
.nav-links a {
display: block; /* Makes the link clickable */
padding: 14px 20px; /* Adds spacing inside links */
color: white; /* White text */
text-decoration: none; /* Removes underline */
text-align: center; /* Centers the text */
transition: background-color 0.3s ease; /* Smooth hover effect */
}
.nav-links a:hover {
background-color: #575757; /* Changes background on hover */
}
Output:
You now have a horizontal navigation bar with links displayed in a row. The hover effect changes the background color for each link when hovered over.
Centering the Navigation Bar
To center the navigation bar within the webpage, add the following CSS:
.horizontal-nav {
display: flex;
justify-content: center; /* Centers the nav bar horizontally */
padding: 10px 0;
}
Adding an Active Link
You can use the .active
class to highlight the current section:
Example:
HTML:
<nav class="horizontal-nav">
<ul class="nav-links">
<li><a href="#home" class="active">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
CSS:
.nav-links a.active {
background-color: #575757; /* Highlight active link */
font-weight: bold; /* Emphasize active link */
}
Output: The “Home” link will appear distinct, indicating the active page.
Making the Navigation Bar Responsive
To ensure the navigation bar looks good on smaller screens, use CSS media queries.
Example CSS:
/* Responsive Styles */
@media (max-width: 768px) {
.nav-links {
flex-direction: column; /* Stack links vertically */
align-items: center; /* Center-align the links */
}
.nav-links a {
width: 100%; /* Full width for each link */
text-align: center;
}
}
Output: On smaller screens, the horizontal navigation bar becomes a vertical menu for better usability.
Adding Dropdown Menus
Dropdown menus are a great way to include sub-links within a horizontal navigation bar.
Example with Dropdown:
HTML:
<nav class="horizontal-nav">
<ul class="nav-links">
<li><a href="#home">Home</a></li>
<li class="dropdown">
<a href="#services">Services</a>
<ul class="dropdown-menu">
<li><a href="#web-design">Web Design</a></li>
<li><a href="#seo">SEO</a></li>
<li><a href="#marketing">Marketing</a></li>
</ul>
</li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
CSS:
/* Dropdown Menu Styles */
.dropdown {
position: relative;
}
.dropdown-menu {
display: none; /* Hide dropdown by default */
position: absolute;
top: 100%; /* Position dropdown below parent */
left: 0;
background-color: #444; /* Dropdown background color */
list-style: none;
margin: 0;
padding: 0;
min-width: 150px; /* Set dropdown width */
}
.dropdown-menu li {
border-bottom: 1px solid #575757; /* Add dividers */
}
.dropdown-menu a {
padding: 10px 15px;
text-decoration: none;
color: white;
display: block;
}
.dropdown:hover .dropdown-menu {
display: block; /* Show dropdown on hover */
}
Output: When you hover over “Services,” a dropdown menu appears with additional links.
Adding Icons to the Navigation Bar
You can include icons in the navigation bar using libraries like Font Awesome.
Example with Icons:
HTML:
<nav class="horizontal-nav">
<ul class="nav-links">
<li><a href="#home"><i class="fa fa-home"></i> Home</a></li>
<li><a href="#about"><i class="fa fa-info-circle"></i> About</a></li>
<li><a href="#services"><i class="fa fa-cogs"></i> Services</a></li>
<li><a href="#contact"><i class="fa fa-envelope"></i> Contact</a></li>
</ul>
</nav>
CSS:
.nav-links a i {
margin-right: 8px; /* Space between icon and text */
}
Output: Icons appear next to each link, improving visual appeal.
Fixed Horizontal Navigation Bar
To keep the navigation bar fixed at the top of the page while scrolling:
.horizontal-nav {
position: fixed;
top: 0;
width: 100%; /* Stretches the bar across the screen */
z-index: 10; /* Ensures it stays above other elements */
}
Summary
In this tutorial, you learned how to:
- Create a basic horizontal navigation bar using HTML and CSS.
- Style it with hover effects and an active link.
- Add responsiveness to make it mobile-friendly.
- Include dropdown menus and icons.
- Make the navigation bar fixed for better usability.
For more CSS tutorials, visit The Coding College.
Happy coding and designing amazing horizontal navigation bars!