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

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

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

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


当前回答

要使用字体awesome使用css遵循以下步骤-

步骤1 -在CSS中添加FontAwesome字体

/*Font Awesome Fonts*/
@font-face {
    font-family: 'FontAwesome';
    //in url add your folder path of FontAwsome Fonts
    src: url('font-awesome/fontawesome-webfont.ttf') format('truetype');
}

使用下面的css在HTML的class元素上应用字体

.sorting_asc:after {
    content: "\f0de"; /* this is your text. You can also use UTF-8 character codes as I do here */
    font-family: FontAwesome;
    padding-left: 10px !important;
    vertical-align: middle;
}

最后,使用“sorting_asc”类在所需的HTML标签/元素上应用css。

其他回答

您可以尝试这个示例类。在这里找到图标内容: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";
    }

为此,你只需要通过:before或:after向所需元素添加content属性和font-family属性。

例如:我想在我的帖子中所有的a元素之后附加一个附件图标。所以,首先我需要搜索这样的图标是否存在于fontawesome。就像我在这里找到的那个箱子,也就是一个回形针。然后我右键单击那里的图标,在pseudo属性之前输入::,以获取它正在使用的内容标记,在我的例子中,我发现它是\f0c6。然后我会在css中这样使用它:

   .post a:after {
     font-family: FontAwesome,
     content: " \f0c6" /* I added a space before \ for better UI */
    }

要使用字体awesome使用css遵循以下步骤-

步骤1 -在CSS中添加FontAwesome字体

/*Font Awesome Fonts*/
@font-face {
    font-family: 'FontAwesome';
    //in url add your folder path of FontAwsome Fonts
    src: url('font-awesome/fontawesome-webfont.ttf') format('truetype');
}

使用下面的css在HTML的class元素上应用字体

.sorting_asc:after {
    content: "\f0de"; /* this is your text. You can also use UTF-8 character codes as I do here */
    font-family: FontAwesome;
    padding-left: 10px !important;
    vertical-align: middle;
}

最后,使用“sorting_asc”类在所需的HTML标签/元素上应用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.
}