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

  1. <div class="dropdown">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown button
  4. </button>
  5. <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
  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

  1. <div class="dropdown">
  2. <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown link
  4. </a>
  5. <ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
  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

  1. <div class="dropdown">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton2" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown button
  4. </button>
  5. <ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="dropdownMenuButton2">
  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

  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="#" id="navbarDarkDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  11. Dropdown
  12. </a>
  13. <ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDarkDropdownMenuLink">
  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.

Dropup

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

Dropdowns - 图9

  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>

Dropright

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

Dropdowns - 图10

  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. Dropright
  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 Dropright</span>
  17. </button>
  18. <ul class="dropdown-menu">
  19. <!-- Dropdown menu links -->
  20. </ul>
  21. </div>

Dropleft

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

Dropdowns - 图11

  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">
  12. <div class="btn-group dropstart" role="group">
  13. <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
  14. <span class="visually-hidden">Toggle Dropstart</span>
  15. </button>
  16. <ul class="dropdown-menu">
  17. <!-- Dropdown menu links -->
  18. </ul>
  19. </div>
  20. <button type="button" class="btn btn-secondary">
  21. Split dropstart
  22. </button>
  23. </div>

Menu items

Historically dropdown menu contents had to be links, but that’s no longer the case with v4. Now you can optionally use <button> elements in your dropdowns instead of just <a>s.

Dropdowns - 图12

  1. <div class="dropdown">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown
  4. </button>
  5. <ul class="dropdown-menu" aria-labelledby="dropdownMenu2">
  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 - 图13

  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 - 图14

  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 - 图15

  1. <ul class="dropdown-menu">
  2. <li><a class="dropdown-item" href="#">Regular link</a></li>
  3. <li><a class="dropdown-item disabled" href="#" tabindex="-1" aria-disabled="true">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 - 图16

  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 - 图17

  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 - 图18

  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 - 图19

  1. <div class="btn-group">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown
  4. </button>
  5. <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
  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 - 图20

  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 - 图21

  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 - 图22

  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 - 图23

  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 - 图24

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

Dropdown options

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

Dropdowns - 图25

  1. <div class="d-flex">
  2. <div class="dropdown me-1">
  3. <button type="button" class="btn btn-secondary dropdown-toggle" id="dropdownMenuOffset" data-bs-toggle="dropdown" aria-expanded="false" data-bs-offset="10,20">
  4. Offset
  5. </button>
  6. <ul class="dropdown-menu" aria-labelledby="dropdownMenuOffset">
  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" id="dropdownMenuReference" 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" aria-labelledby="dropdownMenuReference">
  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 - 图26

  1. <div class="btn-group">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" id="defaultDropdown" data-bs-toggle="dropdown" data-bs-auto-close="true" aria-expanded="false">
  3. Default dropdown
  4. </button>
  5. <ul class="dropdown-menu" aria-labelledby="defaultDropdown">
  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" id="dropdownMenuClickableOutside" data-bs-toggle="dropdown" data-bs-auto-close="inside" aria-expanded="false">
  13. Clickable outside
  14. </button>
  15. <ul class="dropdown-menu" aria-labelledby="dropdownMenuClickableOutside">
  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" id="dropdownMenuClickableInside" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">
  23. Clickable inside
  24. </button>
  25. <ul class="dropdown-menu" aria-labelledby="dropdownMenuClickableInside">
  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" id="dropdownMenuClickable" data-bs-toggle="dropdown" data-bs-auto-close="false" aria-expanded="false">
  33. Manual close
  34. </button>
  35. <ul class="dropdown-menu" aria-labelledby="dropdownMenuClickable">
  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>

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: rgba($black, .15);
  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 / 2;
  14. $dropdown-box-shadow: $box-shadow;
  15. $dropdown-link-color: $gray-900;
  16. $dropdown-link-hover-color: shade-color($gray-900, 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 / 4;
  22. $dropdown-item-padding-x: $spacer;
  23. $dropdown-header-color: $gray-600;
  24. $dropdown-header-padding: $dropdown-padding-y $dropdown-item-padding-x;

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 id="dLabel" type="button" data-bs-toggle="dropdown" aria-expanded="false">
  3. Dropdown trigger
  4. </button>
  5. <ul class="dropdown-menu" aria-labelledby="dLabel">
  6. ...
  7. </ul>
  8. </div>

Via JavaScript

Call the dropdowns via JavaScript:

  1. var dropdownElementList = [].slice.call(document.querySelectorAll('.dropdown-toggle'))
  2. var dropdownList = dropdownElementList.map(function (dropdownToggleEl) {
  3. return new bootstrap.Dropdown(dropdownToggleEl)
  4. })
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

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-bs-, as in data-bs-offset="". 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, instead of using data-bs-autoClose="false", use data-bs-auto-close="false".

NameTypeDefaultDescription
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.
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.
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.

autoCloseboolean | stringtrue

Configure 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.
popperConfignull | object | functionnull

To 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.

Using function with popperConfig

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

Methods

MethodDescription
toggleToggles the dropdown menu of a given navbar or tabbed navigation.
showShows the dropdown menu of a given navbar or tabbed navigation.
hideHides the dropdown menu of a given navbar or tabbed navigation.
updateUpdates the position of an element’s dropdown.
disposeDestroys an element’s dropdown. (Removes stored data on the DOM element)
getInstanceStatic method which allows you to get the dropdown instance associated with a DOM element.

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.

MethodDescription
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.
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.
  1. var myDropdown = document.getElementById('myDropdown')
  2. myDropdown.addEventListener('show.bs.dropdown', function () {
  3. // do something...
  4. })