Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create dropdown for <li> using bootstrap

I need to make drop down for one <li> element

<div class="head-nav">
                <span class="menu"></span>
                <ul class="cl-effect-15">
                    <li><a href="index.php" data-hover="HOME">HOME</a></li>
                    <li><a href="about.php">ABOUT</a></li>
                    <li><a href="contact.php" data-hover="CONTACT">CONTACT</a></li>
                    <li><a href="#" data-hover=" "> </a></li>
                    <li><a href="3" data-hover=" "> </a></li>
                    <li class="pull-right"><a href="#" data-hover="More option" tabindex="-1">More options</a>
                            <ul class="dropdown-menu">
                                <li><a href="#" data-hover=" ">one</a></li>
                                <li><a href="3" data-hover=" ">two</a></li>
                            </ul>
                    </li>
                    <li class="pull-right"><a href="logout.php" data-hover="logout">logout</a></li>
                    <div class="clearfix"> </div>
                </ul>
            </div>

but sub menu doesn't opens

using Bootstrap v3.1.1

NOTE : even adding after jquery also doesn't works

like image 327
romman Avatar asked Apr 24 '15 21:04

romman


People also ask

How do I add a drop-down menu to the Bootstrap 4 navigation bar?

Via data attributes. Add data-toggle="dropdown" to a link or button to toggle a dropdown.

How do I create a drop-down menu in a list in HTML?

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 can I create multiple dropdowns in Bootstrap?

No, it's not possible to have two dropdown menus inside the same div . They need to be separated since the code to toggle them looks for the first element in the parent div of the button/anchor. So if they are in the same parent div only the first will be toggled.


2 Answers

You have to use

<li class="dropdown">
   <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
    <ul class="dropdown-menu" role="menu">
       <li><a href="#">Action</a></li>
       <li><a href="#">Another action</a></li>
       <li><a href="#">Something else here</a></li>
       <li class="divider"></li>
       <li><a href="#">Separated link</a></li>
       <li class="divider"></li>
       <li><a href="#">One more separated link</a></li>
    </ul>
</li>

In your code, you miss class "dropdown" for li tag. For correct dropdown, you have to use this sintax:

<li class="dropdown">
 <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Item Dropdown <span class="caret"></span></a>
  <ul class="dropdown-menu" role="menu">
<!--- Put your menu-item here -->
  </ul>
</li>

You can refer the Bootstrap documentation here

The issue is causing by the missing of data-toggle="dropdown" in tag a

like image 183
MTS Avatar answered Oct 06 '22 05:10

MTS


Maybe you search this solution: JSBin

For example: You need add the content: attr(data-hover); on your before element. HTML:

<li class="HOVER"><a href="#" data-hover="More option" tabindex="-1">More options</a>

CSS:

.HOVER {
  border: solid 7px #000;
  padding: 20px;
  text-transform: uppercase;
  font-weight: bold;
  color: #fff;
  position: relative;
  width: 300px;
}

.HOVER:hover a:before {
  content: attr(data-hover);
  color: red;
  display: block;
  background: #000;
  position: absolute;
  top: 100%;
  left: -7px;
  right: -7px;
  padding: 15px; 
}
like image 23
Guarana Avatar answered Oct 06 '22 05:10

Guarana