Create a hoverable dropdown with CSS.
A dropdown box that appears when the user moves the mouse over an element.
You can use any element like a <span>
or <button>
to trigger the dropdown content.
Wrap the dropdown content inside a <div>
container to position it properly using CSS. You can include any elements you want inside the dropdown content area.
To make the dropdown menu appear correctly below the trigger, the outer container should use position: relative
, and the dropdown content should use position: absolute
.
The .dropdown-content
class holds the actual items shown in the menu. It’s hidden by default and only becomes visible when the user hovers over the dropdown trigger. By default, it has a minimum width of 160px, but you can adjust this to fit your layout.
If you want the dropdown to match the trigger’s width, use width: 100%
and add overflow: auto
to make it scrollable on smaller screens.
Instead of borders, you can use box-shadow
to give the dropdown a modern, card-like appearance.
The :hover
selector is used to display the dropdown content when the user hovers over the trigger button.
A dropdown menu that allows the user to choose an option from a list:
If you want the dropdown menu to go from right to left, instead of left to right, add right: 0;
How to add a dropdown menu inside a navigation bar