国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
How to create a multi-level drop-down menu in the Bootstrap navigation bar
Add CSS style to control the position of submenu
Use JavaScript to control how submenu is triggered
Mobile adaptation and interaction optimization
Home Web Front-end Bootstrap Tutorial How to create a multi-level dropdown in Bootstrap navbar?

How to create a multi-level dropdown in Bootstrap navbar?

Jul 21, 2025 am 01:03 AM

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.

    How to create a multi-level dropdown in Bootstrap navbar?

    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.

    How to create a multi-level dropdown in Bootstrap navbar?

    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.

    How to create a multi-level dropdown in Bootstrap navbar?

    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.

    How to create a multi-level dropdown in Bootstrap navbar?

    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(&#39;.dropdown-submenu > a&#39;).forEach(link => {
      link.addEventListener(&#39;click&#39;, function (e) {
        e.stopPropagation();
        e.preventDefault();
        const submenu = this.nextElementSibling;
        submenu.classList.toggle(&#39;show&#39;);
      });
    });

    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!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
The Ultimate Guide to Creating Basic and Vertical Forms with Bootstrap The Ultimate Guide to Creating Basic and Vertical Forms with Bootstrap Jul 12, 2025 am 12:30 AM

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).

Bootstrap Forms : Common errors Bootstrap Forms : Common errors Jul 14, 2025 am 12:28 AM

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

Bootstrap Grid System: A Beginner's Guide Bootstrap Grid System: A Beginner's Guide Jul 09, 2025 am 01:04 AM

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

Bootstrap Forms: Best template for quick win Bootstrap Forms: Best template for quick win Jul 07, 2025 am 01:36 AM

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

Bootstrap Grid System: A Comprehensive Guide for Responsive Layouts Bootstrap Grid System: A Comprehensive Guide for Responsive Layouts Jul 12, 2025 am 01:23 AM

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

What You Need to Know About the Bootstrap Grid System What You Need to Know About the Bootstrap Grid System Jul 13, 2025 am 01:26 AM

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

How to Create a Navigation Bar in Bootstrap: A Comprehensive Guide How to Create a Navigation Bar in Bootstrap: A Comprehensive Guide Jul 08, 2025 am 12:29 AM

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.

How to install and use Bootstrap Icons library? How to install and use Bootstrap Icons library? Jul 27, 2025 am 01:25 AM

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.

See all articles