Font Awesome & Unicode

matan129 picture matan129 · Jun 22, 2013 · Viewed 142.4k times · Source

I'm using (the excellent) Font-Awesome in my site, and it's working fine, if I use it this way:

<i class="icon-home"></i>

But (for some reasons) I want to use it in the Unicode way, like:

<i>&#xf015;</i>

(Font Awesome's cheatsheet)

But it doesn't work - the browser shows a square instead.

How do I solve this? The CSS path is correct (as the first way of using Font Awesome works).

Edit: I do have the FontAwesome.otf installed.

Answer

Jukka K. Korpela picture Jukka K. Korpela · Jun 23, 2013

It does not work, because <i>&#xf015;</i> simply asks the browser to display the Private Use code point U+F015 using an italic typeface. The Font Awesome CSS code has nothing that would affect this. If you add class=icon-home to the tag, you will get the glyph assigned to U+F015 in the FontAwesome font, but you will get it twice, due to the way the Font Awesome trickery works.

To get the glyph just once, you need to use CSS that asks for the use of the FontAwesome font without triggering the rules that add a glyph via generated content. A simple trick is to use a class name that starts with icon- but does not match any of the predefined names in Font Awesome or any name otherwise used in your CSS or JavaScript code. E.g.,

<i class=icon-foo>&#xf015;</i>

Alternatively, use CSS code that sets font-family: FontAwesome and font-style: normal on the i element.

PS. Note that Private Use code points such as U+F015 have, by definition, no interoperable meaning. Consequently, when style sheets are disabled, &#xf015; will not be displayed as any character; the browser will use its way of communicating the presence of undefined data, such as a small box, possibly containing the code point number.