Learn how to create and style a modern navigation bar with a hover-activated dropdown menu using only HTML and CSS. Step-by-step guide with code examples.
A clean and interactive navigation bar is a must for any website. One common feature is a dropdown menu, which allows users to explore submenus when they hover over a main menu item. In this tutorial, you’ll learn how to create a stylish navigation bar with a hover dropdown menu using only HTML and CSS – no JavaScript needed.
Let’s break everything down step-by-step so even beginners can follow along!
We’ll start by creating a simple HTML layout for the navbar and dropdown.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Hover Dropdown Menu</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<nav class="navbar">
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li class="dropdown">
<a href="#">Services</a>
<ul class="dropdown-menu">
<li><a href="#">Web Design</a></li>
<li><a href="#">SEO</a></li>
<li><a href="#">Marketing</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</body>
</html>
The <nav>
tag defines the navigation bar.
Inside it, we use an unordered list <ul class="menu">
for the main menu items.
The item “Services” contains a nested <ul class="dropdown-menu">
, which acts as the dropdown list.
Next, let’s add styles to make it look neat and modern.
style.css
):/* Reset some basic styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background: #f5f5f5;
}
/* Navbar container */
.navbar {
background-color: #333;
padding: 10px 0;
}
/* Main menu */
.menu {
list-style: none;
display: flex;
justify-content: center;
}
/* Menu items */
.menu li {
position: relative;
}
.menu a {
display: block;
padding: 12px 20px;
color: white;
text-decoration: none;
transition: background 0.3s;
}
.menu a:hover {
background-color: #555;
}
/* Dropdown menu - hidden by default */
.dropdown-menu {
display: none;
position: absolute;
top: 100%;
left: 0;
background-color: #444;
min-width: 160px;
z-index: 1000;
}
.dropdown-menu li {
border-bottom: 1px solid #555;
}
.dropdown-menu a {
padding: 10px 15px;
color: white;
}
.dropdown-menu a:hover {
background-color: #666;
}
/* Show dropdown on hover */
.dropdown:hover .dropdown-menu {
display: block;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
This removes default browser spacing and ensures consistent sizing using the box-sizing
rule.
.navbar {
background-color: #333;
padding: 10px 0;
}
Gives a dark background and vertical padding to the navbar.
.menu {
list-style: none;
display: flex;
justify-content: center;
}
Removes bullet points.
Uses flex
to arrange items horizontally and center them.
.menu li {
position: relative;
}
.menu a {
display: block;
padding: 12px 20px;
color: white;
text-decoration: none;
transition: background 0.3s;
}
.menu a:hover {
background-color: #555;
}
position: relative
is important for dropdown positioning.
transition
adds a smooth background color effect on hover.
.dropdown-menu {
display: none;
position: absolute;
top: 100%;
left: 0;
background-color: #444;
}
display: none
hides it initially.
position: absolute
places it right under the parent item.
top: 100%
ensures the dropdown appears just below the main menu item.
.dropdown:hover .dropdown-menu {
display: block;
}
This line is the magic!
When the user hovers over the .dropdown
item, its nested .dropdown-menu
becomes visible.
For better mobile support, you can add media queries and a hamburger menu — but for now, this pure CSS approach works great for basic layouts and desktops.
Here’s what you’ll get:
A horizontal navbar
When you hover over "Services", a dropdown menu slides out
Styled with modern colors and smooth transitions
You’ve just learned how to create a professional-looking navigation bar with a hover dropdown menu using only CSS and HTML. This technique is clean, fast, and perfect for most websites without needing JavaScript.
This is a great way to enhance UX and organize your site’s navigation clearly and efficiently.
Thank you for visiting! Enjoy exploring our diverse collection of blogs, crafted with passion and insight to inspire and inform. Happy reading!