How to Fix Dropdown Not Working in Bootstrap Quickly
Bootstrap JavaScript or Popper.js is missing or not loaded properly. Ensure you include both the Bootstrap CSS and JavaScript files, plus Popper.js, in the correct order for the dropdown to function.Why This Happens
The dropdown in Bootstrap relies on JavaScript to toggle the menu when clicked. If the Bootstrap JavaScript or its dependency Popper.js is missing, the dropdown won't open. Also, incorrect HTML structure or missing data-bs-toggle="dropdown" attribute can cause it to fail.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <title>Broken Dropdown</title> </head> <body> <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button"> 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> </ul> </div> <!-- Missing Bootstrap JS and Popper.js --> </body> </html>
The Fix
Include the Bootstrap JavaScript bundle which contains Popper.js, and add the data-bs-toggle="dropdown" attribute to the button. This enables the dropdown toggle functionality.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <title>Fixed Dropdown</title> </head> <body> <div class="dropdown"> <button class="btn btn-secondary 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> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Prevention
Always include the Bootstrap JavaScript bundle after the CSS in your HTML. Use the official CDN links or local files correctly. Make sure to add data-bs-toggle="dropdown" on dropdown toggle buttons. Test your dropdowns early to catch missing scripts or markup errors.
Related Errors
Other common Bootstrap dropdown issues include:
- Missing
aria-expandedattribute causing accessibility problems. - Conflicts with other JavaScript libraries blocking Bootstrap scripts.
- Incorrect HTML nesting breaking dropdown structure.
Quick fixes involve checking script order, removing conflicting scripts, and validating HTML structure.