Categories
Web development

How to create a Dropdown Menu?

In my previous post, you learned how to create a navigation bar with CSS. Today I would like to show you how you can create a dropdown menu using CSS.

CSS Dropdown Menu

The CSS dropdown menu is a toggleable menu that allows the user to choose one of the values from a preferred list.

Example1. Hoverable Dropdown Menu:

Step 1: Add HTML

<div class="drop-down">
  <button class="drop-button">Dropdown Menu</button>
  <div class="drop-down-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>

Step 2: Add CSS

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.drop-button {
  background-color: #2a9d8f;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
}

.drop-down {
  position: relative;
  display: inline-block;
}

.drop-down-content {
  display: none;
  position: absolute;
  background-color: #f4a261;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.drop-down-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.drop-down-content a:hover {background-color: #e76f51;}

.drop-down:hover .drop-down-content {display: block;}

.drop-down:hover .drop-button {background-color: #e9c46a;}
</style>
</head>
<body>

<h2>Hoverable Dropdown Menu:</h2>
<p>Hover over the button to open the dropdown menu.</p>

<div class="drop-down">
  <button class="drop-button">Dropdown Menu</button>
  <div class="drop-down-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

</body>
</html>

Output:

Hoverable Dropdown Menu:

Hover over the button to open the dropdown menu.





You can create a dropdown menu that appears when the user moves the mouse over an element inside a navigation bar.  

Example2. Dropdown Menu inside a Navigation Bar:

Step 1: Add HTML

<div class="nav-bar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropDown">
    <button class="dropButton">Dropdown
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropDown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>

Step 2: Add CSS

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>

.nav-bar {
  overflow: hidden;
  background-color: #2a9d8f;
}

.nav-bar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropDown {
  float: left;
  overflow: hidden;
}

.dropDown .dropButton {
  font-size: 16px; 
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.nav-bar a:hover, .dropDown:hover  .dropButton {
  background-color: #e76f51;
}

.dropDown-content {
  display: none;
  position: absolute;
  background-color: #e9c46a;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropDown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropDown-content a:hover {
  background-color: #f4a261;
}

.dropDown:hover .dropDown-content {
  display: block;
}
</style>
</head>
<body>

<div class="nav-bar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropDown">
    <button class="dropButton">Dropdown
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropDown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>

</body>
</html>

Output:

Hover over the “Dropdown” link to see the dropdown menu.





Enjoy coding!

Read also:

CSS Fixed Sidebar Menu

CSS Fallback Fonts

How to create a responsive Navigation Bar?