Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open dropdown on hover in bootstrap 4

Right now my navigation drop down can open on click.

I want it to open upon hover. How do I do this?

like image 288
Owais Idrees Avatar asked Oct 06 '17 11:10

Owais Idrees


People also ask

How do I open dropdown?

To open the dropdown menu, use a button or a link with a class of . dropdown-toggle and the data-toggle="dropdown" attribute. The . caret class creates a caret arrow icon (), which indicates that the button is a dropdown.

How can you use dropdown plugin in web page in Bootstrap?

Using Dropdown plugin you can add dropdown menus to any components like navbars, tabs, pills and buttons. If you want to include this plugin functionality individually, then you will need dropdown. js. Else, as mentioned in the chapter Bootstrap Plugins Overview, you can include bootstrap.

How do I move a drop down to left in Bootstrap?

To override it, use . dropdown-menu-left.

What are Bootstrap drop down menus?

Overview. Dropdowns are toggleable, contextual overlays for displaying lists of links and more. They're made interactive with the included Bootstrap dropdown JavaScript plugin. They're toggled by clicking, not by hovering; this is an intentional design decision.


2 Answers

simply add following css

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

fiddle

.dropdown:hover>.dropdown-menu {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
   <nav class="navbar navbar-toggleable-md navbar-light bg-faded">
 
<ul class="navbar-nav">

  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Dropdown link
    </a>
    <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <a class="dropdown-item" href="#">Something else here</a>
    </div>
  </li>
</ul>
</nav>
like image 108
Znaneswar Avatar answered Oct 03 '22 22:10

Znaneswar


The css from Znaneswar works great but I would add this line as well.

.dropdown-menu {
  margin: -0.125rem 0 0;
}

The dropdown is spaced 0.125rem away from the element that spawns the dropdown. So you'll have a hard time navigating from the link to the dropdown without it disappearing when you mouse over that gap.

And if you want the dropdown link to actually be a link as well, just remove this attribute from the a tag

data-toggle="dropdown"
like image 26
Strangegroove Avatar answered Oct 04 '22 00:10

Strangegroove