Home > Web Front-end > CSS Tutorial > How can I create multi-level dropdowns in Bootstrap 4 navigation?

How can I create multi-level dropdowns in Bootstrap 4 navigation?

Susan Sarandon
Release: 2024-12-11 21:27:15
Original
634 people have browsed it

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;
}
Copy after login

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;
});
Copy after login

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">
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template