331

I have some CSS that looks like this:

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

I would like to replace the image with an icon from Font Awesome.

I do not see anyway to use the icon in CSS as a background image. Is this possible to do assuming the Font Awesome stylesheets/fonts are loaded before my CSS?

6
  • Glyphs from fonts cannot be used as backgrounds. SVGs, however, can
    – cimmanon
    Feb 6, 2013 at 18:57
  • @cimmanon would you mind explaining how that could apply to font-awesome
    – BonsaiOak
    Sep 29, 2014 at 12:30
  • @BonsaiOak What's to explain that isn't already explained by the selected answer? If you have a question that isn't answered by this (or any other question on SO), then ask a new question.
    – cimmanon
    Sep 29, 2014 at 13:25
  • @cimmanon sorry for the lack of clarity. You seemed to imply that it was possible to use an icon from the font-awesome.svg font file as the background. However, you never said that explicitly so I'm not sure.
    – BonsaiOak
    Sep 29, 2014 at 14:42
  • 1
    @BonsaiOak Yes, you can use an SVG as a background image, the same way you would use any other image. However, I have not actually looked at the FontAwesome SVG to verify that it can be used in this way.
    – cimmanon
    Sep 29, 2014 at 14:48

12 Answers 12

535

You can't use text as a background image, but you can use the :before or :after pseudo classes to place a text character where you want it, without having to add all kinds of messy extra mark-up.

Be sure to set position:relative on your actual text wrapper for the positioning to work.

.mytextwithicon {
    position:relative;
}    
.mytextwithicon:before {
    content: "\25AE";  /* this is your text. You can also use UTF-8 character codes as I do here */
    font-family: FontAwesome;
    left:-5px;
    position:absolute;
    top:0;
 }

EDIT:

Font Awesome v5 uses other font names than older versions:

  • For FontAwesome v5, Free Version, use: font-family: "Font Awesome 5 Free"
  • For FontAwesome v5, Pro Version, use: font-family: "Font Awesome 5 Pro"

Note that you should set the same font-weight property, too (seems to be 900).

Another way to find the font name is to right click on a sample font awesome icon on your page and get the font name (same way the utf-8 icon code can be found, but note that you can find it out on :before).

16
  • 68
    The text-equivalent of each icon is there fortawesome.github.io/Font-Awesome/cheatsheet
    – Matthieu
    Feb 19, 2014 at 9:36
  • 62
    You'll have to remove &#x from the beginning and ; from the end. The font-family name is FontAwesome
    – Matthieu
    Feb 19, 2014 at 9:46
  • 5
    @David, it's just text, use font-size. Oct 16, 2015 at 23:05
  • 14
    for FontAwesome v5 you need to use .backspace:before { font-family: "Font Awesome 5 Free"; font-weight: 900; content: "\f55a"; }
    – Digvijayad
    Aug 17, 2018 at 16:37
  • 7
    Just a note that this doesn't appear to work with Input elements (like a submit button) since they don't seem to support :before or :after.
    – Mir
    Aug 28, 2018 at 23:25
25

Actually even font-awesome CSS has a similar strategy for setting their icon styles. If you want to get a quick hold of the icon code, check the non-minified font-awesome.css file and there they are....each font in its purity.

Font-Awesome CSS File screenshot

1
  • And you just can use the classes for any element, no need to transfer any code. Just the font for the element has to be different than for the ::before.
    – David
    Jan 23 at 0:10
23

Consolidating everything above, the following is the final class which works well

   .faArrowIcon {
        position:relative;
    }

    .faArrowIcon:before {
        font-family: FontAwesome;
        top:0;
        left:-5px;
        padding-right:10px;
        content: "\f0a9"; 
    }
1
  • best answer, as you can just remove a previous <i> tag from before the text you want with the icon and replace with this. And the same space is given. Nov 12, 2015 at 6:38
12

To use font awesome using css follow below steps -

step 1 - Add Fonts of FontAwesome in CSS

/*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');
}

Step - 2 Use below css to apply font on class element of HTML

.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;
}

And finally, use "sorting_asc" class to apply the css on desired HTML tag/element.

8

You can try this example class. and find icon content here: 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";
    }
6

I am bit late to the party. Just like to suggest another way.

  button.calendar::before {
    content: '\f073';
    font-family: 'Font Awesome 5 Free';
    left: -4px;
    bottom: 4px;
    position: relative;
  }

position, left and bottom are used to align the icon.

Sometimes adding font-weight: 600 or above also helps.

3

No need to embed content into the CSS. You can put the badge content inside the fa element, then adjust the badge 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>
2
  • 8
    "No need to embed content into the CSS". Well, in some situations it's needed. Jul 13, 2015 at 11:53
  • 2
    If you don't care about progressive enhancement that's fine, but you should really keep your styling inside of a css file whenever possible.
    – Adam Carr
    Nov 29, 2016 at 20:01
3
#content h2:before {
    content: "\f055";
    font-family: FontAwesome;
    left:0;
    position:absolute;
    top:0;
}

Example Link: https://codepen.io/bungeedesign/pen/XqeLQg

Get Icon code from: https://fontawesome.com/cheatsheet?from=io

2

Alternatively, if using Sass, one can "extend" FA icons to display them:

.mytextwithicon:before {
  @extend .fas, .fa-angle-double-right;

  @extend .mr-2; // using bootstrap to add a small gap
                 // between the icon and the text.
}
2

It seems that the given answers don't give a real background as the fontawesome is rendered outside the bloc you want the background in. Here is my solution to have a "real" background effect :

enter image description here

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;}
0

For this you just need to add content attribute and font-family attribute to the required element via :before or :after wherever applicable.

For example: I wanted to attach an attachment icon after all the a element inside my post. So, first I need to search if such icon exists in fontawesome. Like in the case I found it here, i.e. fa fa-paperclip. Then I would right click the icon there, and go the ::before pseudo property to fetch out the content tag it is using, which in my case I found to be \f0c6. Then I would use that in my css like this:

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

This seems to be the simplest solution :-)

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.