Animated Navbar in CSS
An animated navbar enhances the user experience by providing visual feedback during navigation. You can use simple CSS transitions or keyframe animations to animate links, indicators, backgrounds, or mobile toggles.
1. Basic Navbar Structure
2. Hover Underline Animation
Use pseudo-elements and transitions for a smooth underline effect on hover:
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
background-color: #1e3a8a;
color: white;
}
.nav-links {
display: flex;
gap: 1.5rem;
}
.nav-links a {
position: relative;
color: white;
text-decoration: none;
font-weight: 500;
}
.nav-links a::after {
content: '';
position: absolute;
width: 0%;
height: 2px;
background-color: white;
bottom: -4px;
left: 0;
transition: width 0.3s ease;
}
.nav-links a:hover::after {
width: 100%;
}
3. Mobile Toggle Animation (Optional)
Add an animated hamburger icon and slide-in menu for small screens:
.menu-toggle {
display: none;
flex-direction: column;
gap: 5px;
cursor: pointer;
}
.menu-toggle span {
width: 25px;
height: 3px;
background: white;
transition: all 0.3s ease;
}
/* Animate into X */
.menu-toggle.active span:nth-child(1) {
transform: rotate(45deg) translateY(8px);
}
.menu-toggle.active span:nth-child(2) {
opacity: 0;
}
.menu-toggle.active span:nth-child(3) {
transform: rotate(-45deg) translateY(-8px);
}
Combine this with JavaScript or Tailwindβs class toggling to animate open/close actions.
4. Keyframe Animation (Slide In)
You can animate nav items appearing on load with @keyframes
:
@keyframes slideIn {
0% {
transform: translateY(-20px);
opacity: 0;
}
100% {
transform: translateY(0);
opacity: 1;
}
}
.nav-links li {
animation: slideIn 0.5s ease forwards;
}
.nav-links li:nth-child(2) {
animation-delay: 0.1s;
}
.nav-links li:nth-child(3) {
animation-delay: 0.2s;
}
Conclusion
An animated navbar adds a polished feel to your website. From subtle hovers to full entrance animations, CSS lets you enhance interactivity without JavaScript. Combine transitions and keyframes for even more control.