Creating a responsive navbar with animation is a great way to make your website look more dynamic and user-friendly! Let’s build a simple, animated navbar that looks great on both desktop and mobile devices.
We’ll start by creating the HTML for our navbar. It will contain a logo, navigation links, and a hamburger menu for mobile views.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Navbar</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- Navbar -->
<nav class="navbar">
<div class="logo">MyLogo</div>
<ul class="nav-links">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
<div class="hamburger" onclick="toggleMenu()">☰</div>
</nav>
<script src="script.js"></script>
</body>
</html>
Explanation:
The nav contains a logo and a list of navigation links.
The hamburger icon will appear on smaller screens and toggle the menu when clicked.
Now, let’s style the navbar and make it responsive with a simple hamburger menu animation.
/* General Styles */
body {
margin: 0;
font-family: Arial, sans-serif;
}
/* Navbar Styles */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
background-color: #333;
padding: 10px 20px;
color: white;
}
.navbar .logo {
font-size: 24px;
font-weight: bold;
}
/* Navigation Links */
.nav-links {
list-style-type: none;
display: flex;
gap: 20px;
}
.nav-links li a {
text-decoration: none;
color: white;
font-size: 18px;
transition: color 0.3s ease;
}
/* Hover Effect */
.nav-links li a:hover {
color: #ff5733;
}
/* Hamburger Menu (visible only on mobile) */
.hamburger {
display: none;
font-size: 30px;
cursor: pointer;
}
/* Responsive Styles (for mobile) */
@media (max-width: 768px) {
.nav-links {
position: absolute;
top: 60px;
right: -100%;
width: 200px;
height: 100vh;
background-color: #333;
flex-direction: column;
justify-content: center;
align-items: center;
transition: right 0.3s ease-in-out;
}
.nav-links.active {
right: 0;
}
/* Show hamburger menu on mobile */
.hamburger {
display: block;
}
}
Explanation:
We’ve used Flexbox to align the navbar elements and space them out.
The hamburger menu appears when the screen width is 768px or smaller.
The navigation links are initially hidden off-screen and will slide in when the menu is toggled (animated with transition).
Now, let’s add some interactivity using JavaScript to toggle the visibility of the menu when the hamburger icon is clicked.
function toggleMenu() {
const navLinks = document.querySelector('.nav-links');
navLinks.classList.toggle('active'); // Toggle the 'active' class to show/hide the menu
}
Explanation:
The toggleMenu() function adds or removes the active class to the nav-links when the hamburger is clicked.
The .active class makes the menu slide in from the right.
Here’s how everything comes together:
On desktop, the navbar shows the links inline.
On mobile, the hamburger icon appears. When clicked, the navbar links slide in from the right with a smooth animation.
Copy and paste the HTML, CSS, and JavaScript into your own project.
Resize the browser window to see the hamburger menu in action on mobile view.
You can change the animation speed by adjusting the transition value in the CSS!
Color Animation: Add a color change to the navbar when scrolling.
Smooth Scrolling: Add smooth scrolling when links are clicked to jump to sections on the page.