如何更改HTML列表中的项目符号并使用Bootstrap 3附带的字形?
那样:
<ul>
<li>...</li>
<li>...</li>
</ul>
显示为:
[icon] Facere possimus, omnis voluptas assumenda est, numquam eius modi
omnis dolor repellendus. Non numquam eius modi numam dolor omnis
tempora incidunt ut labore.
[icon] Facere possimus, omnis voluptas assumenda est, numquam eius modi
omnis dolor repellendus. Non numquam eius modi numam dolor omnis
tempora incidunt ut labore.
我希望 不 必须注入额外的HTML,例如...
<ul>
<li><i class="glyphicon glyphicon-chevron-right"></i> ...</li>
<li><i class="glyphicon glyphicon-chevron-right"></i> ...</li>
</ul>
答案 0 :(得分:106)
使用一点CSS并不是太困难,并且 比使用子弹的图像更好,因为你可以对其进行缩放和着色,并且它将在所有分辨率下都保持清晰。 / p>
打开Bootstrap docs并检查您要使用的字符,找到该字形的字符代码。
在以下CSS中使用该字符代码
li {
display: block;
}
li:before {
/*Using a Bootstrap glyphicon as the bullet point*/
content: "\e080";
font-family: 'Glyphicons Halflings';
font-size: 9px;
float: left;
margin-top: 4px;
margin-left: -17px;
color: #CCCCCC;
}
您可能希望调整颜色和边距以适合您的字体大小和品味。
答案 1 :(得分:31)
如果有人来这里寻找使用Font Awesome Icons(像我一样)这样做,请查看:https://fontawesome.com/how-to-use/on-the-web/styling/icons-in-a-list
<ul class="fa-ul">
<li><i class="fa-li fa fa-check-square"></i>List icons</li>
<li><i class="fa-li fa fa-check-square"></i>can be used</li>
<li><i class="fa-li fa fa-spinner fa-spin"></i>as bullets</li>
<li><i class="fa-li fa fa-square"></i>in lists</li>
</ul>
fa-ul
和fa-li
类可以轻松替换无序列表中的默认项目符号。
答案 2 :(得分:7)
我使用基于@SimonEast答案的简化版本(仅使用位置相对):
li:before {
content: "\e080";
font-family: 'Glyphicons Halflings';
font-size: 9px;
position: relative;
margin-right: 10px;
top: 3px;
color: #ccc;
}
答案 3 :(得分:1)
如果你想碰巧使用LESS,可以这样实现:
li {
.glyphicon-ok();
&:before {
.glyphicon();
margin-left:-25px;
float:left;
}
}
答案 4 :(得分:0)
如果您希望每个列表项都有一个不同的图标,则建议您在HTML中添加图标,而不要使用伪元素来降低CSS的承受力。可以非常简单地完成以下操作:
<ul>
<li><span><i class="mdi mdi-lightbulb-outline"></i></span>An electric light with a wire filament heated to such a high temperature that it glows with visible light</li>
<li><span><i class="mdi mdi-clipboard-check-outline"></i></span>A thin, rigid board with a clip at the top for holding paper in place.</li>
<li><span><i class="mdi mdi-finance"></i></span>A graphical representation of data, in which the data is represented by symbols, such as bars in a bar chart, lines in a line chart, or slices in a pie chart.</li>
<li><span><i class="mdi mdi-server"></i></span>A system that responds to requests across a computer network worldwide to provide, or help to provide, a network or data service.</li>
</ul>
-
ul {
list-style-type: none;
margin-left: 2.5em;
padding-left: 0;
}
ul>li {
position: relative;
}
span {
left: -2em;
position: absolute;
text-align: center;
width: 2em;
line-height: inherit;
}
在这种情况下,我使用了Material Design Icons
答案 5 :(得分:0)
使用Font Awesome 5,标记比以前的答案要复杂一些。对于the FA documentation,标记应为:
<ul class="fa-ul">
<li><span class="fa-li" ><i class="fas fa-check-square"></i></span>List icons can</li>
<li><span class="fa-li"><i class="fas fa-check-square"></i></span>be used to</li>
<li><span class="fa-li"><i class="fas fa-spinner fa-pulse"></i></span>replace bullets</li>
<li><span class="fa-li"><i class="far fa-square"></i></span>in lists</li>
</ul>