Home >Web Front-end >CSS Tutorial >How to Create Multilevel Dropdowns in Bootstrap 4?
Creating multilevel dropdowns in Bootstrap 4 can be achieved with minimal CSS and JavaScript modifications. Here's how you can do it:
Ensure that your navigation contains nested
<nav class="navbar navbar-toggleable-md navbar-light bg-faded"> ... <div class="collapse navbar-collapse">
Add the following CSS to your stylesheet:
.dropdown-submenu { position: relative; } .dropdown-submenu a::after { transform: rotate(-90deg); position: absolute; right: 6px; top: .8em; } .dropdown-submenu .dropdown-menu { top: 0; left: 100%; margin-left: .1rem; margin-right: .1rem; }
Finally, add the following JavaScript:
$('.dropdown-menu a.dropdown-toggle').on('click', function(e) { if (!$(this).next().hasClass('show')) { $(this).parents('.dropdown-menu').first().find('.show').removeClass('show'); } var $subMenu = $(this).next('.dropdown-menu'); $subMenu.toggleClass('show'); $(this).parents('li.nav-item.dropdown.show').on('hidden.bs.dropdown', function(e) { $('.dropdown-submenu .show').removeClass('show'); }); return false; });
This approach enables you to create complex multilevel dropdowns seamlessly integrated into your Bootstrap 4 navigation. It ensures that only one sub-menu is open at a time, preventing overlaps or nesting conflicts.
The above is the detailed content of How to Create Multilevel Dropdowns in Bootstrap 4?. For more information, please follow other related articles on the PHP Chinese website!