A navigation bar (navbar) is a section of a website that allows users to navigate between different pages or sections.
It is usually placed at the top of the webpage and contains links to important sections such as Home, About, Services, Contact, etc.
Navigation bars improve user experience and help organize website structure.
A navigation bar is typically created using:
<nav> – Semantic HTML5 element for navigation<ul> – Unordered list to structure links<li> – Each menu item<a> – Hyperlink to pages
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
To make it horizontal and visually appealing, we use CSS:
<style>
nav ul {
list-style-type: none; /* Remove bullets */
margin: 0;
padding: 0;
background-color: #333; /* Navbar background */
overflow: hidden;
}
nav ul li {
float: left; /* Horizontal layout */
}
nav ul li a {
display: block;
color: white;
text-align: center;
padding: 14px 20px;
text-decoration: none;
}
nav ul li a:hover {
background-color: #111; /* Hover effect */
}
</style>
<ul> holds all the links<li> represents each menu itemfloat: left makes the items horizontalbackground-color sets the navbar colorhover effect improves interactivityModern websites require mobile-friendly navbars.
A simple responsive approach is using flexbox:
nav ul {
display: flex;
flex-wrap: wrap; /* Wrap items on small screens */
}
nav ul li {
flex: 1; /* Evenly distributed */
}
For fully responsive menus, JavaScript is often used for toggle buttons on mobile.
Create a basic horizontal navigation bar with hover effects:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML Navigation Bar Practice</title>
<style>
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #2c3e50;
overflow: hidden;
}
nav ul li {
float: left;
}
nav ul li a {
display: block;
color: white;
text-align: center;
padding: 14px 20px;
text-decoration: none;
font-family: Arial, sans-serif;
}
nav ul li a:hover {
background-color: #1abc9c;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
<h1>Welcome to My Website</h1>
<p>This is a sample page with a navigation bar.</p>
</body>
</html>