Master creating horizontal and vertical navigation menus with modern CSS
Navigation bars are one of the most important elements of a website. They help users find content, understand site structure, and navigate between pages. Good navigation design improves user experience and accessibility.
Navigation menus should use semantic HTML with <nav>, <ul>, and <li> elements.
<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>
A vertical navigation bar displays menu items stacked vertically, typically used in sidebars.
/* Remove default list styling */
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
}
/* Style links */
nav a {
display: block;
padding: 12px 20px;
text-decoration: none;
color: #333;
background-color: #f4f4f4;
border-bottom: 1px solid #ddd;
}
/* Hover effect */
nav a:hover {
background-color: #667eea;
color: white;
}
/* Fixed vertical navigation */
.sidebar-nav {
position: fixed;
left: 0;
top: 0;
width: 250px;
height: 100vh;
background-color: #2c3e50;
overflow-y: auto;
}
.sidebar-nav ul {
list-style: none;
margin: 0;
padding: 0;
}
.sidebar-nav a {
display: block;
padding: 15px 25px;
color: white;
text-decoration: none;
border-bottom: 1px solid rgba(255,255,255,0.1);
transition: all 0.3s;
}
.sidebar-nav a:hover {
background-color: #34495e;
padding-left: 35px;
}
A horizontal navigation bar displays menu items side by side, commonly used for main site navigation.
/* Horizontal nav with inline-block */
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
}
nav li {
display: inline-block;
}
nav a {
display: block;
padding: 14px 20px;
text-decoration: none;
color: white;
}
nav a:hover {
background-color: #667eea;
}
/* Horizontal nav with Flexbox */
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
display: flex;
}
nav li {
/* flex items */
}
nav a {
display: block;
padding: 14px 20px;
text-decoration: none;
color: white;
transition: background-color 0.3s;
}
nav a:hover {
background-color: #667eea;
}
/* Horizontal nav with float */
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
overflow: auto; /* Clearfix */
}
nav li {
float: left;
}
nav a {
display: block;
padding: 14px 20px;
text-decoration: none;
color: white;
}
nav a:hover {
background-color: #667eea;
}
Highlight the current page to help users understand where they are in the site structure.
/* Active link styling */
nav a.active {
background-color: #667eea;
color: white;
font-weight: bold;
}
/* Alternative with border */
nav a.active {
border-bottom: 3px solid #667eea;
}
/* HTML usage */
<nav>
<ul>
<li><a href="index.html" class="active">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="services.html">Services</a></li>
</ul>
</nav>
Make navigation stay visible when scrolling for better user experience.
/* Fixed to top */
nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #333;
z-index: 1000;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
/* Add padding to body to prevent content hiding */
body {
padding-top: 60px; /* Height of navbar */
}
/* Sticky navigation */
nav {
position: sticky;
top: 0;
background-color: #333;
z-index: 100;
}
/* Becomes fixed when reaching top
No need for body padding */
Create dropdown submenus for hierarchical navigation.
<nav>
<ul>
<li><a href="#">Home</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>
/* Main navigation */
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
background: #333;
}
nav li {
position: relative;
}
nav a {
display: block;
padding: 14px 20px;
color: white;
text-decoration: none;
}
/* Dropdown menu */
.dropdown-menu {
display: none;
position: absolute;
top: 100%;
left: 0;
min-width: 200px;
background: #444;
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}
.dropdown-menu li {
width: 100%;
}
/* Show dropdown on hover */
.dropdown:hover .dropdown-menu {
display: block;
}
.dropdown-menu a:hover {
background-color: #667eea;
}
Create mobile-friendly navigation that adapts to small screens.
/* Mobile menu button (hamburger) */
.menu-toggle {
display: none;
background: #333;
color: white;
padding: 15px;
border: none;
cursor: pointer;
}
.menu-toggle span {
display: block;
width: 25px;
height: 3px;
background: white;
margin: 5px 0;
}
/* Desktop navigation */
nav ul {
display: flex;
list-style: none;
margin: 0;
padding: 0;
}
/* Mobile styles */
@media (max-width: 768px) {
.menu-toggle {
display: block;
}
nav ul {
display: none;
flex-direction: column;
width: 100%;
}
nav ul.active {
display: flex;
}
nav li {
width: 100%;
}
nav a {
border-bottom: 1px solid #444;
}
}
nav {
background: linear-gradient(135deg, #667eea, #764ba2);
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
nav ul {
display: flex;
justify-content: center;
list-style: none;
margin: 0;
padding: 0;
}
nav a {
display: block;
padding: 18px 30px;
color: white;
text-decoration: none;
font-weight: 500;
transition: all 0.3s;
}
nav a:hover {
background: rgba(255,255,255,0.2);
}
nav a.active {
background: rgba(255,255,255,0.3);
border-bottom: 3px solid white;
}
.sidebar {
width: 250px;
height: 100vh;
position: fixed;
left: 0;
top: 0;
background: #2c3e50;
padding-top: 60px;
}
.sidebar ul {
list-style: none;
padding: 0;
margin: 0;
}
.sidebar a {
display: block;
padding: 15px 30px;
color: #ecf0f1;
text-decoration: none;
border-left: 3px solid transparent;
transition: all 0.3s;
}
.sidebar a:hover {
background: #34495e;
border-left-color: #667eea;
padding-left: 40px;
}
.sidebar a.active {
background: #34495e;
border-left-color: #667eea;
}
.tabs {
display: flex;
border-bottom: 2px solid #ddd;
margin-bottom: 20px;
}
.tabs a {
padding: 12px 24px;
text-decoration: none;
color: #666;
border-bottom: 3px solid transparent;
transition: all 0.3s;
}
.tabs a:hover {
color: #667eea;
}
.tabs a.active {
color: #667eea;
border-bottom-color: #667eea;
font-weight: bold;
}
Q1: Which HTML element is semantically correct for navigation?
Q2: What is the modern recommended method for horizontal navigation?
Q3: How do you make navigation stay at the top while scrolling?
Q4: What property removes default list bullets/numbers?
Q5: For dropdown menus, which CSS property shows the submenu on hover?
list-style-type: none; margin: 0; padding: 0;display: flex (modern) or display: inline-block.active class to current page linkposition: fixed; top: 0; (stays on scroll)position: sticky; top: 0; (hybrid)position: absolute on submenu, display: none then display: block on hovertransition for smooth animations