将图标添加到Bootstrap下拉列表

时间:2013-04-11 04:03:49

标签: html css twitter-bootstrap drop-down-menu icons

我想在Bootstrap下拉列表中为链接添加一些图标,使用如下代码:

<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>

但是,图标不合适:

dropdown issue

我尝试使用this answer中的解决方案,但它似乎不起作用。有人可以提供解决方案并解释其原因/工作原理吗?

Demo

谢谢!

4 个答案:

答案 0 :(得分:62)

将图标放在锚标记内:

<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"/>

答案 1 :(得分:12)

用于排名

定义您的 .dropdown-menu li position relative 我定义position absolute

就像这样

.dropdown-menu a {
    white-space:normal;
}
.dropdown-menu > li{position:relative;}

.dropdown-menu > li > i{position:absolute;left:0;top:3px;}

<强> Live Demo

答案 2 :(得分:5)

将图标放在Bootstrap v3的锚标记内(带有http://www.FontAwesome.com的图标):

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

示例:http://jsfiddle.net/9ce3P/

答案 3 :(得分:0)

<ul class="dropdown-menu" role="menu">
    <li role="presentation">
        <a role="menuitem" tabindex="-1" href="javascript:void(0)">
            <span class="pull-right">Edit</span>
            <span class="glyphicon glyphicon-edit pull-left"></span>
            <span class="clearfix"></span>
        </a>
    </li>
    <li role="presentation">
        <a role="menuitem" tabindex="-1" href="javascript:void(0)">
            <span class="pull-right">Delete</span>
            <span class="glyphicon glyphicon-remove pull-left"></span>
            <span class="clearfix"></span>
        </a>
    </li>
</ul>