CHAPTER 33
CSS Dropdowns
Learn how to create dropdown menus using CSS positioning and hover states.
01. What is CSS Dropdowns?
A dropdown menu displays additional options when the user interacts with a menu item.
CSS positioning is commonly used to place the dropdown content.
The :hover pseudo-class can be used to show a basic dropdown.
02. Syntax
.dropdown {
position: relative;
}
.dropdown-menu {
position: absolute;
display: none;
}
.dropdown:hover .dropdown-menu {
display: block;
}
03. Example
CSS / HTML Example
AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-btn {
padding: 12px 20px;
background: #2563eb;
color: white;
border: none;
}
.dropdown-menu {
display: none;
position: absolute;
top: 100%;
left: 0;
min-width: 180px;
background: white;
box-shadow: 0 10px 25px rgba(0,0,0,.15);
}
.dropdown-menu a {
display: block;
padding: 12px;
color: #111827;
text-decoration: none;
}
.dropdown-menu a:hover {
background: #eff6ff;
}
.dropdown:hover .dropdown-menu {
display: block;
}
</style>
</head>
<body>
<div class="dropdown">
<button class="dropdown-btn">
Courses
</button>
<div class="dropdown-menu">
<a href="#">HTML</a>
<a href="#">CSS</a>
<a href="#">JavaScript</a>
</div>
</div>
</body>
</html>
04. Important Points
✓
Use position: relative on the dropdown parent.
✓
Use position: absolute for the dropdown menu.
✓
z-index may be required when the menu overlaps other elements.
✓
Mobile dropdowns should also be accessible without hover.
05. Common Mistakes
⚠
Dropdown disappearing before the user can click.
⚠
Not using enough padding for menu items.
⚠
Making dropdowns dependent only on mouse hover.
⚠
Forgetting z-index when other elements overlap the menu.
06. Real World Use
Dropdowns are widely used in website navigation, course categories, account menus and admin dashboards.
Ready to Practice? 🚀
Open the AIFA CSS Editor and practice this chapter live.
Try It Yourself →
Practice Exercise
Create a Course Dropdown
Create a Courses dropdown containing HTML, CSS, JavaScript and PHP links.