Use Font Awesome icon as CSS content - html

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?

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

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>

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

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

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

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.

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.

Update for Font Awesome 5 using SCSS
.icon {
#extend %fa-icon;
#extend .fas;
&:before {
content: fa-content($fa-var-user);
}
}

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

Related

Auto Remove of Empty Span Class When Added Beside Input Type Text in Wordress

I am having a problem in my wordpress header adding search button.
I figured out that when I added input type text beside an empty span class, the span class search icon removes after refreshing the page. Any idea how can I fix this?
Here's my code
<style>
#font-face {
font-family: 'icomoon';
src:url('../wp-content/plugins/search-extend/fonts/icomoon/icomoon.eot?
#iefix') format('embedded-opentype'),
url('../wp-content/plugins/search-extend/fonts/icomoon/icomoon.ttf')
format('truetype');
}
.sb-icon-search {
color: #fff;
background: #00c80e;
z-index: 90;
font-size: 22px;
font-family: 'icomoon';
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
-webkit-font-smoothing: antialiased;
}
.sb-icon-search:before
{
content: "\e000";
}
</style>
<div id="sb-search" class="sb-search">
<form>
<input class="sb-search-submit" type="text" value="">
<span class="sb-icon-search"></span> #This removes automatically after
refreshing the page
</form>
</div>
I already tried this function
function override_mce_options($initArray) {
$opts = '*[id|name|class|style]';
$initArray['valid_elements'] .= ',' . $opts;
$initArray['extended_valid_elements'] .= ',' . $opts;
return $initArray;
}
add_filter('tiny_mce_before_init', 'override_mce_options');
But it seems that the issue is not from the Text editor since i tried changing
the input type "text" to type "submit" it works fine. All removed even I inspect it.
Any Idea please? Please Help.
Seems like the editor is removing it.
I think you can add the search directly to the sb-search item instead of the icon. I have added the css code which will help you. You might have to adjust the position.
.sb-search {
position: relative;
/* other css properties */
}
.sb-search:before {
content: "\e000";
color: #fff;
background: #00c80e;
z-index: 90;
font-size: 22px;
font-family: 'icomoon';
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
position: relative;
-webkit-font-smoothing: antialiased;
position: absolute;
right: 0;
top: 0;
/* You might have to adjust the position */
}
This way you don't have to worry about the span element and you can remove it.

FontAwesome icons doesn't work in css content

I am entering the following code:
a::before {
content: "\f061";
font-family: FontAwesome;
}
But font-awesome icon doesn't show up, just an empty square.
What is the problem?
Thank you!
TRY THIS, it works for me :)
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css">
or
#import url("https://use.fontawesome.com/releases/v5.2.0/css/all.css");
a::before {
content: "\f061";
font-family: "Font Awesome 5 Free";
font-style: normal;
font-weight: 900;
text-decoration: inherit;
}
This answer from #TemaniAfif may be helpful.
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css">
#import url("https://use.fontawesome.com/releases/v5.2.0/css/all.css");
.fp-prev:before {
color:#000;
content: '\f35a'; /* You should use \ and not /*/
font-family: "Font Awesome 5 Free"; /* This is the correct font-family*/
font-style: normal;
font-weight: normal;
text-decoration: inherit;
}

How to replace icon-font source with normal image source in CSS file

I want to replace the images with new ones but the problem is that the images are in font style.
Now I want to replace the following with normal image source, like
<img src="images/upload.png>
Below is the CSS file in which I want to make changes
#font-face {
font-family: "fl-bigmug-line";
src: url("../fonts/fl-bigmug-line.eot");
src: url("../fonts/fl-bigmug-line.eot#iefix") format("embedded-opentype"),
url("../fonts/fl-bigmug-line.woff") format("woff"),
url("../fonts/fl-bigmug-line.ttf") format("truetype"),
url("../fonts/fl-bigmug-line.svg") format("svg");
font-weight: normal;
font-style: normal;
}
.fl-bigmug-line-ico,
[class^="fl-bigmug-line-"]:before, [class*=" fl-bigmug-line-"]:before,
[class^="fl-bigmug-line-"]:after, [class*=" fl-bigmug-line-"]:after {
font-family: 'fl-bigmug-line';
font-size: inherit;
font-weight: 400;
font-style: normal;
}
.fl-bigmug-line-add137:before {
content: "\e000";
}
.fl-bigmug-line-add139:before {
content: "\e001";
}
.fl-bigmug-line-add149:before {
content: "\e002";
I tried to change the content like content: url("images/upload.png"); but it's not working.
You can do something like this:
.the-icon {
display: inline-block;
width: 30px;
height: 30px;
}
.the-icon:before {
content: url("https://www.gravatar.com/avatar/3116de43059d7ea110181a5bb773bcdd?s=30");
}
<div class="container">
<i class="the-icon"></i> Upload
</div>

How do I use icons from Flaticon in my html?

I want to give my website some icons. Now I see that many people use this website Flaticon.
What I have done is put something like this in my CSS:
/**
* Font 1
*/
#font-face {
font-family: "Flaticon1";
src: url("flaticon1.eot");
src: url("flaticon1.eot#iefix") format("embedded-opentype"), url("flaticon1.woff") format("woff"), url("flaticon1.ttf") format("truetype"), url("flaticon1.svg") format("svg");
font-weight: normal;
font-style: normal;
}
[class^="flaticon1-"]:before,
[class*=" flaticon1-"]:before,
[class^="flaticon1-"]:after,
[class*=" flaticon1-"]:after {
font-family: "Flaticon1";
font-size: 20px;
font-style: normal;
margin-left: 20px;
}
.flaticon1-basic21:before {
content: "\e000";
}
.flaticon1-bicycle21:before {
content: "\e001";
}
.flaticon1-car6:before {
content: "\e002";
}
/**
* Font 2
*/
#font-face {
font-family: "Flaticon2";
src: url("flaticon2.eot");
src: url("flaticon2.eot#iefix") format("embedded-opentype"), url("flaticon2.woff") format("woff"), url("flaticon2.ttf") format("truetype"), url("flaticon2.svg") format("svg");
font-weight: normal;
font-style: normal;
}
[class^="flaticon2-"]:before,
[class*=" flaticon2-"]:before,
[class^="flaticon2-"]:after,
[class*=" flaticon2-"]:after {
font-family: "Flaticon2";
font-size: 20px;
font-style: normal;
margin-left: 20px;
}
.flaticon2-basic21:before {
content: "\e000";
}
.flaticon2-bicycle21:before {
content: "\e001";
}
.flaticon2-car6:before {
content: "\e002";
}
http://support.flaticon.com/hc/en-us/articles/205019862-CSS-code-for-Iconfont-
I downloaded the wanted icon but it doesn't display the icon. It displays this:
What have I done wrong?
1. Prepare your collection
Add monocolor icons to the collection for your website
2. Download iconfont
Open your collection and press the Download collection button, then select Iconfont
3. Prepare your collection
Copy the source files and CSS to your web folder.
4. Load the stylesheet
Request the CSS stylesheet from the head of your web.
< link rel="stylesheet" type="text/css" href="your_website_domain/css_root/flaticon.css" >
5. Use the classes
Use the classes of each icon and they will appear on your website. After, manipulate them with CSS.
Use example:
<i class="flaticon-airplane49"></i> or <span class="flaticon-airplane49"></span>
Apparently I downloaded file formats, not font files. I was only able to download icons as either Iconfont or SVG sprite after I have put icons in my collection. It downloads the whole collection of course.
Just download the icon and put this in the head:
<link rel="icon" href="resources/filename"/>
You need to have the flaticon.css file in your server (and the other 5 or 6).
Then check which items are available there.
.flaticon-bath:before { content: "\f122"; }
And then you just use the class.
To fix the size...
font-family: Flaticon;
font-size: 50px;
font-style: normal;
margin-left: 7px;
}
This is how you can change the font size, just paste the below code in your css file and that's it.
[class^="flaticon-"]:before, [class*=" flaticon-"]:before, [class^="flaticon-"]:after, [class*=" flaticon-"]:after {
font-size: 150px;
}

How to change hover color for iconfont icons?

Just beginning learning web development. I understand how to change the hover color for regular links using .classname a:hover{} but every variation of that I've tried hasn't worked. I think there's some quirk to my icons because I downloaded them as an iconfont from flat-icon.com and their instructions (given here: http://www.flaticon.com/iconfont-demo/) make it seem like you need to manipulate the flaticon.css file to change any characteristics like the icon color.
Anyway, this is the relevant part of my code:
<div class="jumbotron">
<div class="container" >
<h4>Family Owned and Operated Since 1978</h4>
<table>
<tr class="big-flat-icons">
<td><i class="flaticon-pipe9"></i></td>
<td><i class="flaticon-diploma20"></i></td>
<td><i class="flaticon-man204"></i></td>
<td><i class="flaticon-old26"></i></td>
</tr>
</table>
</div>
</div>
I tried putting this in my main.css file but it had no effect:
.jumbotron .container a:hover{
text-decoration: none;
color: #FF00FF;
}
I had to manipulate the code inside the flaticon.css file that came with the font to change the icons' size and color when originally putting them on my page so I feel like there must also be something I need to do in here to get the hover color working but I haven't figured it out yet:
#font-face {
font-family: "Flaticon";
src: url("flaticon.eot");
src: url("flaticon.eot#iefix") format("embedded-opentype"),
url("flaticon.woff") format("woff"),
url("flaticon.ttf") format("truetype"),
url("flaticon.svg") format("svg");
font-weight: normal;
font-style: normal;
}
[class^="flaticon-"]:before, [class*=" flaticon-"]:before,
[class^="flaticon-"]:after, [class*=" flaticon-"]:after {
font-family: Flaticon;
font-size: 100px;
font-style: normal;
margin-left: 20px;
color: white;
}
.flaticon-antique:before {
content: "\e000";
}
.flaticon-construction3:before {
content: "\e001";
}
Sorry for the long question. Does anyone have any ideas?
If it helps, this is where I have the site I'm working on: http://reagankm.github.io/
I think that icon font doesn't receive the hover style properly, but you can try this:
.jumbotron .container a:hover i:before {
text-decoration: none;
color: #FF00FF;
}