我有一些CSS看起来像这样:

#content h2 {
   background: url(../images/tContent.jpg) no-repeat 0 6px;
}

我想用字体很棒的图标替换图像。

我不认为在CSS中使用图标作为背景图像。这是可能的,假设字体Awesome样式表/字体在我的CSS之前加载?


当前回答

#content h2:before {
    content: "\f055";
    font-family: FontAwesome;
    left:0;
    position:absolute;
    top:0;
}

示例链接: https://codepen.io/bungeedesign/pen/XqeLQg

获取图标代码: https://fontawesome.com/cheatsheet?from=io

其他回答

实际上,即使是字体很棒的CSS也有类似的策略来设置图标样式。如果您想快速获取图标代码,请检查非缩小的font-awesome.css文件,它们是....每个字体都很纯净。

或者,如果使用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.
}

巩固以上所有内容,下面是工作良好的最终类

   .faArrowIcon {
        position:relative;
    }

    .faArrowIcon:before {
        font-family: FontAwesome;
        top:0;
        left:-5px;
        padding-right:10px;
        content: "\f0a9"; 
    }

您可以尝试这个示例类。在这里找到图标内容:http://astronautweb.co/snippet/font-awesome/

  #content h2:before {
    display: inline-block;
    font: normal normal normal 14px/1 FontAwesome;
    font-size: inherit;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transform: translate(0, 0);
    content: "\f007";
    }

似乎给出的答案并没有给出一个真正的背景,因为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;}