我有一些CSS看起来像这样:
#content h2 {
background: url(../images/tContent.jpg) no-repeat 0 6px;
}
我想用字体很棒的图标替换图像。
我不认为在CSS中使用图标作为背景图像。这是可能的,假设字体Awesome样式表/字体在我的CSS之前加载?
我有一些CSS看起来像这样:
#content h2 {
background: url(../images/tContent.jpg) no-repeat 0 6px;
}
我想用字体很棒的图标替换图像。
我不认为在CSS中使用图标作为背景图像。这是可能的,假设字体Awesome样式表/字体在我的CSS之前加载?
当前回答
不需要在CSS中嵌入内容。你可以把徽章内容放在fa元素中,然后调整徽章的css。http://jsfiddle.net/vmjwayrk/2/
<i class="fa fa-envelope fa-5x" style="position:relative;color:grey;">
<span style="
background-color: navy;
border-radius: 50%;
font-size: .25em;
display:block;
position:absolute;
text-align: center;
line-height: 2em;
top: -.5em;
right: -.5em;
width: 2em;
height: 2em;
border:solid 4px #fff;
box-shadow:0px 0px 1px #000;
color: #fff;
">17</span>
</i>
其他回答
或者,如果使用Sass,可以“扩展”FA图标来显示它们:
.mytextwithicon:before {
@extend .fas, .fa-angle-double-right;
@extend .mr-2; // using bootstrap to add a small gap
// between the icon and the text.
}
实际上,即使是字体很棒的CSS也有类似的策略来设置图标样式。如果您想快速获取图标代码,请检查非缩小的font-awesome.css文件,它们是....每个字体都很纯净。
我参加聚会有点晚了。只是想建议另一种方式。
button.calendar::before {
content: '\f073';
font-family: 'Font Awesome 5 Free';
left: -4px;
bottom: 4px;
position: relative;
}
位置,左侧和底部用于对齐图标。
有时添加字体重量:600或以上也有帮助。
似乎给出的答案并没有给出一个真正的背景,因为fontawesome是在你想要的背景区域之外渲染的。 下面是我制作“真实”背景效果的方法:
html:
<div id="bloc" class="bg_ico_outer" style="">
<i class="fa fa-bookmark-o bg_ico"></i>
<div class='bloc_inner'>
<h2>test fontawesome as background</h2>
</div>
</div>
css:
.bg_ico {
position: absolute;
top: 0px;
right: -10px;
font-size: 17em;
color: green;
transform: rotate(25deg);
}
.bg_ico_outer{position: relative; overflow: hidden;}
#bloc{
height: 200px;
width:200px;
background: blue;
margin:50px auto;
}
.bloc_inner{
position: absolute;
}
h2{color: white;}
巩固以上所有内容,下面是工作良好的最终类
.faArrowIcon {
position:relative;
}
.faArrowIcon:before {
font-family: FontAwesome;
top:0;
left:-5px;
padding-right:10px;
content: "\f0a9";
}