Skip to main content

CSS Dropdowns

A CSS dropdown reveals a list of options or other content once the user clicks or hovers over an element, such as a button or a navigation link.

Two parts make it work. First comes a trigger element (like <div>, <button>, <p>, <a>, etc.), the thing the user clicks or hovers over.

Second comes the dropdown content: a container element (e.g. <div>) holding hidden material, which can be text, links, images, etc., until the trigger displays it.


CSS Dropdown Box with Text

In this first version, mousing over a <div> element brings up a dropdown box that holds some text.

Dropdown box with text

Prompt
Create a CSS dropdown box with text content. Use a container div with class "dropdown" that contains the text "Mouse over me!" as a trigger. Inside it, place a hidden div with class "dropdown-content" containing the text "Hello World!". The dropdown container should use position: relative. The dropdown content should be hidden by default (display: none), use position: absolute, have a light gray background (#f9f9f9), a minimum width of 130px, a box-shadow of 0px 8px 16px 0px rgba(0,0,0,0.2), and padding of 12px 16px. On hover of the dropdown container, the dropdown content should become visible (display: block).
Output: complete HTML page

Example Explained

  • For the dropdown content, which uses position:absolute, to land right below the trigger element, the .dropdown class needs position:relative.
  • Dropdown content lives in the .dropdown-content class, hidden by default until a hover displays it.
  • A min-width of 130px is set here, and you are free to change it! To match the width of the trigger element, use width at 100%, plus overflow:auto so that small screens can scroll.
  • The "card" look of the dropdown content comes from the box-shadow property, used in place of a border.
  • Finally, the :hover selector reveals the dropdown content once the user mouses over the dropdown <div> element.

CSS Dropdown Menu

A dropdown menu lets the user pick one option from a list of choices.

It builds on the previous example, and the only difference is a button plus links placed in the dropdown box, all styled to suit the dropdown button.

Dropdown menu with button

Prompt
Create a CSS dropdown menu with a button trigger. Use a container div with class "dropdown" (position: relative). Inside it, place a button with class "dropbtn" labeled "Dropdown Menu" styled with a green background (#4CAF50), white text, 16px padding, 16px font size, no border, and cursor pointer. Below the button, add a hidden div with class "dropdown-content" (display: none, position: absolute, background-color: #f9f9f9, min-width: 200px, box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2)) containing three links: "Link 1", "Link 2", and "Link 3". Style the links with black color, 12px 16px padding, no text-decoration, and display: block. On link hover, apply a light gray background (#f1f1f1). On hovering the dropdown container, show the dropdown content (display: block) and change the button background to a darker green (#3e8e41).
Output: complete HTML page