Master Bootstrap From Scratch

Clear, interactive, and structured Bootstrap lessons designed for beginners and developers.

Bootstrap Dropdowns

Learn how to create interactive dropdown menus using Bootstrap 5.

What is a Dropdown?

A dropdown is an interactive menu that displays a list of options when a user clicks a button or another control. It helps organize multiple actions inside a small space.

Examples: Profile menus, Settings, Course categories, Download options, and Admin dashboards.

Learning Objectives

  • Understand the structure of a dropdown.
  • Create dropdown buttons and menus.
  • Use dropdown items, headers, and dividers.
  • Change dropdown direction and alignment.
  • Create split-button dropdowns.
  • Build a practical course-selection dropdown.

1. How Does a Dropdown Work?

When the user clicks the dropdown button, Bootstrap JavaScript opens the associated menu. The user can then select an option.

Dropdown Working Diagram
1
User Clicks

The user clicks the dropdown button.

→ ↓
2
Bootstrap JS

Bootstrap handles the click and toggles the menu.

→ ↓
3
Menu Opens

The user sees the available menu items.

Select an option
↓
4. Perform an Action

Navigate to a page, open a feature, or execute the action connected to the selected item.

Remember: Bootstrap opens and closes the menu. You must connect the menu items to actual application actions if you want them to navigate or change data.

2. Dropdown Structure

A Bootstrap dropdown generally contains three important parts:

  • dropdown — the parent container.
  • dropdown-toggle — the button that opens or closes the menu.
  • dropdown-menu and dropdown-item — the menu and its individual options.
Program Code
<div class="dropdown">

    <button class="btn btn-primary dropdown-toggle"
            type="button"
            data-bs-toggle="dropdown"
            aria-expanded="false">
        Select Course
    </button>

    <ul class="dropdown-menu">
        <li>
            <a class="dropdown-item" href="#">
                Java Full Stack
            </a>
        </li>
        <li>
            <a class="dropdown-item" href="#">
                Python Full Stack
            </a>
        </li>
        <li>
            <a class="dropdown-item" href="#">
                .NET Full Stack
            </a>
        </li>
    </ul>

</div>
Program Output

Click Select Course to open the dropdown menu.

3. Dropdown Items, Headers and Dividers

Dropdown menus can contain normal items, section headers, separators, and disabled options.

Program Code
<div class="dropdown">
    <button class="btn btn-primary dropdown-toggle"
            data-bs-toggle="dropdown">
        Account Options
    </button>

    <ul class="dropdown-menu">
        <li>
            <h6 class="dropdown-header">My Account</h6>
        </li>
        <li>
            <a class="dropdown-item" href="#">My Profile</a>
        </li>
        <li>
            <a class="dropdown-item" href="#">Settings</a>
        </li>
        <li><hr class="dropdown-divider"></li>
        <li>
            <button class="dropdown-item" disabled>
                Admin Panel
            </button>
        </li>
    </ul>
</div>
Program Output
Class Purpose
dropdown-item Defines an individual menu option.
dropdown-header Displays a heading inside the menu.
dropdown-divider Separates groups of menu items.
disabled Disables the menu option in this example.

4. Dropdown Menu Alignment

Use dropdown-menu-end to align the menu to the right side of its dropdown container. This is useful when menus appear near the right edge of a page.

Program Code
<div class="dropdown">
    <button class="btn btn-success dropdown-toggle"
            data-bs-toggle="dropdown">
        Right Aligned Menu
    </button>

    <ul class="dropdown-menu dropdown-menu-end">
        <li>
            <a class="dropdown-item" href="#">Profile</a>
        </li>
        <li>
            <a class="dropdown-item" href="#">Settings</a>
        </li>
    </ul>
</div>
Program Output

5. Dropdown Directions

Bootstrap supports different dropdown directions depending on where the menu should appear.

  • dropup — opens the menu upward.
  • dropend — opens the menu toward the right.
  • dropstart — opens the menu toward the left.
Program Code
<!-- Opens upward -->
<div class="dropup">
    <button class="btn btn-primary dropdown-toggle"
            data-bs-toggle="dropdown">
        Dropup
    </button>
    <ul class="dropdown-menu">
        <li>
            <a class="dropdown-item" href="#">Option 1</a>
        </li>
    </ul>
</div>

<!-- Opens toward the right -->
<div class="dropend">
    <button class="btn btn-success dropdown-toggle"
            data-bs-toggle="dropdown">
        Dropend
    </button>
    <ul class="dropdown-menu">
        <li>
            <a class="dropdown-item" href="#">Option 1</a>
        </li>
    </ul>
</div>

<!-- Opens toward the left -->
<div class="dropstart">
    <button class="btn btn-warning dropdown-toggle"
            data-bs-toggle="dropdown">
        Dropstart
    </button>
    <ul class="dropdown-menu">
        <li>
            <a class="dropdown-item" href="#">Option 1</a>
        </li>
    </ul>
</div>
Program Output

Note: A menu may change its placement automatically if there is not enough space near the edge of the viewport.

6. Split Button Dropdown

A split button contains two separate controls: a main action button and a dropdown toggle. The main button can perform an action while the arrow opens additional options.

Program Code
<div class="btn-group">

    <button type="button" class="btn btn-primary">
        Download
    </button>

    <button type="button"
            class="btn btn-primary dropdown-toggle dropdown-toggle-split"
            data-bs-toggle="dropdown"
            aria-expanded="false">
        <span class="visually-hidden">
            Toggle Dropdown
        </span>
    </button>

    <ul class="dropdown-menu">
        <li>
            <a class="dropdown-item" href="#pdf">Download PDF</a>
        </li>
        <li>
            <a class="dropdown-item" href="#excel">Download Excel</a>
        </li>
    </ul>

</div>
Program Output
Download action selected (demo).

This is a UI demonstration. It does not generate or download a file.

7. Practical Example: Course Selection

Imagine a training institute website. Students can use a dropdown to select a course. In this example, selecting an item displays the selected course below the menu.

Program Code
<div class="dropdown">
    <button class="btn btn-primary dropdown-toggle"
            data-bs-toggle="dropdown">
        Choose a Course
    </button>

    <ul class="dropdown-menu">
        <li>
            <button class="dropdown-item" type="button"
                    onclick="selectCourse('Java Full Stack')">
                Java Full Stack
            </button>
        </li>
        <li>
            <button class="dropdown-item" type="button"
                    onclick="selectCourse('Python Full Stack')">
                Python Full Stack
            </button>
        </li>
        <li>
            <button class="dropdown-item" type="button"
                    onclick="selectCourse('.NET Full Stack')">
                .NET Full Stack
            </button>
        </li>
    </ul>
</div>

<p id="selectedCourse">No course selected.</p>

<script>
    function selectCourse(course) {
        document.getElementById('selectedCourse').textContent =
            'Selected Course: ' + course;
    }
</script>
Program Output

No course selected.

Output example: When you select Python Full Stack, the output becomes “Selected Course: Python Full Stack”.

8. Important Classes and Attributes

Class / Attribute Purpose
dropdown Creates the dropdown container.
dropdown-toggle Styles the toggle button.
data-bs-toggle="dropdown" Enables Bootstrap dropdown behavior.
dropdown-menu Defines the dropdown menu.
dropdown-item Styles individual options.
dropdown-menu-end Aligns the menu to the end of its container.
dropup Positions the menu upward.
btn-group Groups buttons together.

9. Common Mistakes

  • Forgetting to include the Bootstrap JavaScript bundle.
  • Using Bootstrap 4's data-toggle instead of Bootstrap 5's data-bs-toggle.
  • Placing the dropdown menu outside the intended dropdown container without considering its positioning.
  • Reusing IDs for unrelated interactive components.
  • Assuming that selecting a menu item automatically saves data or performs a backend operation.

Summary

  • A dropdown displays a menu of related options.
  • Bootstrap uses data-bs-toggle="dropdown" to toggle the menu.
  • dropdown-menu contains the available options.
  • Use headers and dividers to organize menu items.
  • Use dropup, dropend, and dropstart to change direction.
  • Use JavaScript or appropriate links to connect items to real actions.