Change color when hover a font awesome icon?
Asked Answered
J

3

49

Base in the Font Awesome documentation I create this icon:

<span class="fa-stack fa-5x">
  <i class="fa fa-circle fa-stack-2x"></i>
  <i class="fa fa-flag fa-stack-1x fa-inverse"></i>
</span>

This code create this html:

<span class="fa-stack fa-5x">
  <i class="fa fa-circle fa-stack-2x">::before</i>
  <i class="fa fa-flag fa-stack-1x fa-inverse">::before</i>
</span>

It is a stacked Flag icon. I want to change the icon color on Hover event, I tried with all these:

.fa-stack:hover{
color: red
}
.fa-stack i:hover{
color: red
}
.fa-stack i before:hover{
color: red
}

but not working.

Jud answered 10/9, 2014 at 16:28 Comment(3)
it should be color: red;Kommunarsk
Check your CSS syntaxSilin
was a typo but problem is still thereJud
K
79

if you want to change only the colour of the flag on hover use this:

http://jsfiddle.net/uvamhedx/

.fa-flag:hover {
    color: red;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<i class="fa fa-flag fa-3x"></i>
Kommunarsk answered 10/9, 2014 at 18:23 Comment(2)
And using Font Awesome 5 ? use.fontawesome.com/releases/v5.0.4/js/all.jsRanger
@Ranger do you have codepen or jsffiddle with your issue?Kommunarsk
D
7

use - !important - to override default black

.fa-heart:hover{
   color:red !important;
}
.fa-heart-o:hover{
   color:red !important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<i class="fa fa-heart fa-2x"></i>
<i class="fa fa-heart-o fa-2x"></i>
Dympha answered 4/5, 2019 at 12:5 Comment(3)
Is !important is really necessary? Many consider it as anti-pattern in programming.Vertebral
using Important is not a good solution since it cannot be overwritten. You can overwrite all non inline css using specificityPratique
I had to use !important to get this to work, unfortunately, but the icon was also within an ag-Grid, if that means anything. For an Angular project, I also had to put it in the styles.css file - it would not work in the component.cssGimbals
K
0

The only thing you need to do is this, example:

HTML:

<span class="close_form">
  <i class="fa fa-circle"></i>
</span>

CSS:

.fa-circle {
  color: #d7d7d710;
  transition: 500ms;
}
    
.close_form:hover .fa-circle {
  color: #43434379;
  transition: 500ms;
}
Keffer answered 11/1, 2024 at 23:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.