327

I want to use a Font Awesome icon as CSS content, i.e.,

a:before {
    content: "<i class='fa...'>...</i>";
}

I know I cannot use HTML code in content, so is it only images left?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
sdvnksv
  • 9,350
  • 18
  • 56
  • 108

9 Answers9

754

Update for FontAwesome 5 Thanks to Aurelien

You need to change the font-family to Font Awesome 5 Brands OR Font Awesome 5 Free, based on the type of icon you are trying to render. Also, do not forget to declare font-weight: 900;

a:before {
   font-family: "Font Awesome 5 Free";
   content: "\f095";
   display: inline-block;
   padding-right: 3px;
   vertical-align: middle;
   font-weight: 900;
}

Demo

You can read the rest of the answer below to understand how it works and to know some workarounds for spacing between icon and the text.


FontAwesome 4 and below

That's the wrong way to use it. Open the font awesome style sheet, go to the class of the font you want to use say fa-phone, copy the content property under that class with the entity, and use it like:

a:before {
    font-family: FontAwesome;
    content: "\f095";
}

Demo

Just make sure that if you are looking to target a specific a tag, then consider using a class instead to make it more specific like:

a.class_name:before {
    font-family: FontAwesome;
    content: "\f095";
}

Using the way above will stick the icon with the remaining text of yours, so if you want to have a bit of space between the two of them, make it display: inline-block; and use some padding-right:

a:before {
    font-family: FontAwesome;
    content: "\f095";
    display: inline-block;
    padding-right: 3px;
    vertical-align: middle;
}

Extending this answer further, since many might be having a requirement to change an icon on hover, so for that, we can write a separate selector and rules for :hover action:

a:hover:before {
    content: "\f099"; /* Code of the icon you want to change on hover */
}

Demo

Now in the above example, icon nudges because of the different size and you surely don't want that, so you can set a fixed width on the base declaration like

a:before {
    /* Other properties here, look in the above code snippets */
    width: 12px; /* add some desired width here to prevent nudge */
}

Demo

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • 78
    See here for [the official list of Unicode character codes in Font Awesome](http://fortawesome.github.io/Font-Awesome/cheatsheet/). – Boaz Dec 26 '13 at 09:40
  • With `font-family: FontAwesome;` it changes the font for anchor tags also. How can we handle that? – Shubh Aug 18 '15 at 16:17
  • 1
    @Shubh read the code carefully, `font-family` is suppose to be called on `:before` pseudo and not anchor tag – Mr. Alien Aug 18 '15 at 16:55
  • Just thought I'd add here that you can find the unicode spec for a particular icon without having to dig into the source. Simply go to the list of icons on the font awesome website and click the icon you want. You'll see the Unicode value on the page just beneath the samples of the icon: http://fortawesome.github.io/Font-Awesome/icon/pencil-square-o/ – Allen Underwood Apr 24 '16 at 16:06
  • This CSS Unicode list for FontAwesome is better: http://astronautweb.co/snippet/font-awesome/ because it shows the correct `content` codes for direct copying (i. e. a no-brainer). PS: I still wonder why the FontAwesome guys do not put the content codes onto each icon page?! – Avatar Jan 13 '18 at 14:39
  • Small correction: <>... that's only if you want it to look fat/bold. I've got "font-weight:500;" and I'm very happy with it. I also notice this gives me the style, not the style (both same Unicode codepoint according to their website). I'd rather have but I'm not sure how to access. – user3445853 Feb 05 '19 at 17:04
  • 1
    Here is Font-awesome docs page that shows you how to use it going forward. https://fontawesome.com/how-to-use/on-the-web/advanced/css-pseudo-elements – whisk Mar 13 '19 at 17:46
  • @user3445853 why 500? According to the Demo, I can't see the icon with 500, only with 501 and above, I don't get it. – pmiranda May 22 '19 at 13:22
  • Why do we need `font-weight: 900` ? If I ommit font-weight then it happens to render nothing but a empty square – Black Dec 02 '19 at 10:59
  • updated link for fontawesome 5 official list https://fontawesome.com/cheatsheet – sab Dec 24 '19 at 17:47
  • A couple of things to note here: 1. If you're seeing a square when changing `font-weight`, it means that the icon you're trying to render isn't free for that particular `font-weight`. 2. I've found that removing the `vertical-align: middle` looks better (at least for the FA 5 version and at least in my case). – Eduard Luca Jun 23 '20 at 19:15
26

Another solution without you having to manually mess around with the Unicode characters can be found in Making Font Awesome awesome - Using icons without i-tags (disclaimer: I wrote this article).

In a nutshell, you can create a new class like this:

.icon::before {
    display: inline-block;
    margin-right: .5em;
    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);
}

And then use it with any icon, for example:

<a class="icon fa-car" href="#">This is a link</a>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dustinmoris
  • 3,195
  • 3
  • 25
  • 33
24

If you have access to SCSS files from font-awesome, you can use this simple solution:

.a:after {
    // Import mixin from font-awesome/scss/mixins.scss
    @include fa-icon();

    // Use icon variable from font-awesome/scss/variables.scss
    content: $fa-var-exclamation-triangle;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ersefuril
  • 809
  • 9
  • 16
13

You should have font-weight set to 900 for Font Awesome 5 Free font-family to work.

This is the working one:

.css-selector::before {
   font-family: 'Font Awesome 5 Free';
   content: "\f101";
   font-weight: 900;
}
9
a: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
4

You can use unicode for this in CSS. If you are using font awesome 5, this is the syntax;

.login::before {
    font-family: "Font Awesome 5 Free"; 
    font-weight: 900; 
    content: "\f007";  
}

You can see their documentation here.

Mahib
  • 3,977
  • 5
  • 53
  • 62
1

As it says at FontAwesome website FontAwesome =>

HTML:

<span class="icon login"></span> Login</li>

CSS:

 .icon::before {
    display: inline-block;
    font-style: normal;
    font-variant: normal;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
  }

    .login::before {
        font-family: "Font Awesome 5 Free";
        font-weight: 900;
        content: "\f007";
   }

In .login::before -> edit content:''; to suit your unicode.

Prusakov
  • 327
  • 2
  • 10
1

Update for Font Awesome 5 using SCSS

.icon {
  @extend %fa-icon;
  @extend .fas;

  &:before {
    content: fa-content($fa-var-user);
  }
}
Friendly Code
  • 1,585
  • 1
  • 25
  • 41
0

Here's my webpack 4 + font awesome 5 solution:

webpack plugin:

new CopyWebpackPlugin([
    { from: 'node_modules/font-awesome/fonts', to: 'font-awesome' }
  ]),

global css style:

@font-face {
    font-family: 'FontAwesome';
    src: url('/font-awesome/fontawesome-webfont.eot');
    src: url('/font-awesome/fontawesome-webfont.eot?#iefix') format('embedded-opentype'),
    url('/font-awesome/fontawesome-webfont.woff2') format('woff2'),
    url('/font-awesome/fontawesome-webfont.woff') format('woff'),
    url('/font-awesome/fontawesome-webfont.ttf') format('truetype'),
    url('/font-awesome/fontawesome-webfont.svgfontawesomeregular') format('svg');
    font-weight: normal;
    font-style: normal;
}

i {
    font-family: "FontAwesome";
}
setec
  • 15,506
  • 3
  • 36
  • 51