Home >Web Front-end >CSS Tutorial >How can I create multi-level dropdowns in Bootstrap 4 navigation?
Bootstrap 4: Unleashing Multilevel Dropdowns within Navigations
When seeking a seamless way to incorporate multilevel dropdowns into Bootstrap 4, look no further. The following CSS and JavaScript snippets will guide you through the process.
CSS for Dropdown Submenus
This CSS targets the creation of additional dropdown submenu styles.
.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; }
JavaScript for Submenu Toggle
This JavaScript handles toggling the visibility of dropdown submenus.
$('.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; });
HTML Example
Incorporating the CSS and JavaScript, this HTML example demonstrates a functional three-level dropdown.
<ul class="navbar-nav"> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="http://example.com">
With these steps, you can effortlessly create and customize multilevel dropdowns that enhance user navigation and interaction within Bootstrap 4.
The above is the detailed content of How can I create multi-level dropdowns in Bootstrap 4 navigation?. For more information, please follow other related articles on the PHP Chinese website!