- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Dropdowns
Creating a dropdown menu in CSS allows you to hide a predefined list of options within a button or other elements, keeping your user interface clean and uncluttered. While many developers reach for JavaScript frameworks to handle menus, you can achieve professional, high-performance dropdowns using pure CSS. Let’s explore two common approaches for creating dropdown menus:
Hoverable Dropdown:
A hoverable dropdown appears when the user moves the mouse over an element. This is perfect for "Tooltips" or simple informational overlays. Here’s a simple example:
HTML
<style>
/* The container holds both the trigger and the content */
.dropdown {
position: relative;
display: inline-block;
}
/* The hidden content box */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
padding: 12px 16px;
z-index: 1; /* Ensures the menu sits on top of other elements */
}
/* Show the content when the user hovers over the container */
.dropdown:hover .dropdown-content {
display: block;
}
</style>
<div class="dropdown">
<span>Mouse over me</span>
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>
In this example:
- The
.dropdownclass usesposition: relative. This is crucial because it acts as the "anchor" for the hidden content. - The
.dropdown-contentclass usesposition: absolute. This allows it to float right under the trigger without pushing other elements on the page around. - The magic happens with the
:hoverpseudo-class. When the parent is hovered, we change the child'sdisplayproperty fromnonetoblock.
position: relative on the parent container. Without it, the absolutely positioned dropdown will align itself to the whole browser window instead of the button!
Dropdown Menu with Links:
In real-world applications, dropdowns are most commonly used in navigation bars. If you want a dropdown menu with clickable links, consider the following example:
HTML
<style>
/* Style The Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
border-radius: 4px;
}
/* Container for Dropdown Content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #ffffff;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
border-radius: 4px;
overflow: hidden; /* Ensures child links don't spill over rounded corners */
}
/* Links inside the Dropdown */
.dropdown-content a {
color: #333;
padding: 12px 16px;
text-decoration: none;
display: block; /* Makes the entire row clickable */
}
/* Change Link Color on Hover */
.dropdown-content a:hover {
background-color: #f1f1f1;
}
/* Show Dropdown Menu on Hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change Button Background Color when Dropdown Content is Shown */
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
</style>
<div class="dropdown">
<button class="dropbtn">Settings</button>
<div class="dropdown-content">
<a href="#">Account Profile</a>
<a href="#">Privacy Settings</a>
<a href="#">Logout</a>
</div>
</div>
In this example:
- The
.dropbtnclass styles the button that triggers the dropdown. We usecursor: pointerto signal to the user that it’s interactive. - Inside
.dropdown-content a, we setdisplay: block. This is a vital UX improvement because it makes the entire width of the menu clickable, rather than just the text. - The
box-shadowadds a layer of depth, making the menu appear to "float" above the rest of the content.
<button> element for the dropdown trigger rather than a <div> or <span>. Buttons are natively focusable via the keyboard, which is essential for web accessibility (A11y).