0
0
BootstrapHow-ToBeginner · 3 min read

How to Create Dropdown Button in Bootstrap Easily

To create a dropdown button in Bootstrap, use a button element with the class btn dropdown-toggle and wrap it inside a div with class dropdown. Add a ul with class dropdown-menu containing li items for dropdown options.
📐

Syntax

The dropdown button requires a container with class dropdown. Inside, a button with classes btn and dropdown-toggle triggers the menu. The data-bs-toggle="dropdown" attribute enables the toggle behavior. The menu items go inside a ul with class dropdown-menu, each item inside a li with an a tag.

html
<div class="dropdown">
  <button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown button
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Action</a></li>
    <li><a class="dropdown-item" href="#">Another action</a></li>
    <li><a class="dropdown-item" href="#">Something else here</a></li>
  </ul>
</div>
Output
A button labeled 'Dropdown button' that shows a list of three clickable options when clicked.
💻

Example

This example shows a complete dropdown button using Bootstrap 5. When you click the button, a menu with three options appears. This demonstrates the basic structure and required classes for a working dropdown.

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bootstrap Dropdown Button Example</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="container mt-5">
    <div class="dropdown">
      <button class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
        Dropdown button
      </button>
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
      </ul>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Output
A blue button labeled 'Dropdown button' on a white page. Clicking it reveals a menu with three clickable options: 'Action', 'Another action', and 'Something else here'.
⚠️

Common Pitfalls

  • Forgetting to include Bootstrap's JavaScript bundle will make the dropdown not work.
  • Missing the data-bs-toggle="dropdown" attribute on the button prevents the toggle behavior.
  • Not wrapping the button and menu inside a container with class dropdown breaks styling and functionality.
  • Using incorrect classes like dropdown-toggle on elements other than button or a can cause issues.
html
<!-- Wrong: Missing data-bs-toggle attribute -->
<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button">
    Dropdown
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Item 1</a></li>
  </ul>
</div>

<!-- Right: Includes data-bs-toggle attribute -->
<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Item 1</a></li>
  </ul>
</div>
📊

Quick Reference

  • Container: div.dropdown
  • Button: button.btn.dropdown-toggle with data-bs-toggle="dropdown"
  • Menu: ul.dropdown-menu with li > a.dropdown-item
  • Include Bootstrap CSS and JS: Required for styling and toggle functionality

Key Takeaways

Use a div with class dropdown to wrap the button and menu.
Add btn dropdown-toggle classes and data-bs-toggle="dropdown" to the button for toggle behavior.
Place menu items inside a ul with class dropdown-menu and each item inside li with a.dropdown-item.
Include Bootstrap's CSS and JavaScript bundle for proper styling and dropdown functionality.
Common mistakes include missing the toggle attribute or Bootstrap JS, which breaks the dropdown.