Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdown on hover in Bootstrap 4.1

I have a website in which I have created dropdown using Bootstrap 4.1. At this moment, the dropdown is working on click.

The HTML code which I have used in order to create the dropdown are:

<div class="collapse navbar-collapse text-center" id="navbarResponsive">
    <ul class="navbar-nav ml-auto">

        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      main menu
    </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                <a class="dropdown-item" href="#">A</a>
                <a class="dropdown-item" href="#">B</a>
                <a class="dropdown-item" href="#">C</a>
                <a class="dropdown-item" href="#">D</a>
            </div>
        </li>

        <button type="submit" onclick="location.href='/M';" class="btn btn-default">R</button>

    </ul>
 </div>


Problem Statement:

I am wondering what changes I should make in the code above so that I can see the dropdown items (A,B,C,D) on hover.

like image 748
flash Avatar asked Jul 19 '18 03:07

flash


People also ask

How can create hover dropdown in Bootstrap?

Example Explained Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.

How do I create a drop-down menu in Bootstrap 4?

To open the dropdown menu, use a button or a link with a class of . dropdown-toggle and the data-toggle="dropdown" attribute. Add the . dropdown-menu class to a <div> element to actually build the dropdown menu.

How set dropdown position in Bootstrap?

By default, a dropdown menu is automatically positioned 100% from the top and along the left side of its parent. Add . dropdown-menu-right to a . dropdown-menu to right align the dropdown menu.

Why is my dropdown menu not working Bootstrap?

Solution : The dropdown should be toggled via data attributes or using javascript. In the above program, we have forgotten to add a data attribute so the dropdown is not working. So add data-bs-toggle="dropdown" to toggle the dropdown.


1 Answers

Are you looking for this?

.navbar-nav li:hover>.dropdown-menu {
  display: block;
}
<link href="https://getbootstrap.com/docs/4.1/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="navbar-collapse text-center" id="navbarResponsive">
  <ul class="navbar-nav ml-auto">

    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      main menu
    </a>
      <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
        <a class="dropdown-item" href="#">A</a>
        <a class="dropdown-item" href="#">B</a>
        <a class="dropdown-item" href="#">C</a>
        <a class="dropdown-item" href="#">D</a>
      </div>
    </li>

    <button type="submit" onclick="location.href='/M';" class="btn btn-default">R</button>

  </ul>
</div>
like image 181
Tushar Avatar answered Oct 06 '22 00:10

Tushar