Menu·续
You can also use the .menu-scrollable class to make the menu scrollable. This assumes a generally safe maximum height of 80dvh and sets the overflow for you.
Submenus
Create nested menus with the .submenu wrapper class. Submenus support hover and click activation, keyboard navigation, and viewport-aware positioning. On small screens (below 576px), submenus stack over the parent menu with a back button instead of flying out to the side.
Wrap a .menu-item trigger and a nested .menu inside a .submenu element.
Submenus </button> <div class="menu"> <div class="submenu"> <button class="menu-item" type="button">File</button> <div class="menu"> <a class="menu-item" href="#">New</a> <a class="menu-item" href="#">Open</a> <a class="menu-item" href="#">Save</a> </div> </div> <div class="submenu"> <button class="menu-item" type="button">Edit</button> <div class="menu"> <a class="menu-item" href="#">Cut</a> <a class="menu-item" href="#">Copy</a> <a class="menu-item" href="#">Paste</a> </div> </div> <div class="submenu"> <button class="menu-item" type="button">View</button> <div class="menu"> <a class="menu-item" href="#">Zoom In</a> <a class="menu-item" href="#">Zoom Out</a> </div> </div> <hr class="menu-divider"> <a class="menu-item" href="#">Preferences</a> </div>`} />
Nested
Submenus can be nested to multiple levels. Each level opens to the side and flips direction when there’s not enough viewport space.
Multi-level menu </button> <div class="menu"> <a class="menu-item" href="#">Level 1 action</a> <div class="submenu"> <button class="menu-item" type="button">Level 1 submenu</button> <div class="menu"> <a class="menu-item" href="#">Level 2 action</a> <div class="submenu"> <button class="menu-item" type="button">Level 2 submenu</button> <div class="menu"> <a class="menu-item" href="#">Level 3 action A</a> <a class="menu-item" href="#">Level 3 action B</a> </div> </div> <a class="menu-item" href="#">Another level 2</a> </div> </div> <a class="menu-item" href="#">Another level 1</a> </div>`} />
Menu options
Use data-bs-offset or data-bs-reference to change the location of the menu. Offset is the distance from the trigger element in pixels. Reference is the element to reference the menu from.
<div class="me-1"> <button type="button" class="btn-solid theme-secondary" data-bs-toggle="menu" aria-expanded="false" data-bs-offset="10,20"> Offset </button> <div class="menu"> <a class="menu-item" href="#">Action</a> <a class="menu-item" href="#">Another action</a> <a class="menu-item" href="#">Something else here</a> </div> </div> <div class="btn-group"> <button type="button" class="btn-solid theme-secondary">Reference</button> <button type="button" class="btn-solid theme-secondary menu-toggle-split" data-bs-toggle="menu" aria-expanded="false" data-bs-reference="parent"> <svg class="bi" width="16" height="16" aria-hidden="true"><use href="#chevron-expand" /></svg> <span class="visually-hidden">Toggle Menu</span> </button> <div class="menu"> <a class="menu-item" href="#">Action</a> <a class="menu-item" href="#">Another action</a> <a class="menu-item" href="#">Something else here</a> <hr class="menu-divider"> <a class="menu-item" href="#">Separated link</a> </div> </div> </div>`} />
Auto close behavior
By default, the menu is closed when clicking inside or outside the menu. You can use the autoClose option to change this behavior of the menu.
Regardless of the autoClose setting, pressing <kbd>Esc</kbd> while focus is within the menu (its toggle or items) closes the menu and returns focus to the toggle.
<button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" data-bs-auto-close="true" aria-expanded="false"> Default menu </button> <div class="menu"> <a class="menu-item" href="#">Menu item</a> <a class="menu-item" href="#">Menu item</a> <a class="menu-item" href="#">Menu item</a> </div> </div>
<div class="btn-group"> <button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" data-bs-auto-close="inside" aria-expanded="false"> Clickable inside </button> <div class="menu"> <a class="menu-item" href="#">Menu item</a> <a class="menu-item" href="#">Menu item</a> <a class="menu-item" href="#">Menu item</a> </div> </div>
<div class="btn-group"> <button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" data-bs-auto-close="outside" aria-expanded="false"> Clickable outside </button> <div class="menu"> <a class="menu-item" href="#">Menu item</a> <a class="menu-item" href="#">Menu item</a> <a class="menu-item" href="#">Menu item</a> </div> </div>
<div class="btn-group"> <button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" data-bs-auto-close="false" aria-expanded="false"> Manual close </button> <div class="menu"> <a class="menu-item" href="#">Menu item</a> <a class="menu-item" href="#">Menu item</a> <a class="menu-item" href="#">Menu item</a> </div> </div>`} />
Accessibility
The <abbr title="Web Accessibility Initiative">WAI</abbr> <abbr title="Accessible Rich Internet Applications">ARIA</abbr> standard defines an actual role="menu" widget, but this is specific to application-like menus which trigger actions or functions. <abbr title="Accessible Rich Internet Applications">ARIA</abbr> menus can only contain menu items, checkbox menu items, radio button menu items, radio button groups, and sub-menus.
Bootstrap’s menus, 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 menus 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 <abbr title="Accessible Rich Internet Applications">ARIA</abbr> menus. Authors will have to include these more specific attributes themselves.
Submenu triggers are an exception. Bootstrap adds aria-haspopup="true" to each .submenu > .menu-item that wraps a nested .menu, and keeps aria-expanded on it in sync as the submenu opens and closes. Set aria-expanded yourself only if a submenu must start open.
Keyboard navigation
Menus include built-in keyboard support for navigating menu items.
| Key | Action |
|---|---|
↓ / ↑ |
Navigate to next/previous menu item |
→ |
Enter submenu (or ← in RTL) |
← |
Exit submenu and return to parent (or → in RTL) |
Enter / Space |
Activate focused item or open submenu |
Esc |
Close the menu |
Home / End |
Jump to first/last menu item |
Tab |
Move focus and close the menu |
CSS
Variables
Menu items include at least one variable that is not set on .menu. This allows you to provide a new value while Bootstrap defaults to a fallback value.
--bs-menu-item-border-radius
Usage
Via data attributes or JavaScript, the menu plugin toggles hidden content (menus) by toggling the .show class on the .menu. The data-bs-toggle="menu" attribute is relied on for closing menus at an application level, so it’s a good idea to always use it.
On touch-enabled devices, opening a menu 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 menu from triggering the code that closes the menu. Once the menu is closed, these additional empty mouseover handlers are removed.
Via data attributes
Add data-bs-toggle="menu" to a link or button to toggle a menu.
| Attribute | Description |
|---|---|
data-bs-toggle="menu" |
Initializes the menu on the trigger and wires global close behavior. |
<button type="button" data-bs-toggle="menu" aria-expanded="false">
Menu trigger
</button>
<div class="menu">
...
</div>
Via JavaScript
Menus must have data-bs-toggle="menu" on their trigger element, regardless of whether you call your menu via JavaScript or use the data-api.
Call the menus via JavaScript:
const menuElementList = document.querySelectorAll('[data-bs-toggle="menu"]')
const menuList = [...menuElementList].map(menuToggleEl => new bootstrap.Menu(menuToggleEl))
To open, close, or toggle a menu programmatically, get its instance from the trigger element (the one with data-bs-toggle="menu") and call the corresponding method:
const menuToggleEl = document.querySelector('#myMenuToggle')
const menu = bootstrap.Menu.getOrCreateInstance(menuToggleEl)
menu.show()
menu.hide()
menu.toggle()
Dependencies
Options
| Name | Type | Default | Description |
|---|---|---|---|
autoClose |
boolean, string | true |
Configure the auto close behavior of the menu: <ul class="my-2"><li>true - the menu will be closed by clicking outside or inside the menu.</li><li>false - the menu will be closed by clicking the toggle button and manually calling hide or toggle method. (Also will not be closed by pressing <kbd>Esc</kbd> key)</li><li>'inside' - the menu will be closed (only) by clicking inside the menu.</li> <li>'outside' - the menu will be closed (only) by clicking outside the menu.</li></ul> Note: the menu can always be closed with the <kbd>Esc</kbd> key. |
boundary |
string, element | 'clippingParents' |
Overflow constraint boundary of the menu (applies only to the shift middleware). By default it’s clippingParents and can accept an HTMLElement reference (via JavaScript only). For more information refer to Floating UI’s shift docs. |
container |
string, element, boolean | false |
Appends the menu to a specific element when shown. Use 'body' or true to append to the document body, which helps escape containers with overflow: hidden. The menu is moved back to its original position when hidden. |
display |
string | 'dynamic' |
By default, we use Floating UI for dynamic positioning. Disable this with static. |
offset |
array, string, function | [0, 2] |
Offset of the menu 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 placement, the reference, and floating 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 Floating UI’s offset docs. |
floatingConfig |
null, object, function | null |
To change Bootstrap’s default Floating UI config, see Floating UI’s configuration. When a function is used to create the Floating UI configuration, it’s called with an object that contains the Bootstrap’s default Floating UI configuration. It helps you use and merge the default with your own configuration. The function must return a configuration object for Floating UI. |
placement |
string | 'bottom-start' |
Placement of the menu. Physical placements: 'top', 'bottom', 'left', 'right'. Logical placements (RTL-aware): 'start', 'end'. All support alignment modifiers: -start, -end. Supports responsive prefixes like 'bottom-start md:end'. |
reference |
string, element, object | 'toggle' |
Reference element of the menu. Accepts the values of 'toggle', 'parent', an HTMLElement reference or an object providing getBoundingClientRect. For more information refer to Floating UI’s virtual elements docs. |
strategy |
string | 'absolute' |
Positioning strategy for the menu. Use 'absolute' for default positioning, or 'fixed' to escape containers with overflow: hidden. For more information refer to Floating UI’s strategy docs. |
submenuTrigger |
string | 'both' |
How submenus are triggered. Use 'click' for click only, 'hover' for hover only, or 'both' for both click and hover activation. |
submenuDelay |
number | 100 |
Delay in milliseconds before closing a submenu when the mouse leaves. Provides a grace period for diagonal mouse movement toward the submenu. |
Using function with floatingConfig
const menu = new bootstrap.Menu(element, {
floatingConfig(defaultBsFloatingConfig) {
// const newFloatingConfig = {...}
// use defaultBsFloatingConfig if needed...
// return newFloatingConfig
}
})
Methods
| Method | Description |
|---|---|
dispose |
Destroys an element’s menu. (Removes stored data on the DOM element) |
getInstance |
Static method which allows you to get the menu instance associated to a DOM element, you can use it like this: bootstrap.Menu.getInstance(element). |
getOrCreateInstance |
Static method which returns a menu instance associated to a DOM element or create a new one in case it wasn’t initialized. You can use it like this: bootstrap.Menu.getOrCreateInstance(element). |
hide |
Hides the menu of a given navbar or tabbed navigation. |
show |
Shows the menu of a given navbar or tabbed navigation. |
toggle |
Toggles the menu of a given navbar or tabbed navigation. |
update |
Updates the position of an element’s menu. |
Events
All menu events are fired at the toggling element and then bubbled up. So you can also add event listeners on the .menu's parent element. hide.bs.menu and hidden.bs.menu 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.menu |
Fires immediately when the hide instance method has been called. |
hidden.bs.menu |
Fired when the menu has finished being hidden from the user and CSS transitions have completed. |
show.bs.menu |
Fires immediately when the show instance method is called. |
shown.bs.menu |
Fired when the menu has been made visible to the user and CSS transitions have completed. |
const myMenu = document.getElementById('myMenu')
myMenu.addEventListener('show.bs.menu', event => {
// do something...
})
