A dropdown button lets users pick one option from a list that appears when they click the button. It saves space and keeps the page clean.
Dropdown button basics in Bootsrap
<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>
The data-bs-toggle="dropdown" attribute makes the button open the menu.
Use aria-expanded for accessibility to show if the menu is open or closed.
<div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false"> Choose option </button> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Option 1</a></li> <li><a class="dropdown-item" href="#">Option 2</a></li> </ul> </div>
<div class="dropdown"> <button class="btn btn-success dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false"> Actions </button> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Save</a></li> <li><a class="dropdown-item" href="#">Delete</a></li> <li><a class="dropdown-item" href="#">Edit</a></li> </ul> </div>
This page shows a blue dropdown button labeled "Select Fruit". When clicked, it shows three fruit options. The button uses Bootstrap 5 styles and scripts for the dropdown to work.
<!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> <main class="p-4"> <h1>Dropdown Button Basics</h1> <div class="dropdown"> <button class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false"> Select Fruit </button> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Apple</a></li> <li><a class="dropdown-item" href="#">Banana</a></li> <li><a class="dropdown-item" href="#">Cherry</a></li> </ul> </div> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Make sure to include Bootstrap's CSS and JavaScript files for the dropdown to work.
Dropdown menus close automatically when you click outside or select an option.
Use semantic HTML and ARIA attributes for better accessibility.
Dropdown buttons help keep your page clean by hiding options until needed.
Bootstrap makes it easy to create dropdowns with simple classes and attributes.
Always include Bootstrap's JavaScript for the dropdown toggle to function.
Practice
dropdown button in Bootstrap?Solution
Step 1: Understand dropdown button purpose
A dropdown button groups several options under one button to save space and keep the page tidy.Step 2: Compare options
Only To hide multiple options under one button for a cleaner page describes hiding options under one button, which matches the dropdown's purpose.Final Answer:
To hide multiple options under one button for a cleaner page -> Option DQuick Check:
Dropdown button = hides options [OK]
- Thinking dropdowns only change button color
- Confusing dropdowns with static lists
- Assuming dropdowns add animations automatically
Solution
Step 1: Identify required classes for dropdown toggle
Bootstrap dropdown toggle buttons needbtn, a style class likebtn-primary, anddropdown-toggleto work properly.Step 2: Check options for correct class order and completeness
btn btn-primary dropdown-toggle has all three classes in correct order:btn btn-primary dropdown-toggle. Others miss or misorder classes.Final Answer:
btn btn-primary dropdown-toggle -> Option CQuick Check:
Dropdown toggle = btn + style + dropdown-toggle [OK]
- Omitting btn or dropdown-toggle class
- Wrong class order causing no toggle effect
- Using non-existent classes like btn-toggle
Solution
Step 1: Understand Bootstrap dropdown dependency
Bootstrap dropdowns require JavaScript to handle the toggle action when clicking the button.Step 2: Predict behavior without JavaScript
Without JavaScript, clicking the button won't trigger the menu to open, so the dropdown stays closed.Final Answer:
The dropdown menu will not open when clicking the button -> Option AQuick Check:
Missing JS = no dropdown open [OK]
- Thinking CSS alone opens dropdown
- Assuming menu appears without JS
- Confusing color change with dropdown functionality
<button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown">Menu</button> <div class="dropdown-menu"> <a class="dropdown-item" href="#">Action</a> </div>
Solution
Step 1: Check Bootstrap 5 attribute syntax
Bootstrap 5 usesdata-bs-toggle="dropdown"instead of the olderdata-toggle="dropdown".Step 2: Verify other parts
Button type can be "button";dropdown-menucan be a div;awithdropdown-itemis correct.Final Answer:
The attribute should be data-bs-toggle="dropdown" instead of data-toggle="dropdown" -> Option BQuick Check:
Bootstrap 5 uses data-bs-toggle [OK]
- Using old data-toggle attribute
- Changing button type unnecessarily
- Replacing div with ul incorrectly
- Misusing dropdown-item on wrong elements
Solution
Step 1: Confirm correct attribute for Bootstrap 5
Bootstrap 5 requiresdata-bs-toggle="dropdown"for toggling dropdowns, notdata-toggle.Step 2: Check element types for accessibility and behavior
Usingbuttonelements withdropdown-toggleandaorbuttontags withdropdown-iteminsidedropdown-menuensures keyboard accessibility and closes menu on click.Step 3: Evaluate options
Only C usesdata-bs-toggle="dropdown"(BS5 correct) with properbutton.dropdown-toggleanda.dropdown-itemthat auto-closes the menu.Final Answer:
Usebuttonwithdropdown-toggleanddata-bs-toggle="dropdown", andatags withdropdown-iteminsidedropdown-menu-> Option AQuick Check:
data-bs-toggle + dropdown-item = auto close [OK]
- Using old data-toggle instead of data-bs-toggle
- Omitting data-bs-toggle
- Using
atag for toggle orspanfor items
Ready to prove it?
Pass the test and this pattern moves unknown
