Back to Web Development
2026-01-045 min read

CSS Dropdowns (Web Development)

Learn CSS Dropdowns (Web Development) step by step with clear examples and exercises.

Title: CSS Dropdowns (Web Development)

Why This Matters

Dropdown menus are a common and essential feature on many websites, allowing users to navigate between pages or access additional options with minimal screen space. In this tutorial, we'll learn how to create dropdown menus using CSS, making your website more user-friendly and interactive.

Prerequisites

Before diving into the core concept, you should have a basic understanding of:

  • HTML (Hypertext Markup Language)
  • CSS (Cascading Style Sheets)
  • The DOM (Document Object Model)

Core Concept

To create a dropdown menu using CSS, we'll need three key elements:

  1. A parent element containing the dropdown menu items.
  2. Child elements representing each item in the dropdown menu.
  3. CSS to style and position the parent and child elements.

Here's an example of HTML structure for a simple dropdown menu:

<div class="dropdown">
<button class="dropbtn">Dropdown Menu</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>

In this example, we have a div element with the class "dropdown" that contains two child elements:

  • A button with the class "dropbtn". This will serve as the trigger for the dropdown menu.
  • Another div with the class "dropdown-content", which holds the actual links in the dropdown menu.

Now, let's add some CSS to style and position our dropdown menu:

.dropdown {
position: relative;
display: inline-block;
}

.dropbtn {
background-color: #4CAF50;
color: white;
padding: 15px 32px;
font-size: 16px;
border: none;
cursor: pointer;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}

.dropdown-content a:hover {
background-color: #f1f1f1;
}

In the CSS, we've defined styles for the parent and child elements as well as the dropdown menu itself. The position: relative on the parent element ensures that the child dropdown-content is positioned correctly relative to it. We've also set the initial display of the dropdown-content to "none" so that it's hidden by default.

When the user clicks on the dropbtn, we want the dropdown-content to appear. To achieve this, we can add a CSS transition:

.dropdown:hover .dropdown-content {
display: block;
}

With these styles in place, our dropdown menu should now work as expected!

Worked Example

Let's create a more complex dropdown menu with multiple levels and submenus. In this example, we'll have a main navigation bar with two dropdown menus: "Services" and "About Us".

<nav>
<ul>
<li><a href="#">Home</a></li>
<li class="dropdown">
<button class="dropbtn">Services</button>
<div class="dropdown-content">
<a href="#">Web Development</a>
<a href="#">Graphic Design</a>
<a href="#">SEO Services</a>
<a href="#">Digital Marketing</a>
<li class="dropdown">
<button class="dropbtn">Submenu</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</li>
</div>
</li>
<li class="dropdown">
<button class="dropbtn">About Us</button>
<div class="dropdown-content">
<a href="#">Our Team</a>
<a href="#">Contact Us</a>
<a href="#">Careers</a>
</div>
</li>
</ul>
</nav>

Now, let's add the CSS to style our navigation bar and dropdown menus:

nav {
background-color: #333;
}

nav ul {
list-style-type: none;
padding: 0;
overflow: hidden;
}

nav li {
float: left;
}

nav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

nav li a:hover {
background-color: #111;
}

.dropdown {
position: relative;
display: inline-block;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}

.dropdown-content a:hover {
background-color: #f1f1f1;
}

With these styles in place, our navigation bar and dropdown menus should now work as expected!

Common Mistakes

  1. Not positioning the dropdown-content correctly: Make sure to use position: relative on the parent element of the dropdown menu.
  2. Displaying the dropdown-content by default: Set the initial display of the dropdown-content to "none" so that it's hidden by default.
  3. Not handling mouse events properly: To make sure the dropdown menu closes when the user clicks outside of it, add an event listener for click events on the document and hide the current open dropdown menu if the clicked element is not a dropdown button or its child elements.
  4. Not considering mobile devices: Make sure your dropdown menus are also accessible and usable on mobile devices by using media queries to adjust their styles based on screen size.

Practice Questions

  1. Modify the example above to add a third dropdown menu named "Contact Us" with links to our email, phone number, and social media profiles.
  2. Create a dropdown menu for a portfolio website that displays thumbnails of your latest projects when clicked.
  3. Implement a responsive dropdown menu that expands into a full-width navigation bar on smaller screens.

FAQ

  1. Why use CSS to create dropdown menus instead of JavaScript? While JavaScript can certainly be used to create dropdown menus, using CSS allows for smoother animations and easier styling without the need for additional libraries or complex code.
  2. How do I prevent the dropdown menu from closing when clicking on a link inside it? To keep the dropdown menu open when clicking on a link, you can add an event listener for click events on the links inside the dropdown menu and prevent the default action of navigating away from the page.
  3. How do I make my dropdown menu accessible to screen readers? To ensure your dropdown menu is accessible to screen readers, give each dropdown button a descriptive aria-label attribute that describes the contents of the dropdown menu and use proper HTML structure, such as using ` for the dropdown trigger and ` for the dropdown items.
CSS Dropdowns (Web Development) | Web Development | XQA Learn