亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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)

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 Grid System vs Flexbox: what is better? Bootstrap Grid System vs Flexbox: what is better? Jul 06, 2025 am 12:42 AM

BootstrapGridSystemisbetterforquick,simpleprojects;Flexboxisidealforcustomizationandcontrol.1)Bootstrapiseasiertouseandfastertoimplement.2)Flexboxoffersmorecustomizationandflexibility.3)Flexboxcanbemoreperformant,butthedifferenceisusuallyminor.4)Boot

Bootstrap Grid System and accessibility Bootstrap Grid System and accessibility Jul 05, 2025 am 01:31 AM

TheBootstrapGridSystemcanbeoptimizedforbetteraccessibility.1)UsesemanticHTMLtagslikeandinsteadofgenericelements.2)ImplementARIAattributestoenhancescreenreaderfunctionality.3)ManagefocusorderlogicallywithBootstrap'sorderclasses.4)Useutilityclassesforp

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 Navbar : How to use dropdown menus Bootstrap Navbar : How to use dropdown menus Jul 04, 2025 am 01:36 AM

The dropdown menu of BootstrapNavbar can be implemented through the following steps: 1. Use the dropdown class and the data-bs-toggle="dropdown" attribute. 2. Ensure responsive design. 3. Optimize performance. 4. Improve accessibility. 5. Custom style. This helps create a user-friendly navigation system.

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

Bootstrap Navbar: What about Accessibility? Bootstrap Navbar: What about Accessibility? Jul 03, 2025 am 12:57 AM

BootstrapNavbarsaremoderatelyaccessiblebutrequireadditionaleffortforfullinclusivity.1)UseARIAattributeslikearia-labelonelements.2)EnsurekeyboardnavigationwithcustomJavaScriptforinteractiveelements.3)AdjustcolorcontrasttomeetWCAGstandards.4)Optimizepe

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

See all articles