Bird
Raised Fist0
Bootsrapmarkup~10 mins

Dropdown button basics in Bootsrap - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic Bootstrap dropdown button.

Bootsrap
<div class="dropdown">
  <button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="[1]" aria-expanded="false">
    Dropdown button
  </button>
  <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <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>
Drag options to blanks, or click blank then click option'
Amodal
Btooltip
Ccollapse
Ddropdown
Attempts:
3 left
💡 Hint
Common Mistakes
Using data-bs-toggle="modal" instead of "dropdown"
Forgetting the data-bs-toggle attribute
Using incorrect Bootstrap classes on the button
2fill in blank
medium

Complete the code to add the correct class to the dropdown menu for proper styling.

Bootsrap
<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown
  </button>
  <ul class="[1]" aria-labelledby="dropdownMenu2">
    <li><a class="dropdown-item" href="#">Option 1</a></li>
    <li><a class="dropdown-item" href="#">Option 2</a></li>
  </ul>
</div>
Drag options to blanks, or click blank then click option'
Adropdown-menu
Bbtn-group
Cdropdown-toggle
Ddropdown-item
Attempts:
3 left
💡 Hint
Common Mistakes
Using "dropdown-toggle" on the menu instead of the button
Using "dropdown-item" on the menu container
Omitting the class entirely
3fill in blank
hard

Fix the error in the button element to make the dropdown accessible by keyboard.

Bootsrap
<button class="btn btn-success dropdown-toggle" type="button" id="dropdownMenu3" data-bs-toggle="dropdown" aria-expanded="[1]">
  Dropdown
</button>
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
Cexpanded
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Setting aria-expanded to "true" when dropdown is closed
Using invalid values like "expanded" or "open"
Omitting aria-expanded attribute
4fill in blank
hard

Fill both blanks to create a split dropdown button with correct classes.

Bootsrap
<div class="btn-group">
  <button type="button" class="btn btn-primary">Main Action</button>
  <button type="button" class="btn btn-primary dropdown-toggle [1]" data-bs-toggle="dropdown" aria-expanded="false">
    <span class="[2]"></span>
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Action 1</a></li>
    <li><a class="dropdown-item" href="#">Action 2</a></li>
  </ul>
</div>
Drag options to blanks, or click blank then click option'
Adropdown-toggle-split
Bdropdown-menu
Cvisually-hidden
Ddropdown-item
Attempts:
3 left
💡 Hint
Common Mistakes
Using "dropdown-menu" class on the toggle button
Not using "dropdown-toggle-split" for the split button
Missing "visually-hidden" class on the span
5fill in blank
hard

Fill all three blanks to create a dropdown button with a custom label and an item that is disabled.

Bootsrap
<div class="dropdown">
  <button class="btn btn-warning dropdown-toggle" type="button" id="dropdownMenu4" data-bs-toggle="dropdown" aria-expanded="false">
    [1]
  </button>
  <ul class="dropdown-menu" aria-labelledby="dropdownMenu4">
    <li><a class="dropdown-item" href="#">[2]</a></li>
    <li><a class="dropdown-item [3]" href="#" tabindex="-1" aria-disabled="true">Disabled Item</a></li>
  </ul>
</div>
Drag options to blanks, or click blank then click option'
ACustom Dropdown
BActive Item
Cdisabled
Ddropdown-toggle
Attempts:
3 left
💡 Hint
Common Mistakes
Using "dropdown-toggle" as a class on the disabled item
Not adding the "disabled" class to the disabled item
Forgetting to set aria-disabled="true" on the disabled item

Practice

(1/5)
1. What is the main purpose of a dropdown button in Bootstrap?
easy
A. To create a button that changes color on hover
B. To add animations to buttons automatically
C. To display a static list of links on the page
D. To hide multiple options under one button for a cleaner page

Solution

  1. Step 1: Understand dropdown button purpose

    A dropdown button groups several options under one button to save space and keep the page tidy.
  2. 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.
  3. Final Answer:

    To hide multiple options under one button for a cleaner page -> Option D
  4. Quick Check:

    Dropdown button = hides options [OK]
Hint: Dropdowns hide options under one button for neatness [OK]
Common Mistakes:
  • Thinking dropdowns only change button color
  • Confusing dropdowns with static lists
  • Assuming dropdowns add animations automatically
2. Which of the following is the correct Bootstrap class to create a dropdown toggle button?
easy
A. dropdown-toggle btn-primary
B. btn dropdown-toggle
C. btn btn-primary dropdown-toggle
D. btn-toggle dropdown

Solution

  1. Step 1: Identify required classes for dropdown toggle

    Bootstrap dropdown toggle buttons need btn, a style class like btn-primary, and dropdown-toggle to work properly.
  2. 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.
  3. Final Answer:

    btn btn-primary dropdown-toggle -> Option C
  4. Quick Check:

    Dropdown toggle = btn + style + dropdown-toggle [OK]
Hint: Use btn + style + dropdown-toggle classes together [OK]
Common Mistakes:
  • Omitting btn or dropdown-toggle class
  • Wrong class order causing no toggle effect
  • Using non-existent classes like btn-toggle
3. What will happen if you omit Bootstrap's JavaScript file when using a dropdown button?
medium
A. The dropdown menu will not open when clicking the button
B. The dropdown menu will open but with no styling
C. The dropdown button will change color on click but no menu appears
D. The dropdown menu will open automatically on page load

Solution

  1. Step 1: Understand Bootstrap dropdown dependency

    Bootstrap dropdowns require JavaScript to handle the toggle action when clicking the button.
  2. Step 2: Predict behavior without JavaScript

    Without JavaScript, clicking the button won't trigger the menu to open, so the dropdown stays closed.
  3. Final Answer:

    The dropdown menu will not open when clicking the button -> Option A
  4. Quick Check:

    Missing JS = no dropdown open [OK]
Hint: Bootstrap dropdown needs JS to open menu on click [OK]
Common Mistakes:
  • Thinking CSS alone opens dropdown
  • Assuming menu appears without JS
  • Confusing color change with dropdown functionality
4. Identify the error in this dropdown button code snippet:
<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>
medium
A. The dropdown-menu div should be a ul element
B. The attribute should be data-bs-toggle="dropdown" instead of data-toggle="dropdown"
C. The button is missing the type="submit" attribute
D. The dropdown-item class should be on a button element, not a

Solution

  1. Step 1: Check Bootstrap 5 attribute syntax

    Bootstrap 5 uses data-bs-toggle="dropdown" instead of the older data-toggle="dropdown".
  2. Step 2: Verify other parts

    Button type can be "button"; dropdown-menu can be a div; a with dropdown-item is correct.
  3. Final Answer:

    The attribute should be data-bs-toggle="dropdown" instead of data-toggle="dropdown" -> Option B
  4. Quick Check:

    Bootstrap 5 uses data-bs-toggle [OK]
Hint: Use data-bs-toggle for Bootstrap 5 dropdowns [OK]
Common Mistakes:
  • Using old data-toggle attribute
  • Changing button type unnecessarily
  • Replacing div with ul incorrectly
  • Misusing dropdown-item on wrong elements
5. You want a dropdown button that shows a list of user actions and closes the menu when an item is clicked. Which Bootstrap setup ensures this behavior?
hard
A. Use button with dropdown-toggle and data-bs-toggle="dropdown", and a tags with dropdown-item inside dropdown-menu
B. Use button with dropdown-toggle but omit data-bs-toggle, and use div tags inside dropdown-menu
C. Use button with dropdown-toggle and data-toggle="dropdown", and button tags with dropdown-item inside dropdown-menu
D. Use a tag with dropdown-toggle and data-bs-toggle="dropdown", and span tags inside dropdown-menu

Solution

  1. Step 1: Confirm correct attribute for Bootstrap 5

    Bootstrap 5 requires data-bs-toggle="dropdown" for toggling dropdowns, not data-toggle.
  2. Step 2: Check element types for accessibility and behavior

    Using button elements with dropdown-toggle and a or button tags with dropdown-item inside dropdown-menu ensures keyboard accessibility and closes menu on click.
  3. Step 3: Evaluate options

    Only C uses data-bs-toggle="dropdown" (BS5 correct) with proper button.dropdown-toggle and a.dropdown-item that auto-closes the menu.
  4. Final Answer:

    Use button with dropdown-toggle and data-bs-toggle="dropdown", and a tags with dropdown-item inside dropdown-menu -> Option A
  5. Quick Check:

    data-bs-toggle + dropdown-item = auto close [OK]
Hint: data-bs-toggle + dropdown-item auto closes menu [OK]
Common Mistakes:
  • Using old data-toggle instead of data-bs-toggle
  • Omitting data-bs-toggle
  • Using a tag for toggle or span for items
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard