Dropdowns
Bootstrap 5 Dropdown component
Responsive dropdown/caret built with Bootstrap 5. Examples of navbar dropdown, dropdown list, submenu, dropdown toggle, dropdown hover, hamburger icon & more.
Dropdowns are responsible for toggleable (collapsible) display a list of links.
Toggle contextual overlays for displaying lists of links and more with the MDB dropdown plugin.
Note: Read the API tab to find all available options and advanced customization
*
*
UMD autoinits are enabled
by default. This means that you don't need to initialize
the component manually. However if you are using MDBootstrap ES format then you should pass
the required components to the initMDB
method.
Basic examples
Wrap the dropdown’s toggle (your button or link) and the dropdown menu within
.dropdown
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.
Colors
You can do this with any button variant:
Sizing
Button dropdowns work with buttons of all sizes, including default and split dropdown buttons.
Directions
Dropup
Trigger dropdown menus above elements by adding .dropup
to the parent element.
Dropright
Trigger dropdown menus at the right of the elements by adding
.dropend
to the parent element.
Dropleft
Trigger dropdown menus at the left of the elements by adding
.dropstart
to the parent element.
Remove animation
To remove the fade animation on click, add
data-mdb-dropdown-animation="off"
attribute to the dropdown button.
Dropdown options
Use data-mdb-offset
or data-mdb-reference
to change the location of
the dropdown.
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 - API
Import
Importing components depends on how your application works. If you intend to use the MDBootstrap ES
format, you must
first import the component and then initialize it with the initMDB
method. If you are going to use the UMD
format,
just import the mdb-ui-kit
package.
Usage
Via data attributes or JavaScript, the dropdown plugin toggles hidden content (dropdown menus)
by toggling the .show
class on the parent list item.
Note:
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
Using the Dropdown component doesn't require any additional JavaScript code - simply add
data-mdb-dropdown-init
attribute to
.dropdown-toggle
and use other data attributes to set all options.
For ES
format, you must first import and call the initMDB
method.
Via JavaScript
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to
data-mdb-
, as in data-mdb-dropdown-animation="on"
.
Name | Type | Default | Description |
---|---|---|---|
autoClose |
boolean | string | true |
Configure the auto close behavior of the dropdown:
|
boundary |
string | 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.
|
display |
string | 'dynamic' |
By default, we use Popper for dynamic positioning. Disable this with
static .
|
dropdownAnimation |
string | 'on' |
Turn on / off dropdown animation. |
offset |
array | string | function | [0, 2] |
Offset of the dropdown relative to its target. When a function is used to determine the offset, it is called with an object containing the offset data as its first argument. The function must return an object with the same structure. The triggering element DOM node is passed as the second argument. For more information refer to Popper's offset docs. |
popperConfig |
null | object | function | null |
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. |
reference |
string | 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.
|
Note when boundary
is set to any value other than 'scrollParent'
,
the style position: static
is applied to the .dropdown
container.
Methods
Method | Description | Example |
---|---|---|
toggle |
Toggles the dropdown menu of a given navbar or tabbed navigation. | myDropdown.toggle() |
show |
Shows the dropdown menu of a given navbar or tabbed navigation. | myDropdown.show() |
hide |
Hides the dropdown menu of a given navbar or tabbed navigation. | myDropdown.hide() |
update |
Updates the position of an element's dropdown by recomputing dropdown position and updates it if needed. | myDropdown.update() |
dispose |
Destroys an element's dropdown. | myDropdown.dispose() |
getInstance |
Static method which allows you to get the dropdown instance associated with a DOM element. | Dropdown.getInstance() |
getOrCreateInstance |
Static method which allows you to get the dropdown instance associated with a DOM element or create a new one in case it wasn't initialized. | Dropdown.getOrCreateInstance() |
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.mdb.dropdown
and hidden.mdb.dropdown
events have a
clickEvent
property (only when the original Event type is click
)
that contains an Event Object for the click event.
Method | Description |
---|---|
show.mdb.dropdown
|
Fires immediately when the show instance method is called. |
shown.mdb.dropdown
|
Fired when the dropdown has been made visible to the user and CSS transitions have completed. |
hide.mdb.dropdown
|
Fires immediately when the hide instance method has been called. |
hidden.mdb.dropdown
|
Fired when the dropdown has finished being hidden from the user and CSS transitions have completed. |
CSS variables
As part of MDB’s evolving CSS variables approach, dropdown now uses local CSS variables on
.dropdown-menu
, .dropdown-item
and
.dropdown-divider
for enhanced real-time customization. Values for the CSS
variables are set via Sass, so Sass customization is still supported, too.