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?
You can try this example class. and find icon content here: http://astronautweb.co/snippet/font-awesome/
Actually even
font-awesome
CSS has a similar strategy for setting their icon styles. If you want to get a quick hold of theicon
code, check thenon-minified font-awesome.css
file and there they are....each font in its purity.Alternatively, if using Sass, one can "extend" FA icons to display them:
Example Link: https://codepen.io/bungeedesign/pen/XqeLQg
Get Icon code from: https://fontawesome.com/cheatsheet?from=io
Consolidating everything above, the following is the final class which works well
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.EDIT:
Font Awesome v5 uses other font names than older versions:
font-family: "Font Awesome 5 Free"
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
).