CSS DropDowns


Make a drop-down menu appear when you hover over it using CSS..

Demo: Dropdown Examples

Hover your mouse over the samples below


Basic Dropdown

Make a drop-down box show up when a person moves their mouse over something.

Example

<style>
.dropdown {
  position: relative;
  display: inline-block;
}

.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;
}

.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>
Try it Yourself »

Example Explained

HTML) You can use any item, like a <span> or a <button>, to show the dropdown content.

Create a drop-down menu by using a container element (like <div>) such as a <div> . You can place any content you like inside this container.

Put a <div> box around the items to make sure the dropdown stuff lines up nicely using CSS.

CSS) The .dropdown class uses position:relative. This is important because it helps position the dropdown content directly under the dropdown button. This positioning is achieved using position:absolute.

The class called .dropdown-content holds the stuff that appears when you click on something. At first, you can't see it, but when you move your mouse over it, it shows up (look below). The smallest width it can be is 160 pixels, but you can make it wider if you want. Here's a tip: if you want the dropdown stuff to be as wide as the button you clicked, just set the width to 100%. And if you want to scroll on small screens, add overflow:auto.

Instead of drawing a line around it, we've employed a CSS property called box-shadow to give the dropdown menu the appearance of a "card."

The :hover selector in HTML is used to display the dropdown menu when the user hovers their mouse over the dropdown button.


Dropdown Menu

Create a dropdown menu that allows the user to choose an option from a list:

This is just like the earlier example, but now we've put links inside the dropdown box and made them look nice to match a styled dropdown button.

Example

<style>
/* Style The Dropdown Button */
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.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;
}

/* Links inside the dropdown */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
  display: block;
}

/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
</style>

<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>
Try it Yourself »

Right-aligned Dropdown Content



If you'd like the dropdown menu to appear from the right side rather than the left side, just include this: right: 0;

Example

.dropdown-content {
  right: 0;
}
Try it Yourself »

More Examples

Dropdown Image

Hover over the image:


Try it Yourself »

Dropdown Navbar

Try it Yourself »