Powered by NeGD | MeitY Government of India® UX4G
Dropdowns
Toggle contextual overlays for displaying lists of links and more with the UX4G dropdown plugin.
Overview #
Toggleable dropdowns, contextual overlays that show lists of links, and more are available. The integrated UX4G dropdown JavaScript plugin makes them interactive. Toggling them requires clicking rather than hovering; this was done on purpose.
Dropdowns are constructed using Popper, a third-party module that offers dynamic placement and viewport detection. Make sure to use popper.min.js
or UX4G.bundle.min.js/UX4G.bundle.js
, which contain Popper, before UX4G's JavaScript. However, since dynamic positioning is not necessary, Popper
is not used to place dropdowns in navbars.
Accessibility #
The role="menu"
widget is defined by the WAI ARIA standard, although it is only applicable to application-like menus that initiate actions or functions. Only menu items, checkbox items, radio button items, radio button groups, and sub-menus are permitted in ARIA menus.
On the other hand, UX4G's dropdowns are made to be universal and adaptable to a range of scenarios and markup structures. For instance, dropdowns containing additional inputs and form elements, like login forms or search fields, can be made. Due to this, UX4G neither anticipates nor adds any role or aria-attributes necessary for authentic ARIA menus. These more detailed features will have to be included by the authors themselves.
Examples #
Wrap the dropdown menu and the toggle (your button or link) within any element with the declaration "position: relative;"
such as a .dropdown
. For a better fit with the prospective requirements, dropdowns can be activated via <a>
or <button>
elements. While custom markup is available, the examples shown here use semantic <ul>
components where necessary.
Single button #
With a few markup adjustments, any single .btn
can be transformed into a dropdown toggle. How to use them with either <button>
element is as follows:
<div class="dropdown"><button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Dropdown button</button><ul class="dropdown-menu"><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Another action</a></li><li><a class="dropdown-item" href="#">Something else here</a></li></ul></div>
RESULT
And with <a>
elements:
<div class="dropdown"><a class="btn btn-secondary dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">Dropdown link</a><ul class="dropdown-menu"><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Another action</a></li><li><a class="dropdown-item" href="#">Something else here</a></li></ul></div>
RESULT
The best part is you can do this with any button variant, too:
<div class="btn-group"><button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Primary</button><ul class="dropdown-menu"><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Another action</a></li><li><a class="dropdown-item" href="#">Something else here</a></li><li><hr class="dropdown-divider"/></li><li><a class="dropdown-item" href="#">Separated link</a></li></ul><button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Secondary</button><ul class="dropdown-menu"><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Another action</a></li><li><a class="dropdown-item" href="#">Something else here</a></li><li><hr class="dropdown-divider"/></li><li><a class="dropdown-item" href="#">Separated link</a></li></ul><button type="button" class="btn btn-success dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Success</button><ul class="dropdown-menu"><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Another action</a></li><li><a class="dropdown-item" href="#">Something else here</a></li><li><hr class="dropdown-divider"/></li><li><a class="dropdown-item" href="#">Separated link</a></li></ul><button type="button" class="btn btn-info dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Info</button><ul class="dropdown-menu"><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Another action</a></li><li><a class="dropdown-item" href="#">Something else here</a></li><li><hr class="dropdown-divider"/></li><li><a class="dropdown-item" href="#">Separated link</a></li></ul><button type="button" class="btn btn-warning dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Warning</button><ul class="dropdown-menu"><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Another action</a></li><li><a class="dropdown-item" href="#">Something else here</a></li><li><hr class="dropdown-divider"/></li><li><a class="dropdown-item" href="#">Separated link</a></li></ul><button type="button" class="btn btn-danger dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Danger</button><ul class="dropdown-menu"><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Another action</a></li><li><a class="dropdown-item" href="#">Something else here</a></li><li><hr class="dropdown-divider"/></li><li><a class="dropdown-item" href="#">Separated link</a></li></ul></div>
RESULT
Split button #
Similar to single button dropdowns, split button dropdowns can be made by using the same syntax, but with the addition of. For adequate space around the dropdown caret, use dropdown-toggle-split
.
Using this additional class, it is possible to eliminate the margin-left
that is provided for standard button dropdowns and lower the horizontal padding on each side of the caret by 25%. These additional adjustments retain the caret in the split button's center and create a larger hit area close to the main button.
<div class="btn-group"><button type="button" class="btn btn-danger">Danger</button><button type="button" class="btn btn-danger dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false"><span class="visually-hidden">Toggle Dropdown</span></button><button type="button" class="btn btn-primary">Primary</button><button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false"><span class="visually-hidden">Toggle Dropdown</span></button><button type="button" class="btn btn-secondary">Secondary</button><button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false"><span class="visually-hidden">Toggle Dropdown</span></button><button type="button" class="btn btn-success">Success</button><button type="button" class="btn btn-success dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false"><span class="visually-hidden">Toggle Dropdown</span></button><button type="button" class="btn btn-info">Info</button><button type="button" class="btn btn-info dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false"><span class="visually-hidden">Toggle Dropdown</span></button><button type="button" class="btn btn-warning">Warning</button><button type="button" class="btn btn-warning dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false"><span class="visually-hidden">Toggle Dropdown</span></button><ul class="dropdown-menu"><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Another action</a></li><li><a class="dropdown-item" href="#">Something else here</a></li><li><hr class="dropdown-divider"/></li><li><a class="dropdown-item" href="#">Separated link</a></li></ul></div>
RESULT
Sizing #
All button sizes, including split and default dropdown buttons, are supported by button dropdowns.
<div class="btn-group">
<button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Large button
</button>
<ul class="dropdown-menu">
...
</ul>
</div>
<div class="btn-group">
<button class="btn btn-secondary btn-lg" type="button">
Large split button
</button>
<button type="button" class="btn btn-lg btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
...
</ul>
</div>
RESULT
<div class="btn-group">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Small button
</button>
<ul class="dropdown-menu">
...
</ul>
</div>
<div class="btn-group">
<button class="btn btn-secondary btn-sm" type="button">
Small split button
</button>
<button type="button" class="btn btn-sm btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
...
</ul>
</div>
RESULT
Dark dropdowns #
With the addition of .dropdown-menu-dark
to an existing .dropdown-menu
, developers may choose to use darker dropdowns to go with a dark navbar or custom style. The dropdown options don't need to be changed.
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown button
</button>
<ul class="dropdown-menu dropdown-menu-dark">
<li><a class="dropdown-item active" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
<li><hr class="dropdown-divider"/></li>
<li><a class="dropdown-item" href="#">Separated link</a></li>
</ul>
</div>
RESULT
And putting it to use in a navbar:
<nav class="navbar navbar-expand-lg navbar-dark bg-dark"><div class="container-fluid"><a class="navbar-brand" href="#">Navbar</a><button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDarkDropdown" aria-controls="navbarNavDarkDropdown" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse" id="navbarNavDarkDropdown"><ul class="navbar-nav"><li class="nav-item dropdown"><a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</a><ul class="dropdown-menu dropdown-menu-dark"><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Another action</a></li><li><a class="dropdown-item" href="#">Something else here</a></li></ul></li></ul></div></div></nav>
RESULT
Directions #
RTL ​
Directions are mirrored when using UX4G in RTL, meaning
.dropstart
will appear on the right side.
Centered #
Under the toggle, center the drop-down menu with on the parent element, use .dropdown-center
.
<div class="dropdown-center"><button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Centered dropdown</button><ul class="dropdown-menu"><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Action two</a></li><li><a class="dropdown-item" href="#">Action three</a></li></ul></div>
RESULT
Dropup #
Dropdown menus can be activated above elements by adding the class .dropup to the parent element.
<div class="btn-group dropup">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropup
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
</ul>
</div>
<div class="btn-group dropup">
<button type="button" class="btn btn-secondary">
Split dropup
</button>
<button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
</ul>
</div>
RESULT
Dropup centered #
With, center the drop-down menu over the toggle the parent element's .dropup-center property
.
<div class="dropup-center dropup">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Centered dropup
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Action two</a></li>
<li><a class="dropdown-item" href="#">Action three</a></li>
</ul>
</div>
RESULT
Dropend #
Add .dropstart
to the parent element to start dropdown menus to the left of the elements.
<!-- Default dropend button -->
<div class="btn-group dropend">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropend
</button>
<ul class="dropdown-menu">
<!-- Dropdown menu links -->
</ul>
</div>
<!-- Split dropend button -->
<div class="btn-group dropend">
<button type="button" class="btn btn-secondary">
Split dropend
</button>
<button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropend</span>
</button>
<ul class="dropdown-menu">
<!-- Dropdown menu links -->
</ul>
</div>
RESULT
Dropstart #
Trigger dropdown menus at the left of the
elements by adding .dropstart
to the parent
element.
<div class="btn-group dropstart">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropstart
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
</ul>
</div>
<div class="btn-group dropstart">
<button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropstart</span>
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
</ul>
<button type="button" class="btn btn-secondary">
Split dropstart
</button>
</div>
RESULT
Menu items #
With .dropdown-item-text
, you can also create non-interactive
dropdown items. Please feel free to style text using special CSS or other tools.
<div class="dropdown"><button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button><ul class="dropdown-menu"><li><button class="dropdown-item" type="button">Action</button></li><li><button class="dropdown-item" type="button">Another action</button></li><li><button class="dropdown-item" type="button">Something else here</button></li></ul></div>
RESULT
You can also create non-interactive
dropdown items with .dropdown-item-text
. Feel free
to style further with custom CSS or text utilities.
<ul class="dropdown-menu"><li><span class="dropdown-item-text">Dropdown item text</span></li><li><a class="dropdown-item" href="#">Action</a></li><li><a class="dropdown-item" href="#">Another action</a></li><li><a class="dropdown-item" href="#">Something else here</a></li></ul>
RESULT
Active #
To style items in the dropdown as active, add the class .active
to them. Use the aria-current
attribute
with the page value for the current page or true for the current item in a set to communicate the active
status to assistive technologies.
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Regular link</a></li>
<li><a class="dropdown-item active" href="#" aria-current="true">Active link</a></li>
<li><a class="dropdown-item" href="#">Another link</a></li>
</ul>
RESULT
Disabled #
To style items in the dropdown as disabled, add the class .disabled
to them.
<ul class="dropdown-menu"><li><a class="dropdown-item" href="#">Regular link</a></li><li><a class="dropdown-item disabled">Disabled link</a></li><li><a class="dropdown-item" href="#">Another link</a></li></ul>
RESULT
Menu alignment #
A dropdown menu's default placement is 100% from the top and along the parent's left side. The directional.drop*
classes allows to alter this, but developers can additionally exert control over them using extra modifier classes.
To right align a .dropdown-menu
, add .dropdown-menu-end
to the end of the dropdown menu. When using UX4G in RTL, directions are mirrored, so .dropdown-menu-end
will show up on the left.
<div class="btn-group"><button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Right-aligned menu example</button><ul class="dropdown-menu dropdown-menu-end"><li><button class="dropdown-item" type="button">Action</button></li><li><button class="dropdown-item" type="button">Another action</button></li><li><button class="dropdown-item" type="button">Something else here</button></li></ul></div>
RESULT
Responsive alignment #
Employ the responsive
variation classes and data-bs-display="static"
to deactivate dynamic positioning if developers want to employ responsive alignment.
Add .dropdown-menu{-sm|-md|-lg|-xl|-xxl}
-end to align the dropdown menu to the right with the specified breakpoint or larger.
<div class="btn-group"><button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">Left-aligned but right aligned when large screen</button><ul class="dropdown-menu dropdown-menu-lg-end"><li><button class="dropdown-item" type="button">Action</button></li><li><button class="dropdown-item" type="button">Another action</button></li><li><button class="dropdown-item" type="button">Something else here</button></li></ul></div>
RESULT
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
.
<div class="btn-group"><button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">Right-aligned but left aligned when large screen</button><ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-start"><li><button class="dropdown-item" type="button">Action</button></li><li><button class="dropdown-item" type="button">Another action</button></li><li><button class="dropdown-item" type="button">Something else here</button></li></ul></div>
RESULT
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 #
Here is a little kitchen-sink
demonstration of the various dropdown alignment possibilities, using the majority of the options from the above list.
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Right-aligned menu
</button>
<ul class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
Left-aligned, right-aligned lg
</button>
<ul class="dropdown-menu dropdown-menu-lg-end">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
Right-aligned, left-aligned lg
</button>
<ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-start">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group dropstart">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropstart
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group dropend">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropend
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group dropup">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropup
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
RESULT
Menu content #
Headers #
Add a header to label sections of actions in any dropdown menu.
<ul class="dropdown-menu">
<li><h6 class="dropdown-header">Dropdown header</h6></li>
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
</ul>
RESULT
-
Dropdown header
- Action
- Another action
Dividers #
Separate groups of related menu items with a divider.
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
<li><hr class="dropdown-divider"/></li>
<li><a class="dropdown-item" href="#">Separated link</a></li>
</ul>
RESULT
Text #
Use spacing tools to insert any freeform text
into a dropdown menu with text. Keep in mind that extra sizing styles will probably be required to limit the menu's width.
<div class="dropdown-menu p-4 text-muted" styles="max-width: 200px;"><p>Some example text that's free-flowing within the dropdown menu.</p><p class="mb-0">And this is more example text.</p></div>
RESULT
Some example text that's free-flowing within the dropdown menu.
And this is more example text.
Forms #
Use margin or padding tools to give a form the necessary negative space before putting it inside or making it into a dropdown menu
.
<div class="dropdown-menu">
<form class="px-4 py-3">
<div class="mb-3">
<label for="exampleDropdownFormEmail1" class="form-label">Email address</label>
<input type="email" class="form-control" id="exampleDropdownFormEmail1" placeholder="email@example.com"/>
</div>
<div class="mb-3">
<label for="exampleDropdownFormPassword1" class="form-label">Password</label>
<input type="password" class="form-control" id="exampleDropdownFormPassword1" placeholder="Password"/>
</div>
<div class="mb-3">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="dropdownCheck"/>
<label class="form-check-label" for="dropdownCheck">
Remember me
</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">New around here? Sign up</a>
<a class="dropdown-item" href="#">Forgot password?</a>
</div>
RESULT
<div class="dropdown"><button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false" data-bs-auto-close="outside">Dropdown form</button><form class="dropdown-menu p-4"><div class="mb-3"><label for="exampleDropdownFormEmail2" class="form-label">Email address</label><input type="email" class="form-control" id="exampleDropdownFormEmail2" placeholder="email@example.com"/></div><div class="mb-3"><label for="exampleDropdownFormPassword2" class="form-label">Password</label><input type="password" class="form-control" id="exampleDropdownFormPassword2" placeholder="Password"/></div><div class="mb-3"><div class="form-check"><input type="checkbox" class="form-check-input" id="dropdownCheck2"/><label class="form-check-label" for="dropdownCheck2">Remember me</label></div></div><button type="submit" class="btn btn-primary">Sign in</button></form></div>
RESULT
Dropdown options #
Use data-bs-offset
or
data-bs-reference
to change the location of the
dropdown.
<div class="d-flex">
<div class="dropdown me-1">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false" data-bs-offset="10,20">
Offset
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-secondary">Reference</button>
<button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false" data-bs-reference="parent">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Separated link</a></li>
</ul>
</div>
</div>
RESULT
Auto close behavior #
When a click is made inside or outside the dropdown menu, it closes
by default. To alter this dropdown's behavior, utilize the autoClose
option.
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="true" aria-expanded="false">
Default dropdown
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="inside" aria-expanded="false">
Clickable outside
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">
Clickable inside
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="false" aria-expanded="false">
Manual close
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
RESULT
CSS #
Variables #
Added in v1.0.1Dropdowns now employ local CSS variables on .dropdown-menu as part of UX4G's expanding approach to CSS variables for improved real-time customisation. Sass is used to set the values for the CSS variables
, therefore Sass customization is still available.
--#{$prefix}dropdown-zindex: #{$zindex-dropdown};
--#{$prefix}dropdown-min-width: #{$dropdown-min-width};
--#{$prefix}dropdown-padding-x: #{$dropdown-padding-x};
--#{$prefix}dropdown-padding-y: #{$dropdown-padding-y};
--#{$prefix}dropdown-spacer: #{$dropdown-spacer};
@include rfs($dropdown-font-size, --#{$prefix}dropdown-font-size);
--#{$prefix}dropdown-color: #{$dropdown-color};
--#{$prefix}dropdown-bg: #{$dropdown-bg};
--#{$prefix}dropdown-border-color: #{$dropdown-border-color};
--#{$prefix}dropdown-border-radius: #{$dropdown-border-radius};
--#{$prefix}dropdown-border-width: #{$dropdown-border-width};
--#{$prefix}dropdown-inner-border-radius: #{$dropdown-inner-border-radius};
--#{$prefix}dropdown-divider-bg: #{$dropdown-divider-bg};
--#{$prefix}dropdown-divider-margin-y: #{$dropdown-divider-margin-y};
--#{$prefix}dropdown-box-shadow: #{$dropdown-box-shadow};
--#{$prefix}dropdown-link-color: #{$dropdown-link-color};
--#{$prefix}dropdown-link-hover-color: #{$dropdown-link-hover-color};
--#{$prefix}dropdown-link-hover-bg: #{$dropdown-link-hover-bg};
--#{$prefix}dropdown-link-active-color: #{$dropdown-link-active-color};
--#{$prefix}dropdown-link-active-bg: #{$dropdown-link-active-bg};
--#{$prefix}dropdown-link-disabled-color: #{$dropdown-link-disabled-color};
--#{$prefix}dropdown-item-padding-x: #{$dropdown-item-padding-x};
--#{$prefix}dropdown-item-padding-y: #{$dropdown-item-padding-y};
--#{$prefix}dropdown-header-color: #{$dropdown-header-color};
--#{$prefix}dropdown-header-padding-x: #{$dropdown-header-padding-x};
--#{$prefix}dropdown-header-padding-y: #{$dropdown-header-padding-y};
The .dropdown-menu-dark
class demonstrates how CSS variables can be customized by overriding particular values without introducing extra CSS selectors.
--#{$prefix}dropdown-color: #{$dropdown-dark-color};
--#{$prefix}dropdown-bg: #{$dropdown-dark-bg};
--#{$prefix}dropdown-border-color: #{$dropdown-dark-border-color};
--#{$prefix}dropdown-box-shadow: #{$dropdown-dark-box-shadow};
--#{$prefix}dropdown-link-color: #{$dropdown-dark-link-color};
--#{$prefix}dropdown-link-hover-color: #{$dropdown-dark-link-hover-color};
--#{$prefix}dropdown-divider-bg: #{$dropdown-dark-divider-bg};
--#{$prefix}dropdown-link-hover-bg: #{$dropdown-dark-link-hover-bg};
--#{$prefix}dropdown-link-active-color: #{$dropdown-dark-link-active-color};
--#{$prefix}dropdown-link-active-bg: #{$dropdown-dark-link-active-bg};
--#{$prefix}dropdown-link-disabled-color: #{$dropdown-dark-link-disabled-color};
--#{$prefix}dropdown-header-color: #{$dropdown-dark-header-color};
Sass variables #
Variables for all dropdowns:
$dropdown-min-width: 10rem;
$dropdown-padding-x: 0;
$dropdown-padding-y: .5rem;
$dropdown-spacer: .125rem;
$dropdown-font-size: $font-size-base;
$dropdown-color: $body-color;
$dropdown-bg: $white;
$dropdown-border-color: var(--#{$prefix}border-color-translucent);
$dropdown-border-radius: $border-radius;
$dropdown-border-width: $border-width;
$dropdown-inner-border-radius: subtract($dropdown-border-radius, $dropdown-border-width);
$dropdown-divider-bg: $dropdown-border-color;
$dropdown-divider-margin-y: $spacer * .5;
$dropdown-box-shadow: $box-shadow;
$dropdown-link-color: $gray-900;
$dropdown-link-hover-color: shade-color($dropdown-link-color, 10%);
$dropdown-link-hover-bg: $gray-200;
$dropdown-link-active-color: $component-active-color;
$dropdown-link-active-bg: $component-active-bg;
$dropdown-link-disabled-color: $gray-500;
$dropdown-item-padding-y: $spacer * .25;
$dropdown-item-padding-x: $spacer;
$dropdown-header-color: $gray-600;
$dropdown-header-padding-x: $dropdown-item-padding-x;
$dropdown-header-padding-y: $dropdown-padding-y;
// fusv-disable
$dropdown-header-padding: $dropdown-header-padding-y $dropdown-header-padding-x; // Deprecated in v1.0.1
// fusv-enable
Variables for the dark dropdown:
$dropdown-dark-color: $gray-300;
$dropdown-dark-bg: $gray-800;
$dropdown-dark-border-color: $dropdown-border-color;
$dropdown-dark-divider-bg: $dropdown-divider-bg;
$dropdown-dark-box-shadow: null;
$dropdown-dark-link-color: $dropdown-dark-color;
$dropdown-dark-link-hover-color: $white;
$dropdown-dark-link-hover-bg: rgba($white, .15);
$dropdown-dark-link-active-color: $dropdown-link-active-color;
$dropdown-dark-link-active-bg: $dropdown-link-active-bg;
$dropdown-dark-link-disabled-color: $gray-500;
$dropdown-dark-header-color: $gray-500;
Variables for the CSS-based carets that indicate a dropdown’s interactivity:
$caret-width: .3em;
$caret-vertical-align: $caret-width * .85;
$caret-spacing: $caret-width * .85;
Mixins #
Mixins are used to generate the CSS-based
carets and can be found in scss/mixins/_caret.scss
.
@mixin caret-down {
border-top: $caret-width solid;
border-right: $caret-width solid transparent;
border-bottom: 0;
border-left: $caret-width solid transparent;
}
@mixin caret-up {
border-top: 0;
border-right: $caret-width solid transparent;
border-bottom: $caret-width solid;
border-left: $caret-width solid transparent;
}
@mixin caret-end {
border-top: $caret-width solid transparent;
border-right: 0;
border-bottom: $caret-width solid transparent;
border-left: $caret-width solid;
}
@mixin caret-start {
border-top: $caret-width solid transparent;
border-right: $caret-width solid;
border-bottom: $caret-width solid transparent;
}
@mixin caret($direction: down) {
@if $enable-caret {
&::after {
display: inline-block;
margin-left: $caret-spacing;
vertical-align: $caret-vertical-align;
content: "";
@if $direction == down {
@include caret-down();
} @else if $direction == up {
@include caret-up();
} @else if $direction == end {
@include caret-end();
}
}
@if $direction == start {
&::after {
display: none;
}
&::before {
display: inline-block;
margin-right: $caret-spacing;
vertical-align: $caret-vertical-align;
content: "";
@include caret-start();
}
}
&:empty::after {
margin-left: 0;
}
}
}
Usage #
The dropdown plugin controls hidden content (dropdown menus) by switching the .show
class on the parent .dropdown-menu
using data attributes or JavaScript. Use the data-bs-toggle="dropdown"
attribute consistently because it is necessary for dismissing dropdown menus at the application level.
touch-enabled
device provides empty mouseover handlers to the element's direct children. To get past an issue with iOS' event delegation that would otherwise prohibit a tap anywhere outside of the dropdown from activating the code that closes the dropdown, this admittedly unsightly workaround is required. These additional empty mouseover
handlers are eliminated after the dropdown is closed.
Via data attributes #
Add data-bs-toggle="dropdown" to a link or button to toggle a dropdown.
<div class="dropdown">
<button type="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown trigger
</button>
<ul class="dropdown-menu">
...
</ul>
</div>
Via JavaScript #
Call the dropdowns via JavaScript:
const dropdownElementList = document.querySelectorAll('.dropdown-toggle')
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 #
Data-bs-animation
can have an option name appended to it, for example, data-bs-animation="value,"
since options can be provided through data attributes or JavaScript. When sending the choices via data attributes, be sure to modify the case type of the option name from "camelCase"
to "kebab-case."
Use data-bs-custom-class="beautifier"
as opposed to data-bs-customClass="beautifier"
for instance.
As of UX4G 1.0.0, every component is compatible with the experimental reserved data property data-bs-config, which can store a JSON string for straightforward component setting. When an element has the data-bs-config={"delay":0, "title":123}"
and data-bs-title="456"
attributes, the final title value will be 456 and the individual data attributes will take precedence over the values specified on the data-bs-config
. Additionally, JSON variables like data-bs-delay='{"show":0,"hide":150}'
can be stored in existing data attributes.
Name | Type | Default | Description |
---|---|---|---|
autoClose |
boolean, string | true |
Configure the auto close behavior of the
dropdown:
|
boundary |
string, element | 'scrollParent' |
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 .
|
offset |
number, 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.
|
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 UX4G 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 | '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
const dropdown = new bootstrap.Dropdown(element, {
popperConfig(defaultBsPopperConfig) {
// const newPopperConfig = {...}
// use defaultBsPopperConfig if needed...
// return newPopperConfig
}
})
Methods #
Method | Description |
---|---|
dispose |
Destroys an element’s dropdown. (Removes stored data on the DOM element) |
getInstance |
Static method which allows you to get the
dropdown instance associated to a DOM element,
you can use it like this:
bootstrap.Dropdown.getInstance(element)
|
getOrCreateInstance |
Static 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) .
|
hide |
Hides the dropdown menu of a given navbar or tabbed navigation. |
show |
Shows the dropdown menu of a given navbar or tabbed navigation. |
toggle |
Toggles the dropdown menu of a given navbar or tabbed navigation. |
update |
Updates 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 type | Description |
---|---|
hide.bs.dropdown |
Fires immediately when the hide
instance method has been called. |
hidden.bs.dropdown |
Fired when the dropdown has finished being hidden from the user and CSS transitions have completed. |
show.bs.dropdown |
Fires immediately when the show
instance method is called. |
shown.bs.dropdown |
Fired when the dropdown has been made visible to the user and CSS transitions have completed. |
const myDropdown = document.getElementById('myDropdown')
myDropdown.addEventListener('show.bs.dropdown', event => {
// do something...
})