Navigation Menus With CSS and JavaScript_ Comprehensive Guide
Navigation Menus With CSS and JavaScript_ Comprehensive Guide
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
1
4. Hamburger Menu (Responsive) 6
Detailed Examples 7
Example 1: Highlight Active Menu Item 7
Example 2: Smooth Scroll for Single Page Navigation 8
Exercises 8
Exercise 1: Create a Vertical Dropdown Menu 8
Exercise 2: Make the Hamburger Menu Sticky 8
Exercise 3: Add a Search Bar 8
Multiple-Choice Questions 8
Question 1: 9
Question 2: 9
Question 3: 9
Best Practices 9
Navigation menus are a key component of web design, offering users a way to move through a
website. This guide explains how to create dynamic and responsive navigation menus using
CSS and JavaScript. It includes code examples, detailed explanations, exercises, and
multiple-choice questions.
1. Horizontal Navigation: Menus aligned horizontally at the top or bottom of the page.
2. Vertical Navigation: Menus aligned vertically, often on the side of the page.
3. Dropdown Menus: Menus with sub-items that expand when hovered or clicked.
4. Hamburger Menus: Compact menus, often used in mobile design.
HTML:
<nav class="nav">
<ul>
<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>
</nav>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
CSS:
.nav ul {
list-style: none;
padding: 0;
margin: 0;
display: flex;
justify-content: center;
background-color: #333;
}
.nav li {
margin: 0 10px;
}
.nav a {
color: white;
text-decoration: none;
padding: 10px 15px;
display: block;
transition: background-color 0.3s;
}
.nav a:hover {
background-color: #555;
}
HTML:
<nav class="vertical-nav">
<ul>
<li><a href="#">Dashboard</a></li>
<li><a href="#">Profile</a></li>
<li><a href="#">Settings</a></li>
<li><a href="#">Logout</a></li>
</ul>
</nav>
CSS:
.vertical-nav ul {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
3
list-style: none;
padding: 0;
margin: 0;
width: 200px;
background-color: #f4f4f4;
}
.vertical-nav li {
margin: 0;
}
.vertical-nav a {
text-decoration: none;
color: #333;
display: block;
padding: 10px;
border-bottom: 1px solid #ddd;
transition: background-color 0.3s;
}
.vertical-nav a:hover {
background-color: #ddd;
}
3. Dropdown Menu
HTML:
<nav class="dropdown-nav">
<ul>
<li><a href="#">Home</a></li>
<li>
<a href="#">Services</a>
<ul class="dropdown">
<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>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
4
</nav>
CSS:
.dropdown-nav ul {
list-style: none;
padding: 0;
margin: 0;
display: flex;
background-color: #333;
}
.dropdown-nav li {
position: relative;
}
.dropdown-nav a {
color: white;
text-decoration: none;
padding: 10px 15px;
display: block;
transition: background-color 0.3s;
}
.dropdown-nav a:hover {
background-color: #555;
}
.dropdown {
display: none;
position: absolute;
top: 100%;
left: 0;
background-color: #444;
}
.dropdown li {
width: 150px;
}
.dropdown-nav li:hover .dropdown {
display: block;
}
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
4. Hamburger Menu (Responsive)
HTML:
<nav class="hamburger-nav">
<div class="hamburger" id="hamburger">
☰
</div>
<ul class="menu" id="menu">
<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>
</nav>
CSS:
.hamburger-nav {
position: relative;
}
.hamburger {
font-size: 24px;
cursor: pointer;
display: none;
}
.menu {
list-style: none;
padding: 0;
margin: 0;
display: flex;
background-color: #333;
}
.menu li {
margin: 0 10px;
}
.menu a {
color: white;
text-decoration: none;
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
padding: 10px 15px;
display: block;
}
@media (max-width: 768px) {
.hamburger {
display: block;
}
.menu {
display: none;
flex-direction: column;
}
.menu.active {
display: flex;
}
}
JavaScript:
Detailed Examples
CSS:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
7
.nav a.active {
background-color: #007bff;
}
Exercises
● Create a vertical navigation menu with a dropdown for one of the items.
● Show the dropdown menu when hovered.
● Modify the hamburger menu to remain at the top of the page when scrolling.
● Add a search bar to a horizontal navigation menu that filters menu items based on user
input.
Multiple-Choice Questions
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
8
Question 1:
1. visibility: hidden;
2. display: none;
3. opacity: 0;
4. overflow: hidden;
Question 2:
1. classList.add()
2. classList.toggle()
3. classList.replace()
4. classList.contains()
Answer: 2. classList.toggle()
Question 3:
Best Practices
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis