ChatGPT解决这个技术问题 Extra ChatGPT

Can I change the color of Font Awesome's icon color?

I have to wrap my icon within an <a> tag for some reason.
Is there any possible way to change the color of a font-awesome icon to black?
or is it impossible as long as it wrapped within an <a> tag? Font awesome is supposed to be font not image, right?

https://i.stack.imgur.com/OEntj.png

<a href="/users/edit"><i class="icon-cog"></i> Edit profile</a>

E
Evan Hahn

This worked for me:

.icon-cog {
  color: black;
}

For versions of Font Awesome above 4.7.0, it looks this:

.fa-cog {
  color: black;
}

@Evans Thanks but the problem is I sometime put on the table cell which is black colored. Your css forced to change it into black also:(
You can add the following: .icon-cog.icon-white { color: white; }
Just came across this - I found that adding a class of 'fa' to all icons and then putting .fa {color: green;} did the trick. You can then do .fa.icon-white {color: white;} to get the same effect as you want above.
Is it possible to change background-color
@vamsikrishnamannem Yup. You can add background-color: #112233 to the CSS. Check out this code for an example.
J
J.B.

HTML:

    <i class="icon-cog blackiconcolor">

css :

    .blackiconcolor {color:black;}

you can also add extra class to the button icon...


This is a nice, neat solution. I prefer to name classes with an fa prefix like fa-black or fa-blue just to make it clear I'm applying them to font awesome icons.
d
dandrews

You can specify the color in the style attribute:

<a href="/users/edit"><i class="icon-cog" style="color:black"></i> Edit profile</a>

-1 Firstly, this is wrong, because it will change the color of the text also, not just the color of the icon, as requested. Secondly, using inline css should not be promoted.
Where does the question say "just the color of the icon" and not the color of the text?
I think that's rather obviously implied by the way the question is phrased. And it seems you would agree, since you updated your answer :)
I wanna give a small hint for Bootstrap.<i> tag with using text-primary class, override the color of icon as blue. so its good to be aware of it.
@AdrianSchmidt isn't the color style inside the i tag and text outside the i tag
C
ChAnDu353

To change the font awesome icons color for your entire project use this in your css

.fa {
  color : red;
}

This is nice but changes all the icons in my project. I created classes like .fa-black {color: #000;}. I then added the fa-black classes to the icons where I wanted a different colour.
F
Frank van Wijk

Try this:

<i class="icon-cog text-red">
<i class="icon-cog text-blue">
<i class="icon-cog text-yellow">

And where does text-red, text-blue or text-yellow come from?
it come from bootstrap classes
you can also use text-danger instead of text-red, text-warning instead of text-yellow, text-success instead of text-green etc. depends on what you want.
d
dwjohnston

If you don't want to alter the CSS file, this is what works for me. In HTML, add style with color:

<i class="fa fa-cog" style="color:#fff;"></i>

A
Adrian Schmidt

To hit only cog-icons in that kind of button, you can give the button a class, and set the color for the icon only when inside the button.

HTML:

<a class="my-nice-button" href="/users/edit">
    <i class="icon-cog"></i>
    Edit profile
</a>

CSS:

.my-nice-button>i { color: black; }

This will make any icon that is a direct descendant of your button black.


G
Gonçalo Peres

Is there any possible way to change the color of a font-awesome icon to black?

Yes, there is. See the snipped bellow

Edit Profile

Font awesome is supposed to be font not image, right?

Yes, it is a font. Therefore, you are able to scale it to any size without losing quality.


S
Sategroup

With reference to @ClarkeyBoy answer, below code works fine, if using latest version of Font-Awesome icons or if you using fa classes

.fa.icon-white {
    color: white;
}

And, then add icon-white to existing class


C
Crasher

For me the only thing that worked is inline css + overriding

<i class="fas fa-ellipsis-v fa-2x" style="color:red !important"></i>

S
Styx

just give and text style whatever you want like :D HTML:

<a href="javascript:;" class="fa fa-trash" style="color:#d9534f;">
   <span style="color:black;">Text Name</span>
</a>

S
Stanjhae
.fa-search{
    color:#fff;
 }

you write that code in css and it would change the color to white or any color you want, you specify it


A
Afeesudheen

You can change the Font Awesome's icon color with the bootstrap class

use

text-color

example

text-white

B
Buddy

just give it the style whatever you want like

style="color: white;font-size: 20px;"

M
Mohammed Shareef C

You can change the color of a fontawesome icon by changing its foreground color using the css color property. The following are examples:

<i class="fa fa-cog" style="color:white">

This supports svgs also

<style>
.fa-cog{
color:white;
}
</style>

<style>
.parent svg, .parent i{
color:white
}
</style>

<div class="parent">
  <i class="fa fa-cog" style="color:white">
</div>


T
Tasos K.

Write this code in the same line, this change the icon color:

<li class="fa fa-id-card-o" style="color:white" aria-hidden="true">

m
mohamed ibrahim

Use color property to change the color of your target element as follow :

.icon-cog { // selector of your element
    color: black;
}

Or in the newest version of font-awesome , you can choose among filled or not filled icons


A
Ahmad

If you want to change the color of a specific icon, you can use something like this:

.fa-stop {
    color:red;
}

P
Pepe

It might a little bit tricky to change the color of font-awesome icons. The simpler method is to add your own class name inside the font-awesome defined classes like this:

.

And target your custom_defined__class_name in your CSS to change the color to whatever you like.


R
Rasla

Open the svg file in vs code or something

change the line

path fill="gray" to what ever color you want! in my case i change it to gray!


I
Imran Wahid

Sometimes changing the colour in the external css file doesn't work. You can add inline css property in the icon tag and that will work.

For example


R
Re Captcha

HTML:

<i class="icon-cog blackiconcolor">

css :

 .blackiconcolor {color:black;}

Using class will give you a free binding property which you can apply on any tag you require.


-1 Font Awesome icons are text, hence the name. They should be colored using css like any other text, not using vendor-specific tricks. Also, since they are text, they are already black-and-white (or mono-color).
@AdrianSchmidt Above stated answer is for converting an image to black, I didn't mean to change the font color with this, I hope you are getting me ..
I understand. The answer is still incorrect in regard to the question though. Font Awesome icons are not images, they are text.