Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding icons to a Bootstrap dropdown

I want to add some icons to links in a Bootstrap dropdown, using code like this:

<ul>     <li class="dropdown">         <a href="#" class="dropdown-toggle" data-toggle="dropdown">Some Dropdown<b class="caret"></b></a>         <ul class="dropdown-menu">             <li><i class="icon-arrow-up"></i><a href="#">Up</a></li>             <li><i class="icon-arrow-down"></i><a href="#">Down</a></li>             <li><i class="icon-arrow-left"></i><a href="#">Left</a></li>             <li><i class="icon-arrow-right"></i><a href="#">Right</a></li>         </ul>     </li> </ul> 

However, the icons are out of place:

dropdown issue

I tried using the solution in this answer, but it doesn't seem to work. Can somebody provide a solution and explain why/how it works?

Demo

Thanks!

like image 863
wrongusername Avatar asked Apr 11 '13 04:04

wrongusername


People also ask

How do I style a dropdown in Bootstrap?

Basic 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 do I add icons to Bootstrap project?

Include the Bootstrap Icons font stylesheet in the <head> of your website. Or, use @import to include the stylesheet that way. /* Option 2: Import via CSS */ @import url("https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css"); Add HTML snippets to include Bootstrap Icons where desired.


2 Answers

Put the icons inside the anchor tags:

<ul>      <li class="dropdown">          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Some Dropdown<b class="caret"></b></a>          <ul class="dropdown-menu">              <li><a href="#"><i class="icon-arrow-up"></i> Up</a></li>              <li><a href="#"><i class="icon-arrow-down"></i> Down</a></li>              <li><a href="#"><i class="icon-arrow-left"></i> Left</a></li>              <li><a href="#"><i class="icon-arrow-right"></i> Right</a></li>          </ul>      </li>  </ul>      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>  <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet"/>
like image 87
N1ck Avatar answered Sep 17 '22 23:09

N1ck


Used to Position

Define your .dropdown-menu li position relative and i define position absolute

as like this

.dropdown-menu a {     white-space:normal; } .dropdown-menu > li{position:relative;}  .dropdown-menu > li > i{position:absolute;left:0;top:3px;} 

Live Demo

like image 31
Rohit Azad Malik Avatar answered Sep 16 '22 23:09

Rohit Azad Malik