如何在Bootstrap下拉菜单中添加图标

10

我有这段代码

        <div class="btn-group" dropdown>
            <button type="button" class="btn btn-danger">Action</button>
            <button type="button" class="btn btn-danger dropdown-toggle" dropdown-toggle>
                <span class="caret"></span>
                <span class="sr-only">Split button!</span>
            </button>
            <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>
            </ul>
        </div>

这看起来像是

enter image description here

我想添加类似齿轮的图标,而不是文本,像这样:

enter image description here

我尝试了这个:

<button type="button" class="glyphicon glyphicon-cog"></button>

但并没有起作用。

3个回答

12

对于一个按钮,图标需要作为按钮内容放在按钮内部,因此应该使用以下代码:<button type="button" class="btn btn-default"><i class="glyphicon glyphicon-cog"></i></button>

<button type="button" class="btn">
    <span class="glyphicon glyphicon-cog"></span>
</button>

编辑:要在Bootstrap中添加小三角符号,您可以使用<span class="caret"></span>

因此,获取具有齿轮和下拉菜单小三角符号的按钮的最终结果为:

<button type="button" class="btn">
    <span class="glyphicon glyphicon-cog"></span><span class="caret"></span>
</button>
当我将那段代码粘贴到Bootstrap的主页时,我得到了这个:cog button

1
如果您正在使用Bootstrap 4(其中已删除glyphicon字体),请查看此线程:https://dev59.com/klwY5IYBdhLWcg3wcXeE - Greg Holst

1
<div class="btn-group" dropdown>
    <button type="button" class="btn btn-danger"><span class="glyphicon glyphicon-cog"></span></button>
    <button type="button" class="btn btn-danger dropdown-toggle" dropdown-toggle>
        <span class="caret"></span>
        <span class="sr-only">Split button!</span>
    </button>
    <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>
    </ul>
</div>

你可以使用这段代码。

0

只需添加这个 span 标签

<span class="glyphicon glyphicon-cog" aria-hidden="true"></span>

请查看此演示


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接