Dropdowns in CSS
?? Dropdowns in CSS
Dropdown menus let users pick options from a hidden list that appears on interaction (hover, click). While full dropdown functionality often uses JavaScript, you can create simple dropdowns using only CSS.
Basic CSS Dropdown Structure
<div class="dropdown"> <button class="dropbtn">Menu</button> <div class="dropdown-content"> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> </div></div>CSS for Dropdown
.dropdown { position: relative; display: inline-block;}.dropbtn { background-color: #3498db; color: white; padding: 10px 16px; border: none; cursor: pointer;}.dropdown-content { display: none; /* Hidden by default */ position: absolute; /* Positioned relative to .dropdown */ background-color: #f9f9f9; min-width: 160px; box-shadow: 0 8px 16px 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;}/* Show dropdown on hover */.dropdown:hover .dropdown-content { display: block;}/* Optional: change button background on hover */.dropdown:hover .dropbtn { background-color: #2980b9;}How It Works
.dropdownis the container, positioned relatively..dropdown-contentis hidden by default.On hovering
.dropdown, the.dropdown-contentbecomes visible.Links inside
.dropdown-contentare styled like a menu.
Notes
This CSS-only method works best on desktop (hover-based).
For click/tap interaction (mobile-friendly), JavaScript is usually needed.
You can customize dropdown styles with colors, animations, and positioning.
Want me to help you build a multi-level dropdown, or add animation effects to the dropdown?