332

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?

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
L84
  • 45,514
  • 58
  • 177
  • 257
  • Glyphs from fonts cannot be used as backgrounds. SVGs, however, *can* – cimmanon Feb 06 '13 at 18:57
  • @cimmanon would you mind explaining how that could apply to font-awesome – BonsaiOak Sep 29 '14 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 '14 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 '14 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 '14 at 14:48
  • https://ohmycheatsheet.com/fontawesome/ might be helpful – Shiva Mar 25 '20 at 07:19

12 Answers12

538

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).

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • 1
    This is awesome! I'm currently switching countless icons over to font awesome in a large enterprise application. This is making it way easier! – Anthony Oct 21 '13 at 16:36
  • 68
    The text-equivalent of each icon is there http://fortawesome.github.io/Font-Awesome/cheatsheet/ – Matthieu Feb 19 '14 at 09:36
  • 62
    You'll have to remove from the beginning and ; from the end. The font-family name is FontAwesome – Matthieu Feb 19 '14 at 09:46
  • 1
    ok why does text gets converted to icon when inside pesudeo content but not in real element like p or div. say if i put \f010 in content i get icon, and div i get \f010 as is... How come? – Muhammad Umer Mar 18 '15 at 23:05
  • Now, how do we make it scale? – David Sep 30 '15 at 08:41
  • 5
    @David, it's just text, use ``font-size``. – jonathancardoso Oct 16 '15 at 23:05
  • Could anyone advise how this might work with FortAwesome icon packs? FortAwesome provides you with JS to fetch the icons paid for in your pack, however I'm guessing the CSS doesn't know about these. So when I try to use one of my FortAwesome icons it just renders as an empty square. – Richard Fortune Mar 22 '16 at 07:04
  • 1
    Just want to add that the latest version of Font Awesome have this in the docs, please refer to this page for documentation: https://fontawesome.com/how-to-use/web-fonts-with-css – Björn Hjorth May 13 '18 at 07:22
  • 15
    for FontAwesome v5 you need to use ```.backspace:before { font-family: "Font Awesome 5 Free"; font-weight: 900; content: "\f55a"; }``` – Digvijayad Aug 17 '18 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 '18 at 23:25
  • 1
    And if you're using Font Awesome 5 Free, then `font-weight: 900` will show the filled version of the icon (the Brands), and `font-weight: 400` will show the regular version. For instance, [this](https://fontawesome.com/icons/circle?style=solid) and [this](https://fontawesome.com/icons/circle?style=regular) has the same 'icon-code'. So to differ between the two, then the font-weight is used. – Zeth Jan 10 '19 at 19:12
  • How do you determine what exactly the font family string should be based on the icon I want to load? – xaunlopez Apr 30 '19 at 04:24
  • 1
    Use the cheat sheet: https://fontawesome.com/cheatsheet It has all the hex codes. – Diodeus - James MacFarlane Apr 30 '19 at 13:36
  • for me, it is working when i put `font-weight: 900;` – Syamsoul Azrien Nov 13 '19 at 07:38
  • Just add `.mytextwithicon *{position:relative;}` to avoid the font in background to come upon the html elements in .mytextwithicon. – lsmpascal Apr 28 '21 at 19:38
  • This answer brought me on the right way! Nevertheless, assigning "content" by yourself is pretty dumb as fontawesome has all the required classes. You just have to overwrite the font for the main element as fa-css is assigning it's font to it and not only to the `::before`. Like this you can use all defined icons without parsing or looking up any code. – David Jan 23 '23 at 00:04
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

Bruno Krebs
  • 3,059
  • 1
  • 24
  • 36
Aakash
  • 21,375
  • 7
  • 100
  • 81
  • 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 '23 at 00: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"; 
    }
Lakshman Pilaka
  • 1,803
  • 2
  • 24
  • 48
  • best answer, as you can just remove a previous tag from before the text you want with the icon and replace with this. And the same space is given. – andygoestohollywood Nov 12 '15 at 06: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.

Ravindra Vairagi
  • 1,055
  • 15
  • 22
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";
    }
Shiv Singh
  • 6,939
  • 3
  • 40
  • 50
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.

Pankaj Salunkhe
  • 235
  • 1
  • 5
  • 8
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>
George Hix
  • 63
  • 3
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

Pervez
  • 516
  • 6
  • 10
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.
}
Hiura
  • 3,500
  • 2
  • 20
  • 39
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;}
lsmpascal
  • 762
  • 8
  • 24
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 */
    }
Animesh Singh
  • 8,382
  • 1
  • 17
  • 20
0

This seems to be the simplest solution :-)

#content h2:before {
   font-family: FontAwesome;
   content: "\f055";
   position:absolute;
   left:0;
   top:0;
}
Arun s
  • 869
  • 9
  • 19