Lesson 27 of 60 – Bootstrap Dropdowns
45%

Bootstrap Dropdowns

Bootstrap Dropdowns are interactive menus that display a list of options when the user clicks or activates a button or link. Dropdowns are commonly used in navigation menus, buttons, user profiles, and action menus.

Note: Bootstrap dropdowns use classes such as .dropdown, .dropdown-toggle, .dropdown-menu, and .dropdown-item. Dropdown interactions require Bootstrap JavaScript.

1. What Is a Bootstrap Dropdown?

A Dropdown is a menu that remains hidden until the user activates its toggle button or link.

Dropdowns are useful when several related options need to be grouped under one menu.

<div class="dropdown">

    <button
    class="btn btn-primary dropdown-toggle"
    type="button"
    data-bs-toggle="dropdown">

    Menu

    </button>

    <ul class="dropdown-menu">

        <li>
        <a class="dropdown-item"
        href="#">
        Option 1
        </a>
        </li>

    </ul>

</div>

2. Basic Dropdown

The basic Dropdown consists of a wrapper, a toggle, and a dropdown menu.

<div class="dropdown">

    <button
    class="btn btn-primary dropdown-toggle"
    type="button"
    data-bs-toggle="dropdown">

    Select

    </button>

    <ul class="dropdown-menu">

        <li>
        <a class="dropdown-item"
        href="#">
        HTML
        </a>
        </li>

        <li>
        <a class="dropdown-item"
        href="#">
        CSS
        </a>
        </li>

        <li>
        <a class="dropdown-item"
        href="#">
        JavaScript
        </a>
        </li>

    </ul>

</div>

3. The .dropdown Class

The .dropdown class is normally used as the wrapper around a Dropdown component.

<div class="dropdown">

    ...

</div>

It provides the positioning context for the dropdown menu.

4. The .dropdown-toggle Class

The .dropdown-toggle class styles the element that opens the dropdown menu and adds the visual dropdown indicator.

<button
class="btn btn-primary dropdown-toggle"
type="button"
data-bs-toggle="dropdown">

Options

</button>

5. The .dropdown-menu Class

The .dropdown-menu class creates the container that holds the dropdown options.

<ul class="dropdown-menu">

    <li>
    <a class="dropdown-item"
    href="#">
    Home
    </a>
    </li>

    <li>
    <a class="dropdown-item"
    href="#">
    About
    </a>
    </li>

</ul>

6. The .dropdown-item Class

Each option inside a dropdown menu commonly uses the .dropdown-item class.

<a
class="dropdown-item"
href="#">

HTML Tutorial

</a>

You can use links, buttons, or other suitable elements depending on the action you want to perform.

7. Dropdown with Links

Dropdown items are often links that take the user to another page.

<div class="dropdown">

    <button
    class="btn btn-primary dropdown-toggle"
    data-bs-toggle="dropdown">

    Tutorials

    </button>

    <ul class="dropdown-menu">

        <li>
        <a
        class="dropdown-item"
        href="html.php">

        HTML

        </a>
        </li>

        <li>
        <a
        class="dropdown-item"
        href="css.php">

        CSS

        </a>
        </li>

    </ul>

</div>

8. Dropdown Divider

The .dropdown-divider class creates a horizontal divider between groups of dropdown items.

<ul class="dropdown-menu">

    <li>
    <a class="dropdown-item"
    href="#">
    Profile
    </a>
    </li>

    <li>
    <a class="dropdown-item"
    href="#">
    Settings
    </a>
    </li>

    <li>
    <hr class="dropdown-divider">
    </li>

    <li>
    <a class="dropdown-item"
    href="#">
    Logout
    </a>
    </li>

</ul>

9. Dropdown Heading

The .dropdown-header class adds a non-interactive heading inside a dropdown menu.

<ul class="dropdown-menu">

    <li>
    <h6 class="dropdown-header">
    Tutorials
    </h6>
    </li>

    <li>
    <a class="dropdown-item"
    href="#">
    HTML
    </a>
    </li>

</ul>

10. Active Dropdown Item

The .active class can visually indicate the currently selected dropdown item.

<a
class="dropdown-item active"
href="#"
aria-current="true">

HTML

</a>

11. Disabled Dropdown Item

The .disabled class can be used to indicate that a dropdown item is unavailable.

<a
class="dropdown-item disabled"
aria-disabled="true"
href="#">

Coming Soon

</a>

12. Dropdown Placement

Bootstrap supports different dropdown directions and placements. The dropdown can open downward, upward, toward the start, or toward the end depending on the required layout.

Useful classes include:

  • .dropup
  • .dropend
  • .dropstart
<div class="dropup">

    <button
    class="btn btn-primary dropdown-toggle"
    data-bs-toggle="dropdown">

    Open Up

    </button>

    ...

</div>

13. Dropup Menu

The .dropup class makes the dropdown open upward when there is appropriate space and positioning.

<div class="dropup">

    <button
    class="btn btn-secondary dropdown-toggle"
    data-bs-toggle="dropdown">

    Dropup

    </button>

    <ul class="dropdown-menu">

        <li>
        <a class="dropdown-item"
        href="#">
        Option 1
        </a>
        </li>

    </ul>

</div>

14. Dropend Menu

The .dropend class can be used to make a dropdown open toward the end side of the element.

<div class="dropend">

    <button
    class="btn btn-dark dropdown-toggle"
    data-bs-toggle="dropdown">

    Dropend

    </button>

    <ul class="dropdown-menu">
        ...
    </ul>

</div>

15. Dropstart Menu

The .dropstart class can be used to make the dropdown open toward the start side.

<div class="dropstart">

    <button
    class="btn btn-primary dropdown-toggle"
    data-bs-toggle="dropdown">

    Dropstart

    </button>

    <ul class="dropdown-menu">
        ...
    </ul>

</div>

16. Dropdown with Buttons

A Dropdown can be combined with Bootstrap buttons to create action menus.

<div class="dropdown">

    <button
    class="btn btn-success dropdown-toggle"
    data-bs-toggle="dropdown">

    Actions

    </button>

    <ul class="dropdown-menu">

        <li>
        <a class="dropdown-item"
        href="#">
        Edit
        </a>
        </li>

        <li>
        <a class="dropdown-item"
        href="#">
        Delete
        </a>
        </li>

    </ul>

</div>

17. Dropdown with Outline Button

Dropdowns can also be combined with Bootstrap outline buttons.

<button
class="btn btn-outline-primary dropdown-toggle"
data-bs-toggle="dropdown">

Options

</button>

18. Dropdown in Navbar

Dropdowns are commonly used inside navigation menus.

<li class="nav-item dropdown">

    <a
    class="nav-link dropdown-toggle"
    href="#"
    role="button"
    data-bs-toggle="dropdown">

    Tutorials

    </a>

    <ul class="dropdown-menu">

        <li>
        <a
        class="dropdown-item"
        href="#">

        HTML

        </a>
        </li>

        <li>
        <a
        class="dropdown-item"
        href="#">

        Bootstrap

        </a>
        </li>

    </ul>

</li>

19. Dropdown with Text

The .dropdown-menu-text utility can be used for text content that should appear inside a dropdown menu without behaving as a clickable item.

<div class="dropdown-menu">

    <span class="dropdown-item-text">

    Welcome to our website.

    </span>

</div>

20. Dropdown Menu Alignment

Bootstrap provides utilities for aligning a dropdown menu. For example, .dropdown-menu-end aligns the menu toward the end of its parent.

<ul
class="dropdown-menu dropdown-menu-end">

    <li>
    <a
    class="dropdown-item"
    href="#">

    Profile

    </a>
    </li>

</ul>

21. Dropdown Menu Dark

The .dropdown-menu-dark class creates a dark dropdown menu.

<ul
class="dropdown-menu dropdown-menu-dark">

    <li>
    <a
    class="dropdown-item"
    href="#">

    Home

    </a>
    </li>

    <li>
    <a
    class="dropdown-item"
    href="#">

    About

    </a>
    </li>

</ul>

22. Dropdown Menu with Form

A form can be placed inside a dropdown menu when a small interactive form is required.

<div class="dropdown-menu p-3">

    <form>

        <label
        class="form-label">

        Email

        </label>

        <input
        type="email"
        class="form-control"
        placeholder="Enter email">

        <button
        class="btn btn-primary mt-2"
        type="submit">

        Submit

        </button>

    </form>

</div>

This pattern can be useful for compact search, login, or filter forms.

23. Split Dropdown Button

A split button separates the main action from the dropdown toggle. Bootstrap provides the .btn-group pattern for this.

<div class="btn-group">

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

    Save

    </button>

    <button
    type="button"
    class="btn btn-primary dropdown-toggle dropdown-toggle-split"
    data-bs-toggle="dropdown">

    <span class="visually-hidden">
    Toggle Dropdown
    </span>

    </button>

    <ul class="dropdown-menu">

        <li>
        <a class="dropdown-item"
        href="#">
        Save As
        </a>
        </li>

        <li>
        <a class="dropdown-item"
        href="#">
        Save Copy
        </a>
        </li>

    </ul>

</div>

24. Dropdown with Icons or Text

Dropdown items can contain text and other inline content. This makes them useful for user account menus and action lists.

<ul class="dropdown-menu">

    <li>
    <a class="dropdown-item"
    href="#">

    👤 Profile

    </a>
    </li>

    <li>
    <a class="dropdown-item"
    href="#">

    ⚙ Settings

    </a>
    </li>

    <li>
    <a class="dropdown-item"
    href="#">

    🚪 Logout

    </a>
    </li>

</ul>

25. Dropdown in a Button Group

Dropdowns can be combined with multiple buttons using .btn-group.

<div class="btn-group">

    <button
    class="btn btn-primary">

    Action

    </button>

    <button
    class="btn btn-primary dropdown-toggle"
    data-bs-toggle="dropdown">

    More

    </button>

    <ul class="dropdown-menu">

        <li>
        <a
        class="dropdown-item"
        href="#">
        Edit
        </a>
        </li>

        <li>
        <a
        class="dropdown-item"
        href="#">
        Delete
        </a>
        </li>

    </ul>

</div>

26. Dropdown with Navbar Example

The following example shows a simple Navbar containing a dropdown menu.

<nav
class="navbar navbar-expand-lg bg-dark"
data-bs-theme="dark">

    <div class="container-fluid">

        <a
        class="navbar-brand"
        href="#">

        My Website

        </a>

        <div class="navbar-nav">

            <a
            class="nav-link"
            href="#">

            Home

            </a>

            <div class="nav-item dropdown">

                <a
                class="nav-link dropdown-toggle"
                href="#"
                role="button"
                data-bs-toggle="dropdown">

                Courses

                </a>

                <ul class="dropdown-menu">

                    <li>
                    <a
                    class="dropdown-item"
                    href="#">

                    HTML

                    </a>
                    </li>

                    <li>
                    <a
                    class="dropdown-item"
                    href="#">

                    Python

                    </a>
                    </li>

                </ul>

            </div>

        </div>

    </div>

</nav>

27. Dropdown Accessibility

Dropdowns should be designed so that users can understand and operate them using keyboards and assistive technologies.

  • Use meaningful names for dropdown toggles.
  • Use aria-expanded when appropriate.
  • Use aria-current for the current page when needed.
  • Ensure dropdown controls are keyboard accessible.
  • Do not depend only on color to communicate selection.
  • Use readable and descriptive dropdown item names.
<button
class="btn btn-primary dropdown-toggle"
type="button"
data-bs-toggle="dropdown"
aria-expanded="false">

Courses

</button>

28. Common Dropdown Mistakes

Avoid these common mistakes when creating Bootstrap Dropdowns:

  • Forgetting data-bs-toggle="dropdown".
  • Forgetting to load Bootstrap JavaScript.
  • Using incorrect HTML structure.
  • Using duplicate IDs elsewhere in the page when IDs are needed.
  • Adding too many items to one dropdown.
  • Using unclear dropdown labels.
  • Ignoring keyboard accessibility.
  • Using poor contrast.
  • Making dropdown menus unnecessarily complicated.
Tip: Keep dropdown menus short and group related options logically.

29. Practical Tutorial Dropdown

A tutorial website can use dropdowns to organize different subjects under one Tutorials menu.

<div class="dropdown">

    <button
    class="btn btn-primary dropdown-toggle"
    type="button"
    data-bs-toggle="dropdown">

    Tutorials

    </button>


    <ul class="dropdown-menu">

        <li>
        <h6 class="dropdown-header">
        Web Development
        </h6>
        </li>

        <li>
        <a
        class="dropdown-item"
        href="#">

        HTML

        </a>
        </li>

        <li>
        <a
        class="dropdown-item"
        href="#">

        CSS

        </a>
        </li>

        <li>
        <a
        class="dropdown-item"
        href="#">

        Bootstrap

        </a>
        </li>

        <li>
        <hr class="dropdown-divider">
        </li>

        <li>
        <h6 class="dropdown-header">
        Programming
        </h6>
        </li>

        <li>
        <a
        class="dropdown-item"
        href="#">

        Python

        </a>
        </li>

        <li>
        <a
        class="dropdown-item"
        href="#">

        JavaScript

        </a>
        </li>

    </ul>

</div>

30. Complete Bootstrap Dropdown Example

This complete example combines a Bootstrap button, dropdown heading, dropdown links, divider, active item, and disabled item.

<div class="dropdown">

    <button
    class="btn btn-success dropdown-toggle"
    type="button"
    data-bs-toggle="dropdown"
    aria-expanded="false">

    Courses

    </button>


    <ul class="dropdown-menu">


        <li>

            <h6 class="dropdown-header">

            Web Development

            </h6>

        </li>


        <li>

            <a
            class="dropdown-item active"
            href="#"
            aria-current="true">

            HTML

            </a>

        </li>


        <li>

            <a
            class="dropdown-item"
            href="#">

            CSS

            </a>

        </li>


        <li>

            <a
            class="dropdown-item"
            href="#">

            Bootstrap

            </a>

        </li>


        <li>

            <hr class="dropdown-divider">

        </li>


        <li>

            <h6 class="dropdown-header">

            Programming

            </h6>

        </li>


        <li>

            <a
            class="dropdown-item"
            href="#">

            Python

            </a>

        </li>


        <li>

            <a
            class="dropdown-item disabled"
            aria-disabled="true"
            href="#">

            Java

            </a>

        </li>


    </ul>

</div>

📌 Key Points

  • .dropdown is the main Dropdown wrapper.
  • .dropdown-toggle creates the Dropdown toggle style.
  • .dropdown-menu contains the Dropdown options.
  • .dropdown-item styles individual menu items.
  • .dropdown-divider creates a separator.
  • .dropdown-header creates a heading inside a Dropdown.
  • .dropdown-item-text can display non-clickable text.
  • .active can indicate an active Dropdown item.
  • .disabled can indicate an unavailable item.
  • .dropdown-menu-end aligns the menu toward the end.
  • .dropdown-menu-dark creates a dark Dropdown menu.
  • .dropup, .dropend, and .dropstart control Dropdown direction.
  • Dropdowns can be combined with Navbars and Button Groups.
  • Dropdown interactions require Bootstrap JavaScript.
  • Accessible labels and keyboard support are important for Dropdowns.

🧠 Quick Quiz

Question: Which Bootstrap class is used for the container that holds dropdown items?