๐ Introduction
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!
๐งฑ Step 1: Basic HTML Structure
We’ll start by creating a simple HTML layout for the navbar and dropdown.
โ HTML Code:
๐ Explanation:
-
The
<nav>
tag defines the navigation bar. -
Inside it, we use an unordered list
<ul class="menu">
for the main menu items. -
Google Advertisement
The item “Services” contains a nested
<ul class="dropdown-menu">
, which acts as the dropdown list.
๐จ Step 2: CSS Styling for the Navbar
Next, let’s add styles to make it look neat and modern.
โ
CSS Code (style.css
):
๐ง Step-by-Step Explanation of CSS:
1. Reset Styles
This removes default browser spacing and ensures consistent sizing using the box-sizing
rule.
2. Navbar Container
Gives a dark background and vertical padding to the navbar.
3. Main Menu Styling
-
Removes bullet points.
-
Uses
flex
to arrange items horizontally and center them.
4. Menu Items
-
Google Advertisement
position: relative
is important for dropdown positioning. -
transition
adds a smooth background color effect on hover.
5. Dropdown Menu
-
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.
6. Show Dropdown on Hover
-
This line is the magic!
-
Google Advertisement
When the user hovers over the
.dropdown
item, its nested.dropdown-menu
becomes visible.
๐ฑ Bonus Tip: Make it Responsive (Optional)
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.
โ Final Output Preview
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
๐ฏ Conclusion
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.