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:
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!
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.
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.
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"/>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With