View on GitHub

Dropdowns

Toggle contextual overlays for displaying lists of links and more with the Bootstrap dropdown plugin.

Overview

Dropdowns are toggleable, contextual overlays for displaying lists of links and more. They’re made interactive with the included Bootstrap dropdown JavaScript plugin. They’re toggled by clicking, not by hovering; this is an intentional design decision.

Dropdowns are built on a third party library, Popper, which provides dynamic positioning and viewport detection. Be sure to include popper.min.js before Bootstrap’s JavaScript or use bootstrap.bundle.min.js / bootstrap.bundle.js which contains Popper. Popper isn’t used to position dropdowns in navbars though as dynamic positioning isn’t required.

Accessibility

The WAI ARIA standard defines an actual role=”menu” widget, but this is specific to application-like menus which trigger actions or functions. ARIA menus can only contain menu items, checkbox menu items, radio button menu items, radio button groups, and sub-menus.

Bootstrap’s dropdowns, on the other hand, are designed to be generic and applicable to a variety of situations and markup structures. For instance, it is possible to create dropdowns that contain additional inputs and form controls, such as search fields or login forms. For this reason, Bootstrap does not expect (nor automatically add) any of the role and aria- attributes required for true ARIA menus. Authors will have to include these more specific attributes themselves.

However, Bootstrap does add built-in support for most standard keyboard menu interactions, such as the ability to move through individual .dropdown-item elements using the cursor keys and close the menu with the ESC key.

Examples

Wrap the dropdown’s toggle (your button or link) and the dropdown menu within .dropdown, or another element that declares position: relative;. Dropdowns can be triggered from <a> or <button> elements to better fit your potential needs. The examples shown here use semantic <ul> elements where appropriate, but custom markup is supported.

Single button

Any single .btn can be turned into a dropdown toggle with some markup changes. Here’s how you can put them to work with either <button> elements:

Dropdowns - 图1

html

  1. <div class="dropdown">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown button
  4. </button>
  5. <ul class="dropdown-menu">
  6. <li><a class="dropdown-item" href="#">Action</a></li>
  7. <li><a class="dropdown-item" href="#">Another action</a></li>
  8. <li><a class="dropdown-item" href="#">Something else here</a></li>
  9. </ul>
  10. </div>

And with <a> elements:

Dropdowns - 图2

html

  1. <div class="dropdown">
  2. <a class="btn btn-secondary dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown link
  4. </a>
  5. <ul class="dropdown-menu">
  6. <li><a class="dropdown-item" href="#">Action</a></li>
  7. <li><a class="dropdown-item" href="#">Another action</a></li>
  8. <li><a class="dropdown-item" href="#">Something else here</a></li>
  9. </ul>
  10. </div>

The best part is you can do this with any button variant, too:

Dropdowns - 图3

  1. <!-- Example single danger button -->
  2. <div class="btn-group">
  3. <button type="button" class="btn btn-danger dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
  4. Action
  5. </button>
  6. <ul class="dropdown-menu">
  7. <li><a class="dropdown-item" href="#">Action</a></li>
  8. <li><a class="dropdown-item" href="#">Another action</a></li>
  9. <li><a class="dropdown-item" href="#">Something else here</a></li>
  10. <li><hr class="dropdown-divider"></li>
  11. <li><a class="dropdown-item" href="#">Separated link</a></li>
  12. </ul>
  13. </div>

Split button

Similarly, create split button dropdowns with virtually the same markup as single button dropdowns, but with the addition of .dropdown-toggle-split for proper spacing around the dropdown caret.

We use this extra class to reduce the horizontal padding on either side of the caret by 25% and remove the margin-left that’s added for regular button dropdowns. Those extra changes keep the caret centered in the split button and provide a more appropriately sized hit area next to the main button.

Dropdowns - 图4

  1. <!-- Example split danger button -->
  2. <div class="btn-group">
  3. <button type="button" class="btn btn-danger">Action</button>
  4. <button type="button" class="btn btn-danger dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
  5. <span class="visually-hidden">Toggle Dropdown</span>
  6. </button>
  7. <ul class="dropdown-menu">
  8. <li><a class="dropdown-item" href="#">Action</a></li>
  9. <li><a class="dropdown-item" href="#">Another action</a></li>
  10. <li><a class="dropdown-item" href="#">Something else here</a></li>
  11. <li><hr class="dropdown-divider"></li>
  12. <li><a class="dropdown-item" href="#">Separated link</a></li>
  13. </ul>
  14. </div>

Sizing

Button dropdowns work with buttons of all sizes, including default and split dropdown buttons.

Dropdowns - 图5

  1. <!-- Large button groups (default and split) -->
  2. <div class="btn-group">
  3. <button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
  4. Large button
  5. </button>
  6. <ul class="dropdown-menu">
  7. ...
  8. </ul>
  9. </div>
  10. <div class="btn-group">
  11. <button class="btn btn-secondary btn-lg" type="button">
  12. Large split button
  13. </button>
  14. <button type="button" class="btn btn-lg btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
  15. <span class="visually-hidden">Toggle Dropdown</span>
  16. </button>
  17. <ul class="dropdown-menu">
  18. ...
  19. </ul>
  20. </div>

Dropdowns - 图6

  1. <div class="btn-group">
  2. <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
  3. Small button
  4. </button>
  5. <ul class="dropdown-menu">
  6. ...
  7. </ul>
  8. </div>
  9. <div class="btn-group">
  10. <button class="btn btn-secondary btn-sm" type="button">
  11. Small split button
  12. </button>
  13. <button type="button" class="btn btn-sm btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
  14. <span class="visually-hidden">Toggle Dropdown</span>
  15. </button>
  16. <ul class="dropdown-menu">
  17. ...
  18. </ul>
  19. </div>

Dark dropdowns

Opt into darker dropdowns to match a dark navbar or custom style by adding .dropdown-menu-dark onto an existing .dropdown-menu. No changes are required to the dropdown items.

Dropdowns - 图7

html

  1. <div class="dropdown">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown button
  4. </button>
  5. <ul class="dropdown-menu dropdown-menu-dark">
  6. <li><a class="dropdown-item active" href="#">Action</a></li>
  7. <li><a class="dropdown-item" href="#">Another action</a></li>
  8. <li><a class="dropdown-item" href="#">Something else here</a></li>
  9. <li><hr class="dropdown-divider"></li>
  10. <li><a class="dropdown-item" href="#">Separated link</a></li>
  11. </ul>
  12. </div>

And putting it to use in a navbar:

Dropdowns - 图8

html

  1. <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  2. <div class="container-fluid">
  3. <a class="navbar-brand" href="#">Navbar</a>
  4. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDarkDropdown" aria-controls="navbarNavDarkDropdown" aria-expanded="false" aria-label="Toggle navigation">
  5. <span class="navbar-toggler-icon"></span>
  6. </button>
  7. <div class="collapse navbar-collapse" id="navbarNavDarkDropdown">
  8. <ul class="navbar-nav">
  9. <li class="nav-item dropdown">
  10. <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  11. Dropdown
  12. </a>
  13. <ul class="dropdown-menu dropdown-menu-dark">
  14. <li><a class="dropdown-item" href="#">Action</a></li>
  15. <li><a class="dropdown-item" href="#">Another action</a></li>
  16. <li><a class="dropdown-item" href="#">Something else here</a></li>
  17. </ul>
  18. </li>
  19. </ul>
  20. </div>
  21. </div>
  22. </nav>

Directions

RTL

Directions are mirrored when using Bootstrap in RTL, meaning .dropstart will appear on the right side.

Centered

Make the dropdown menu centered below the toggle with .dropdown-center on the parent element.

Dropdowns - 图9

html

  1. <div class="dropdown-center">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
  3. Centered dropdown
  4. </button>
  5. <ul class="dropdown-menu">
  6. <li><a class="dropdown-item" href="#">Action</a></li>
  7. <li><a class="dropdown-item" href="#">Action two</a></li>
  8. <li><a class="dropdown-item" href="#">Action three</a></li>
  9. </ul>
  10. </div>

Dropup

Trigger dropdown menus above elements by adding .dropup to the parent element.

Dropdowns - 图10

  1. <!-- Default dropup button -->
  2. <div class="btn-group dropup">
  3. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
  4. Dropup
  5. </button>
  6. <ul class="dropdown-menu">
  7. <!-- Dropdown menu links -->
  8. </ul>
  9. </div>
  10. <!-- Split dropup button -->
  11. <div class="btn-group dropup">
  12. <button type="button" class="btn btn-secondary">
  13. Split dropup
  14. </button>
  15. <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
  16. <span class="visually-hidden">Toggle Dropdown</span>
  17. </button>
  18. <ul class="dropdown-menu">
  19. <!-- Dropdown menu links -->
  20. </ul>
  21. </div>

Dropup centered

Make the dropup menu centered above the toggle with .dropup-center on the parent element.

Dropdowns - 图11

html

  1. <div class="dropup-center dropup">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
  3. Centered dropup
  4. </button>
  5. <ul class="dropdown-menu">
  6. <li><a class="dropdown-item" href="#">Action</a></li>
  7. <li><a class="dropdown-item" href="#">Action two</a></li>
  8. <li><a class="dropdown-item" href="#">Action three</a></li>
  9. </ul>
  10. </div>

Dropend

Trigger dropdown menus at the right of the elements by adding .dropend to the parent element.

Dropdowns - 图12

  1. <!-- Default dropend button -->
  2. <div class="btn-group dropend">
  3. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
  4. Dropend
  5. </button>
  6. <ul class="dropdown-menu">
  7. <!-- Dropdown menu links -->
  8. </ul>
  9. </div>
  10. <!-- Split dropend button -->
  11. <div class="btn-group dropend">
  12. <button type="button" class="btn btn-secondary">
  13. Split dropend
  14. </button>
  15. <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
  16. <span class="visually-hidden">Toggle Dropend</span>
  17. </button>
  18. <ul class="dropdown-menu">
  19. <!-- Dropdown menu links -->
  20. </ul>
  21. </div>

Dropstart

Trigger dropdown menus at the left of the elements by adding .dropstart to the parent element.

Dropdowns - 图13

  1. <!-- Default dropstart button -->
  2. <div class="btn-group dropstart">
  3. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
  4. Dropstart
  5. </button>
  6. <ul class="dropdown-menu">
  7. <!-- Dropdown menu links -->
  8. </ul>
  9. </div>
  10. <!-- Split dropstart button -->
  11. <div class="btn-group dropstart">
  12. <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
  13. <span class="visually-hidden">Toggle Dropstart</span>
  14. </button>
  15. <ul class="dropdown-menu">
  16. <!-- Dropdown menu links -->
  17. </ul>
  18. <button type="button" class="btn btn-secondary">
  19. Split dropstart
  20. </button>
  21. </div>

Menu items

You can use <a> or <button> elements as dropdown items.

Dropdowns - 图14

html

  1. <div class="dropdown">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown
  4. </button>
  5. <ul class="dropdown-menu">
  6. <li><button class="dropdown-item" type="button">Action</button></li>
  7. <li><button class="dropdown-item" type="button">Another action</button></li>
  8. <li><button class="dropdown-item" type="button">Something else here</button></li>
  9. </ul>
  10. </div>

You can also create non-interactive dropdown items with .dropdown-item-text. Feel free to style further with custom CSS or text utilities.

Dropdowns - 图15

html

  1. <ul class="dropdown-menu">
  2. <li><span class="dropdown-item-text">Dropdown item text</span></li>
  3. <li><a class="dropdown-item" href="#">Action</a></li>
  4. <li><a class="dropdown-item" href="#">Another action</a></li>
  5. <li><a class="dropdown-item" href="#">Something else here</a></li>
  6. </ul>

Active

Add .active to items in the dropdown to style them as active. To convey the active state to assistive technologies, use the aria-current attribute — using the page value for the current page, or true for the current item in a set.

Dropdowns - 图16

html

  1. <ul class="dropdown-menu">
  2. <li><a class="dropdown-item" href="#">Regular link</a></li>
  3. <li><a class="dropdown-item active" href="#" aria-current="true">Active link</a></li>
  4. <li><a class="dropdown-item" href="#">Another link</a></li>
  5. </ul>

Disabled

Add .disabled to items in the dropdown to style them as disabled.

Dropdowns - 图17

html

  1. <ul class="dropdown-menu">
  2. <li><a class="dropdown-item" href="#">Regular link</a></li>
  3. <li><a class="dropdown-item disabled">Disabled link</a></li>
  4. <li><a class="dropdown-item" href="#">Another link</a></li>
  5. </ul>

Menu alignment

By default, a dropdown menu is automatically positioned 100% from the top and along the left side of its parent. You can change this with the directional .drop* classes, but you can also control them with additional modifier classes.

Add .dropdown-menu-end to a .dropdown-menu to right align the dropdown menu. Directions are mirrored when using Bootstrap in RTL, meaning .dropdown-menu-end will appear on the left side.

Heads up! Dropdowns are positioned thanks to Popper except when they are contained in a navbar.

Dropdowns - 图18

html

  1. <div class="btn-group">
  2. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
  3. Right-aligned menu example
  4. </button>
  5. <ul class="dropdown-menu dropdown-menu-end">
  6. <li><button class="dropdown-item" type="button">Action</button></li>
  7. <li><button class="dropdown-item" type="button">Another action</button></li>
  8. <li><button class="dropdown-item" type="button">Something else here</button></li>
  9. </ul>
  10. </div>

Responsive alignment

If you want to use responsive alignment, disable dynamic positioning by adding the data-bs-display="static" attribute and use the responsive variation classes.

To align right the dropdown menu with the given breakpoint or larger, add .dropdown-menu{-sm|-md|-lg|-xl|-xxl}-end.

Dropdowns - 图19

html

  1. <div class="btn-group">
  2. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
  3. Left-aligned but right aligned when large screen
  4. </button>
  5. <ul class="dropdown-menu dropdown-menu-lg-end">
  6. <li><button class="dropdown-item" type="button">Action</button></li>
  7. <li><button class="dropdown-item" type="button">Another action</button></li>
  8. <li><button class="dropdown-item" type="button">Something else here</button></li>
  9. </ul>
  10. </div>

To align left the dropdown menu with the given breakpoint or larger, add .dropdown-menu-end and .dropdown-menu{-sm|-md|-lg|-xl|-xxl}-start.

Dropdowns - 图20

html

  1. <div class="btn-group">
  2. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
  3. Right-aligned but left aligned when large screen
  4. </button>
  5. <ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-start">
  6. <li><button class="dropdown-item" type="button">Action</button></li>
  7. <li><button class="dropdown-item" type="button">Another action</button></li>
  8. <li><button class="dropdown-item" type="button">Something else here</button></li>
  9. </ul>
  10. </div>

Note that you don’t need to add a data-bs-display="static" attribute to dropdown buttons in navbars, since Popper isn’t used in navbars.

Alignment options

Taking most of the options shown above, here’s a small kitchen sink demo of various dropdown alignment options in one place.

Dropdowns - 图21

html

  1. <div class="btn-group">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown
  4. </button>
  5. <ul class="dropdown-menu">
  6. <li><a class="dropdown-item" href="#">Menu item</a></li>
  7. <li><a class="dropdown-item" href="#">Menu item</a></li>
  8. <li><a class="dropdown-item" href="#">Menu item</a></li>
  9. </ul>
  10. </div>
  11. <div class="btn-group">
  12. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
  13. Right-aligned menu
  14. </button>
  15. <ul class="dropdown-menu dropdown-menu-end">
  16. <li><a class="dropdown-item" href="#">Menu item</a></li>
  17. <li><a class="dropdown-item" href="#">Menu item</a></li>
  18. <li><a class="dropdown-item" href="#">Menu item</a></li>
  19. </ul>
  20. </div>
  21. <div class="btn-group">
  22. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
  23. Left-aligned, right-aligned lg
  24. </button>
  25. <ul class="dropdown-menu dropdown-menu-lg-end">
  26. <li><a class="dropdown-item" href="#">Menu item</a></li>
  27. <li><a class="dropdown-item" href="#">Menu item</a></li>
  28. <li><a class="dropdown-item" href="#">Menu item</a></li>
  29. </ul>
  30. </div>
  31. <div class="btn-group">
  32. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
  33. Right-aligned, left-aligned lg
  34. </button>
  35. <ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-start">
  36. <li><a class="dropdown-item" href="#">Menu item</a></li>
  37. <li><a class="dropdown-item" href="#">Menu item</a></li>
  38. <li><a class="dropdown-item" href="#">Menu item</a></li>
  39. </ul>
  40. </div>
  41. <div class="btn-group dropstart">
  42. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
  43. Dropstart
  44. </button>
  45. <ul class="dropdown-menu">
  46. <li><a class="dropdown-item" href="#">Menu item</a></li>
  47. <li><a class="dropdown-item" href="#">Menu item</a></li>
  48. <li><a class="dropdown-item" href="#">Menu item</a></li>
  49. </ul>
  50. </div>
  51. <div class="btn-group dropend">
  52. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
  53. Dropend
  54. </button>
  55. <ul class="dropdown-menu">
  56. <li><a class="dropdown-item" href="#">Menu item</a></li>
  57. <li><a class="dropdown-item" href="#">Menu item</a></li>
  58. <li><a class="dropdown-item" href="#">Menu item</a></li>
  59. </ul>
  60. </div>
  61. <div class="btn-group dropup">
  62. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
  63. Dropup
  64. </button>
  65. <ul class="dropdown-menu">
  66. <li><a class="dropdown-item" href="#">Menu item</a></li>
  67. <li><a class="dropdown-item" href="#">Menu item</a></li>
  68. <li><a class="dropdown-item" href="#">Menu item</a></li>
  69. </ul>
  70. </div>

Menu content

Headers

Add a header to label sections of actions in any dropdown menu.

Dropdowns - 图22

html

  1. <ul class="dropdown-menu">
  2. <li><h6 class="dropdown-header">Dropdown header</h6></li>
  3. <li><a class="dropdown-item" href="#">Action</a></li>
  4. <li><a class="dropdown-item" href="#">Another action</a></li>
  5. </ul>

Dividers

Separate groups of related menu items with a divider.

Dropdowns - 图23

html

  1. <ul class="dropdown-menu">
  2. <li><a class="dropdown-item" href="#">Action</a></li>
  3. <li><a class="dropdown-item" href="#">Another action</a></li>
  4. <li><a class="dropdown-item" href="#">Something else here</a></li>
  5. <li><hr class="dropdown-divider"></li>
  6. <li><a class="dropdown-item" href="#">Separated link</a></li>
  7. </ul>

Text

Place any freeform text within a dropdown menu with text and use spacing utilities. Note that you’ll likely need additional sizing styles to constrain the menu width.

Dropdowns - 图24

html

  1. <div class="dropdown-menu p-4 text-muted" style="max-width: 200px;">
  2. <p>
  3. Some example text that's free-flowing within the dropdown menu.
  4. </p>
  5. <p class="mb-0">
  6. And this is more example text.
  7. </p>
  8. </div>

Forms

Put a form within a dropdown menu, or make it into a dropdown menu, and use margin or padding utilities to give it the negative space you require.

Dropdowns - 图25

html

  1. <div class="dropdown-menu">
  2. <form class="px-4 py-3">
  3. <div class="mb-3">
  4. <label for="exampleDropdownFormEmail1" class="form-label">Email address</label>
  5. <input type="email" class="form-control" id="exampleDropdownFormEmail1" placeholder="email@example.com">
  6. </div>
  7. <div class="mb-3">
  8. <label for="exampleDropdownFormPassword1" class="form-label">Password</label>
  9. <input type="password" class="form-control" id="exampleDropdownFormPassword1" placeholder="Password">
  10. </div>
  11. <div class="mb-3">
  12. <div class="form-check">
  13. <input type="checkbox" class="form-check-input" id="dropdownCheck">
  14. <label class="form-check-label" for="dropdownCheck">
  15. Remember me
  16. </label>
  17. </div>
  18. </div>
  19. <button type="submit" class="btn btn-primary">Sign in</button>
  20. </form>
  21. <div class="dropdown-divider"></div>
  22. <a class="dropdown-item" href="#">New around here? Sign up</a>
  23. <a class="dropdown-item" href="#">Forgot password?</a>
  24. </div>

Dropdowns - 图26

html

  1. <div class="dropdown">
  2. <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false" data-bs-auto-close="outside">
  3. Dropdown form
  4. </button>
  5. <form class="dropdown-menu p-4">
  6. <div class="mb-3">
  7. <label for="exampleDropdownFormEmail2" class="form-label">Email address</label>
  8. <input type="email" class="form-control" id="exampleDropdownFormEmail2" placeholder="email@example.com">
  9. </div>
  10. <div class="mb-3">
  11. <label for="exampleDropdownFormPassword2" class="form-label">Password</label>
  12. <input type="password" class="form-control" id="exampleDropdownFormPassword2" placeholder="Password">
  13. </div>
  14. <div class="mb-3">
  15. <div class="form-check">
  16. <input type="checkbox" class="form-check-input" id="dropdownCheck2">
  17. <label class="form-check-label" for="dropdownCheck2">
  18. Remember me
  19. </label>
  20. </div>
  21. </div>
  22. <button type="submit" class="btn btn-primary">Sign in</button>
  23. </form>
  24. </div>

Dropdown options

Use data-bs-offset or data-bs-reference to change the location of the dropdown.

Dropdowns - 图27

html

  1. <div class="d-flex">
  2. <div class="dropdown me-1">
  3. <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false" data-bs-offset="10,20">
  4. Offset
  5. </button>
  6. <ul class="dropdown-menu">
  7. <li><a class="dropdown-item" href="#">Action</a></li>
  8. <li><a class="dropdown-item" href="#">Another action</a></li>
  9. <li><a class="dropdown-item" href="#">Something else here</a></li>
  10. </ul>
  11. </div>
  12. <div class="btn-group">
  13. <button type="button" class="btn btn-secondary">Reference</button>
  14. <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false" data-bs-reference="parent">
  15. <span class="visually-hidden">Toggle Dropdown</span>
  16. </button>
  17. <ul class="dropdown-menu">
  18. <li><a class="dropdown-item" href="#">Action</a></li>
  19. <li><a class="dropdown-item" href="#">Another action</a></li>
  20. <li><a class="dropdown-item" href="#">Something else here</a></li>
  21. <li><hr class="dropdown-divider"></li>
  22. <li><a class="dropdown-item" href="#">Separated link</a></li>
  23. </ul>
  24. </div>
  25. </div>

Auto close behavior

By default, the dropdown menu is closed when clicking inside or outside the dropdown menu. You can use the autoClose option to change this behavior of the dropdown.

Dropdowns - 图28

html

  1. <div class="btn-group">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="true" aria-expanded="false">
  3. Default dropdown
  4. </button>
  5. <ul class="dropdown-menu">
  6. <li><a class="dropdown-item" href="#">Menu item</a></li>
  7. <li><a class="dropdown-item" href="#">Menu item</a></li>
  8. <li><a class="dropdown-item" href="#">Menu item</a></li>
  9. </ul>
  10. </div>
  11. <div class="btn-group">
  12. <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="inside" aria-expanded="false">
  13. Clickable outside
  14. </button>
  15. <ul class="dropdown-menu">
  16. <li><a class="dropdown-item" href="#">Menu item</a></li>
  17. <li><a class="dropdown-item" href="#">Menu item</a></li>
  18. <li><a class="dropdown-item" href="#">Menu item</a></li>
  19. </ul>
  20. </div>
  21. <div class="btn-group">
  22. <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">
  23. Clickable inside
  24. </button>
  25. <ul class="dropdown-menu">
  26. <li><a class="dropdown-item" href="#">Menu item</a></li>
  27. <li><a class="dropdown-item" href="#">Menu item</a></li>
  28. <li><a class="dropdown-item" href="#">Menu item</a></li>
  29. </ul>
  30. </div>
  31. <div class="btn-group">
  32. <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="false" aria-expanded="false">
  33. Manual close
  34. </button>
  35. <ul class="dropdown-menu">
  36. <li><a class="dropdown-item" href="#">Menu item</a></li>
  37. <li><a class="dropdown-item" href="#">Menu item</a></li>
  38. <li><a class="dropdown-item" href="#">Menu item</a></li>
  39. </ul>
  40. </div>

CSS

Variables

Added in v5.2.0

As part of Bootstrap’s evolving CSS variables approach, dropdowns now use local CSS variables on .dropdown-menu for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

  1. --#{$prefix}dropdown-zindex: #{$zindex-dropdown};
  2. --#{$prefix}dropdown-min-width: #{$dropdown-min-width};
  3. --#{$prefix}dropdown-padding-x: #{$dropdown-padding-x};
  4. --#{$prefix}dropdown-padding-y: #{$dropdown-padding-y};
  5. --#{$prefix}dropdown-spacer: #{$dropdown-spacer};
  6. @include rfs($dropdown-font-size, --#{$prefix}dropdown-font-size);
  7. --#{$prefix}dropdown-color: #{$dropdown-color};
  8. --#{$prefix}dropdown-bg: #{$dropdown-bg};
  9. --#{$prefix}dropdown-border-color: #{$dropdown-border-color};
  10. --#{$prefix}dropdown-border-radius: #{$dropdown-border-radius};
  11. --#{$prefix}dropdown-border-width: #{$dropdown-border-width};
  12. --#{$prefix}dropdown-inner-border-radius: #{$dropdown-inner-border-radius};
  13. --#{$prefix}dropdown-divider-bg: #{$dropdown-divider-bg};
  14. --#{$prefix}dropdown-divider-margin-y: #{$dropdown-divider-margin-y};
  15. --#{$prefix}dropdown-box-shadow: #{$dropdown-box-shadow};
  16. --#{$prefix}dropdown-link-color: #{$dropdown-link-color};
  17. --#{$prefix}dropdown-link-hover-color: #{$dropdown-link-hover-color};
  18. --#{$prefix}dropdown-link-hover-bg: #{$dropdown-link-hover-bg};
  19. --#{$prefix}dropdown-link-active-color: #{$dropdown-link-active-color};
  20. --#{$prefix}dropdown-link-active-bg: #{$dropdown-link-active-bg};
  21. --#{$prefix}dropdown-link-disabled-color: #{$dropdown-link-disabled-color};
  22. --#{$prefix}dropdown-item-padding-x: #{$dropdown-item-padding-x};
  23. --#{$prefix}dropdown-item-padding-y: #{$dropdown-item-padding-y};
  24. --#{$prefix}dropdown-header-color: #{$dropdown-header-color};
  25. --#{$prefix}dropdown-header-padding-x: #{$dropdown-header-padding-x};
  26. --#{$prefix}dropdown-header-padding-y: #{$dropdown-header-padding-y};

Customization through CSS variables can be seen on the .dropdown-menu-dark class where we override specific values without adding duplicate CSS selectors.

  1. --#{$prefix}dropdown-color: #{$dropdown-dark-color};
  2. --#{$prefix}dropdown-bg: #{$dropdown-dark-bg};
  3. --#{$prefix}dropdown-border-color: #{$dropdown-dark-border-color};
  4. --#{$prefix}dropdown-box-shadow: #{$dropdown-dark-box-shadow};
  5. --#{$prefix}dropdown-link-color: #{$dropdown-dark-link-color};
  6. --#{$prefix}dropdown-link-hover-color: #{$dropdown-dark-link-hover-color};
  7. --#{$prefix}dropdown-divider-bg: #{$dropdown-dark-divider-bg};
  8. --#{$prefix}dropdown-link-hover-bg: #{$dropdown-dark-link-hover-bg};
  9. --#{$prefix}dropdown-link-active-color: #{$dropdown-dark-link-active-color};
  10. --#{$prefix}dropdown-link-active-bg: #{$dropdown-dark-link-active-bg};
  11. --#{$prefix}dropdown-link-disabled-color: #{$dropdown-dark-link-disabled-color};
  12. --#{$prefix}dropdown-header-color: #{$dropdown-dark-header-color};

Sass variables

Variables for all dropdowns:

  1. $dropdown-min-width: 10rem;
  2. $dropdown-padding-x: 0;
  3. $dropdown-padding-y: .5rem;
  4. $dropdown-spacer: .125rem;
  5. $dropdown-font-size: $font-size-base;
  6. $dropdown-color: $body-color;
  7. $dropdown-bg: $white;
  8. $dropdown-border-color: var(--#{$prefix}border-color-translucent);
  9. $dropdown-border-radius: $border-radius;
  10. $dropdown-border-width: $border-width;
  11. $dropdown-inner-border-radius: subtract($dropdown-border-radius, $dropdown-border-width);
  12. $dropdown-divider-bg: $dropdown-border-color;
  13. $dropdown-divider-margin-y: $spacer * .5;
  14. $dropdown-box-shadow: $box-shadow;
  15. $dropdown-link-color: $gray-900;
  16. $dropdown-link-hover-color: shade-color($dropdown-link-color, 10%);
  17. $dropdown-link-hover-bg: $gray-200;
  18. $dropdown-link-active-color: $component-active-color;
  19. $dropdown-link-active-bg: $component-active-bg;
  20. $dropdown-link-disabled-color: $gray-500;
  21. $dropdown-item-padding-y: $spacer * .25;
  22. $dropdown-item-padding-x: $spacer;
  23. $dropdown-header-color: $gray-600;
  24. $dropdown-header-padding-x: $dropdown-item-padding-x;
  25. $dropdown-header-padding-y: $dropdown-padding-y;
  26. // fusv-disable
  27. $dropdown-header-padding: $dropdown-header-padding-y $dropdown-header-padding-x; // Deprecated in v5.2.0
  28. // fusv-enable

Variables for the dark dropdown:

  1. $dropdown-dark-color: $gray-300;
  2. $dropdown-dark-bg: $gray-800;
  3. $dropdown-dark-border-color: $dropdown-border-color;
  4. $dropdown-dark-divider-bg: $dropdown-divider-bg;
  5. $dropdown-dark-box-shadow: null;
  6. $dropdown-dark-link-color: $dropdown-dark-color;
  7. $dropdown-dark-link-hover-color: $white;
  8. $dropdown-dark-link-hover-bg: rgba($white, .15);
  9. $dropdown-dark-link-active-color: $dropdown-link-active-color;
  10. $dropdown-dark-link-active-bg: $dropdown-link-active-bg;
  11. $dropdown-dark-link-disabled-color: $gray-500;
  12. $dropdown-dark-header-color: $gray-500;

Variables for the CSS-based carets that indicate a dropdown’s interactivity:

  1. $caret-width: .3em;
  2. $caret-vertical-align: $caret-width * .85;
  3. $caret-spacing: $caret-width * .85;

Mixins

Mixins are used to generate the CSS-based carets and can be found in scss/mixins/_caret.scss.

  1. @mixin caret-down {
  2. border-top: $caret-width solid;
  3. border-right: $caret-width solid transparent;
  4. border-bottom: 0;
  5. border-left: $caret-width solid transparent;
  6. }
  7. @mixin caret-up {
  8. border-top: 0;
  9. border-right: $caret-width solid transparent;
  10. border-bottom: $caret-width solid;
  11. border-left: $caret-width solid transparent;
  12. }
  13. @mixin caret-end {
  14. border-top: $caret-width solid transparent;
  15. border-right: 0;
  16. border-bottom: $caret-width solid transparent;
  17. border-left: $caret-width solid;
  18. }
  19. @mixin caret-start {
  20. border-top: $caret-width solid transparent;
  21. border-right: $caret-width solid;
  22. border-bottom: $caret-width solid transparent;
  23. }
  24. @mixin caret($direction: down) {
  25. @if $enable-caret {
  26. &::after {
  27. display: inline-block;
  28. margin-left: $caret-spacing;
  29. vertical-align: $caret-vertical-align;
  30. content: "";
  31. @if $direction == down {
  32. @include caret-down();
  33. } @else if $direction == up {
  34. @include caret-up();
  35. } @else if $direction == end {
  36. @include caret-end();
  37. }
  38. }
  39. @if $direction == start {
  40. &::after {
  41. display: none;
  42. }
  43. &::before {
  44. display: inline-block;
  45. margin-right: $caret-spacing;
  46. vertical-align: $caret-vertical-align;
  47. content: "";
  48. @include caret-start();
  49. }
  50. }
  51. &:empty::after {
  52. margin-left: 0;
  53. }
  54. }
  55. }

Usage

Via data attributes or JavaScript, the dropdown plugin toggles hidden content (dropdown menus) by toggling the .show class on the parent .dropdown-menu. The data-bs-toggle="dropdown" attribute is relied on for closing dropdown menus at an application level, so it’s a good idea to always use it.

On touch-enabled devices, opening a dropdown adds empty mouseover handlers to the immediate children of the <body> element. This admittedly ugly hack is necessary to work around a quirk in iOS’ event delegation, which would otherwise prevent a tap anywhere outside of the dropdown from triggering the code that closes the dropdown. Once the dropdown is closed, these additional empty mouseover handlers are removed.

Via data attributes

Add data-bs-toggle="dropdown" to a link or button to toggle a dropdown.

  1. <div class="dropdown">
  2. <button type="button" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown trigger
  4. </button>
  5. <ul class="dropdown-menu">
  6. ...
  7. </ul>
  8. </div>

Via JavaScript

Call the dropdowns via JavaScript:

  1. const dropdownElementList = document.querySelectorAll('.dropdown-toggle')
  2. const dropdownList = [...dropdownElementList].map(dropdownToggleEl => new bootstrap.Dropdown(dropdownToggleEl))
data-bs-toggle="dropdown" still required

Regardless of whether you call your dropdown via JavaScript or instead use the data-api, data-bs-toggle="dropdown" is always required to be present on the dropdown’s trigger element.

Options

As options can be passed via data attributes or JavaScript, you can append an option name to data-bs-, as in data-bs-animation="{value}". Make sure to change the case type of the option name from “camelCase” to “kebab-case” when passing the options via data attributes. For example, use data-bs-custom-class="beautifier" instead of data-bs-customClass="beautifier".

As of Bootstrap 5.2.0, all components support an experimental reserved data attribute data-bs-config that can house simple component configuration as a JSON string. When an element has data-bs-config='{"delay":0, "title":123}' and data-bs-title="456" attributes, the final title value will be 456 and the separate data attributes will override values given on data-bs-config. In addition, existing data attributes are able to house JSON values like data-bs-delay='{"show":0,"hide":150}'.

NameTypeDefaultDescription
autoCloseboolean, stringtrueConfigure the auto close behavior of the dropdown:
  • true - the dropdown will be closed by clicking outside or inside the dropdown menu.
  • false - the dropdown will be closed by clicking the toggle button and manually calling hide or toggle method. (Also will not be closed by pressing esc key)
  • ‘inside’ - the dropdown will be closed (only) by clicking inside the dropdown menu.
  • ‘outside’ - the dropdown will be closed (only) by clicking outside the dropdown menu.
Note: the dropdown can always be closed with the ESC key.
boundarystring, element‘clippingParents’Overflow constraint boundary of the dropdown menu (applies only to Popper’s preventOverflow modifier). By default it’s clippingParents and can accept an HTMLElement reference (via JavaScript only). For more information refer to Popper’s detectOverflow docs.
displaystring‘dynamic’By default, we use Popper for dynamic positioning. Disable this with static.
offsetarray, string, function[0, 2]Offset of the dropdown relative to its target. You can pass a string in data attributes with comma separated values like: data-bs-offset=”10,20”. When a function is used to determine the offset, it is called with an object containing the popper placement, the reference, and popper rects as its first argument. The triggering element DOM node is passed as the second argument. The function must return an array with two numbers: skidding, distance. For more information refer to Popper’s offset docs.
popperConfignull, object, functionnullTo change Bootstrap’s default Popper config, see Popper’s configuration. When a function is used to create the Popper configuration, it’s called with an object that contains the Bootstrap’s default Popper configuration. It helps you use and merge the default with your own configuration. The function must return a configuration object for Popper.
referencestring, element, object‘toggle’Reference element of the dropdown menu. Accepts the values of ‘toggle’, ‘parent’, an HTMLElement reference or an object providing getBoundingClientRect. For more information refer to Popper’s constructor docs and virtual element docs.

Using function with popperConfig

  1. const dropdown = new bootstrap.Dropdown(element, {
  2. popperConfig(defaultBsPopperConfig) {
  3. // const newPopperConfig = {...}
  4. // use defaultBsPopperConfig if needed...
  5. // return newPopperConfig
  6. }
  7. })

Methods

MethodDescription
disposeDestroys an element’s dropdown. (Removes stored data on the DOM element)
getInstanceStatic method which allows you to get the dropdown instance associated to a DOM element, you can use it like this: bootstrap.Dropdown.getInstance(element).
getOrCreateInstanceStatic method which returns a dropdown instance associated to a DOM element or create a new one in case it wasn’t initialized. You can use it like this: bootstrap.Dropdown.getOrCreateInstance(element).
hideHides the dropdown menu of a given navbar or tabbed navigation.
showShows the dropdown menu of a given navbar or tabbed navigation.
toggleToggles the dropdown menu of a given navbar or tabbed navigation.
updateUpdates the position of an element’s dropdown.

Events

All dropdown events are fired at the toggling element and then bubbled up. So you can also add event listeners on the .dropdown-menu’s parent element. hide.bs.dropdown and hidden.bs.dropdown events have a clickEvent property (only when the original Event type is click) that contains an Event Object for the click event.

Event typeDescription
hide.bs.dropdownFires immediately when the hide instance method has been called.
hidden.bs.dropdownFired when the dropdown has finished being hidden from the user and CSS transitions have completed.
show.bs.dropdownFires immediately when the show instance method is called.
shown.bs.dropdownFired when the dropdown has been made visible to the user and CSS transitions have completed.
  1. const myDropdown = document.getElementById('myDropdown')
  2. myDropdown.addEventListener('show.bs.dropdown', event => {
  3. // do something...
  4. })