In CSS, a dropdown is a component that allows users to select an option from a list. Dropdowns are commonly used in forms, navigation menus, and other user interface elements. They typically consist of a button or link that, when clicked, reveals a list of options.
Simple Dropdown Menu
simple example of a dropdown menu using HTML and CSS
<style> /* Basic styling for the dropdown container */ .dropdown { position: relative; display: inline-block; } /* Styling for the dropdown button */ .dropdown-button { background-color: #3498db; color: white; padding: 10px 20px; font-size: 16px; border: none; cursor: pointer; } /* Container for the dropdown content (hidden by default) */ .dropdown-content { display: none; position: absolute; background-color: #f1f1f1; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; } /* Individual dropdown items */ .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } /* Show the dropdown content when the button is hovered */ .dropdown:hover .dropdown-content { display: block; } /* Change the color of dropdown items on hover */ .dropdown-content a:hover { background-color: #ddd; } </style> <div class="dropdown"> <button class="dropdown-button">Dropdown</button> <div class="dropdown-content"> <a href="#">Option 1</a> <a href="#">Option 2</a> <a href="#">Option 3</a> </div> </div>
Dropdown Container (
.dropdown
): This div wraps the dropdown button and the content. It is positioned relative to allow the dropdown content to be positioned absolutely within this container.Dropdown Button (
.dropdown-button
): A button that users click to reveal the dropdown content. It has some basic styling to make it look like a button.Dropdown Content (
.dropdown-content
): This is the hidden menu that becomes visible when the button is hovered over. It contains links or items that users can select.Hover Effect: The dropdown content is hidden by default (
display: none
). When the user hovers over the dropdown button, the content is shown (display: block
).
Dropdown with CSS :hover
and :focus
Example using the :hover
and :focus
pseudo-classes for a more accessible dropdown
<style> .dropdown { position: relative; display: inline-block; } .dropdown-button { background-color: #2ecc71; color: white; padding: 10px 20px; 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; } .dropdown:hover .dropdown-content, .dropdown:focus-within .dropdown-content { display: block; } </style> <div class="dropdown" tabindex="0"> <button class="dropdown-button">Dropdown</button> <div class="dropdown-content"> <a href="#">Option A</a> <a href="#">Option B</a> <a href="#">Option C</a> </div> </div>
:hover
and:focus-within
: The dropdown content appears when the user hovers over the dropdown button or when the dropdown container receives focus (e.g., through keyboard navigation).tabindex="0"
: Allows the dropdown container to receive focus, making it more accessible for keyboard users.
Use Cases for Dropdowns
- Navigation Menus: Dropdowns are often used in navigation bars to group related links.
- Forms: In forms, dropdowns allow users to select an option from a predefined list (e.g., country selection).
- Interactive UIs: Dropdowns are used in complex UIs where users need to make selections from a list without navigating away from the current page.
Dropdowns are a fundamental UI component in web design, providing a clean and user-friendly way to present multiple options.