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
Susan SarandonOriginal
2024-12-11 21:27:15525browse

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!

Statement:
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