then

How do you create a dropdown menu in HTML?

Dropdown menus are a common feature in websites and can help to organize and display information in a user-friendly way. Here are two ways to create a dropdown menu in HTML:

Method 1: Using HTML and CSS

The first method involves using HTML and CSS to create a dropdown menu. Here is an example:

 <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>


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

.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
}

.dropdown-content {
  display: none;
  position: absolute;
  z-index: 1;
}

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

.dropdown:hover .dropdown-content {
  display: block;
}

 

The above code creates a dropdown menu with the text "Dropdown" as the button. When the user hovers over the button, a list of links appears. Here is how the code works:

  • The HTML code creates a div element with class "dropdown". Inside the div, there is a button element with class "dropbtn" and a div element with class "dropdown-content". The div element contains three links.
  • The CSS code hides the dropdown-content div by default using the display:none property. When the user hovers over the dropdown div, the display property is changed to block, which makes the dropdown-content visible.

Method 2: Using Bootstrap

The second method involves using Bootstrap, which is a popular framework for building responsive websites. Here is an example:

 <div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Link 1</a>
    <a class="dropdown-item" href="#">Link 2</a>
    <a class="dropdown-item" href="#">Link 3</a>
  </div>
</div> 

The above code creates a dropdown menu with the text "Dropdown button" as the button. When the user clicks on the button, a list of links appears. Here is how the code works:

  • The HTML code creates a div element with class "dropdown". Inside the div, there is a button element with classes "btn btn-secondary dropdown-toggle". The button has several data attributes that enable the dropdown functionality.
  • The HTML code also creates a div element with class "dropdown-menu" and several aria attributes. Inside the div, there are three links.

Bootstrap provides several customization options for dropdown menus, such as different colors, sizes, and positioning. You can learn more about Bootstrap dropdown menus here.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe