To create a multi-level drop-down menu in the Bootstrap navigation bar, 1. You need to build a multi-level menu through a nested
- structure; 2. Add a custom CSS to control the submenu position, such as setting the relative positioning of .dropdown-submenu and the left offset of .dropdown-menu; 3. Use data-bs-toggle="dropdown" or custom JS to implement the submenu expansion logic, such as clicking to switch the display status; 4. Optimizing mobile interaction, it is recommended to click to expand, limit the levels, and consider using the folded menu to adapt to touch screen operations.
Bootstrap's navigation bar (navbar) only supports single-layer drop-down menus by default, but many times we want to add multi-level submenu to the navigation (such as expanding the second or even the third level menu after mouse hovering or clicking). It is actually not difficult to implement this function. It mainly extends the native functions of Bootstrap through custom CSS and JS.

The following are the specific operation methods and precautions:
How to create a multi-level drop-down menu in the Bootstrap navigation bar
Bootstrap itself does not directly support multi-level drop-down menus, so you need to manually modify the HTML structure and cooperate with some CSS or JavaScript to make the secondary menu appear. The key point is to correctly nest <ul></ul>
elements and control how they appear.

The basic structure is as follows:
<ul class="navbar-nav"> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown">First-level menu</a> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Submenu1</a></li> <li class="dropdown-submenu"> <a class="dropdown-item dropdown-toggle" href="#" data-bs-toggle="dropdown">Items with submenu</a> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Second-level menu 1</a></li> <li><a class="dropdown-item" href="#">Second-level menu 2</a></li> </ul> </li> </ul> </li> </ul>
This will build a basic multi-level structure, but it also requires style support to display it normally.

Add CSS style to control the position of submenu
By default, Bootstrap's .dropdown-menu
is hidden, and even if you set up a nested .dropdown-menu
, it will appear below the parent menu, not next to it.
You need to add a CSS to make it display horizontally and position it to the right place:
.dropdown-submenu { position: relative; } .dropdown-submenu .dropdown-menu { top: 0; left: 100%; margin-top: -1px; }
This CSS allows the submenu to pop up to the right and display it against the parent menu. If you want to pop up from right to left, you can change it to right: 100%
.
Use JavaScript to control how submenu is triggered
Although Bootstrap provides data-bs-toggle="dropdown"
attribute to control the display of the primary menu, the default is not automatically expanded for the secondary menu. You can choose the following ways to deal with it:
- Dropdown plugin using Bootstrap : Add
data-bs-toggle="dropdown"
attribute to the link to the submenu. - Add custom JS : prevents the menu from closing when clicked, or automatically expanding when hovering.
For example:
document.querySelectorAll('.dropdown-submenu > a').forEach(link => { link.addEventListener('click', function (e) { e.stopPropagation(); e.preventDefault(); const submenu = this.nextElementSibling; submenu.classList.toggle('show'); }); });
This allows the user to manually switch the display status of the submenu when clicking an item with a submenu.
Mobile adaptation and interaction optimization
Multi-level menus may not be very friendly on mobile, especially when they are nested too deeply. It is recommended to make the following optimizations:
- Disable the hover behavior of the submenu on the small screen and click to expand instead.
- Avoid nesting too many levels, it is best to have two layers at most.
- Consider using a folding menu (Collapse) instead of a drop-down menu, which is more suitable for touch screen operations.
Basically that's it. The key to implementing multi-level drop-down menus is structural nesting, CSS positioning and appropriate interaction logic. It doesn't seem complicated, but it's easy to ignore details in actual projects, especially the responsive design part.
The above is the detailed content of How to create a multi-level dropdown in Bootstrap navbar?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The advantage of creating forms with Bootstrap is that it provides a consistent and responsive design, saving time, and ensuring cross-device compatibility. 1) Basic forms are simple to use, such as form-control and btn classes. 2) Vertical forms achieve a more structured layout through grid classes (such as col-sm-2 and col-sm-10).

Bootstrapformscanleadtoerrorslikemisusingthegridsystem,improperformcontrols,validationissues,neglectingcustomCSS,accessibility,andperformance.Toavoidthese:1)Usecolumnclasseslikecol-sm-orcol-md-forresponsiveness;2)Wrapinputfieldsin.form-groupforproper

Bootstrap'sGridSystemisessentialforcreatingresponsive,modernwebsites.1)Itusesa12-columnlayoutforflexiblecontentdisplay.2)Columnsaredefinedwithinrowsinsideacontainer,withwidthslikecol-6forhalf-width.3)Responsivenessisachievedusingclasseslikecol-sm-6fo

Bootstrapformtemplatesareidealforquickwinsduetotheirsimplicity,flexibility,andeaseofcustomization.1)UseacleanlayoutwithBootstrap'sform-groupandform-controlclassesfororganizedandconsistentstyling.2)Customizecolors,sizes,andlayouttofityourbrandbyoverri

Bootstrap'sGridSystemhelpsinbuildingresponsivelayoutsbyofferingflexibilityandeaseofuse.1)Itallowsquickcreationofadaptablelayoutsacrossdevices.2)Advancedfeatureslikenestedrowsenablecomplexdesigns.3)Itencouragesaresponsivedesignphilosophy,enhancingcont

BootstrapGridSystemisapowerfultoolforcreatingresponsive,mobile-firstlayouts.1)Itusesa12-columngridwithclasseslike'row'and'col'forstructuringcontent.2)Breakpointslike'col-sm-6'or'col-md-4'allowlayoutstoadapttodifferentscreensizes.3)Nestinggridsandusin

The steps to create a navigation bar using Bootstrap include: 1. Create an initial navigation bar using the basic navbar component. 2. Customize styles through Bootstrap's utility class and custom CSS. 3. Ensure the navigation bar is responsive on different devices. 4. Add advanced features to the pull-down menu and search bar. 5. Test and optimize the performance and user experience of the navigation bar. With these steps, you can create a powerful and beautiful navigation bar with Bootstrap.

There are three ways to install and use BootstrapIcons: 1. Use CDN and add links to the HTML head; 2. Install through npm, suitable for modern projects such as React and Vue. You need to run npminstallbootstrap-icons and import CSS; 3. Manually download SVG or font files and import them. When using it, you can add bi and icon name classes (such as bi-heart) to insert icons. You can also use other inline elements such as span. It is recommended to use SVG files for better performance and customization capabilities. You can adjust the size through bi-lg, bi-2x and other classes, and use Bootstrap text such as text-danger.
