How to make Twitter Bootstrap menu dropdown on hover rather than click

后端 未结 30 2614
小鲜肉
小鲜肉 2020-11-22 02:08

I\'d like to have my Bootstrap menu automatically drop down on hover, rather than having to click the menu title. I\'d also like to lose the little arrows next to the menu t

30条回答
  •  不知归路
    2020-11-22 02:22

    To get the menu to automatically drop on hover then this can achieved using basic CSS. You need to work out the selector to the hidden menu option and then set it to display as block when the appropriate li tag is hovered over. Taking the example from the twitter bootstrap page, the selector would be as follows:

    ul.nav li.dropdown:hover > ul.dropdown-menu {
        display: block;    
    }
    

    However, if you are using Bootstrap's responsive features, you will not want this functionality on a collapsed navbar (on smaller screens). To avoid this, wrap the code above in a media query:

    @media (min-width: 979px) {
      ul.nav li.dropdown:hover > ul.dropdown-menu {
        display: block;
      }
    }
    

    To hide the arrow (caret) this is done in different ways depending on whether you are using Twitter Bootstrap version 2 and lower or version 3:

    Bootstrap 3

    To remove the caret in version 3 you just need to remove the HTML from the .dropdown-toggle anchor element:

    
        Dropdown
            <-- remove this line
    
    

    Bootstrap 2 & lower

    To remove the caret in version 2 you need a little more insight into CSS and I suggest looking at how the :after pseudo element works in more detail. To get you started on your way to understanding, to target and remove the arrows in the twitter bootstrap example, you would use the following CSS selector and code:

    a.menu:after, .dropdown-toggle:after {
        content: none;
    }
    

    It will work in your favour if you look further into how these work and not just use the answers that I have given you.

    Thanks to @CocaAkat for pointing out that we were missing the ">" child combinator to prevent sub menus being shown on the parent hover

提交回复
热议问题