Change the color of Font Awesome's icon

2020-01-29 18:12发布

How to change the inner (white) color of the icon <i class="fa fa-exclamation-triangle"></i> ?

P.S. Applying <i class="fa fa-exclamation-triangle" style="color:red"></i> is not an answer, as the red color applies to the outer color, not to the inner exclamation white sign itself.

The Font Awesome version is: 4.7.0.

2条回答
成全新的幸福
2楼-- · 2020-01-29 19:00

The answer of Temani Afif is a good one! Here's another approach;

.fa-exclamation-triangle{
  position: relative;
  z-index:0;
}
.fa-exclamation-triangle:after{
  content: ' ';
  width: 20%;
  height: 70%;
  position: absolute;
  top: 15%;
  left: 40%;
  background: red;
  z-index: -1;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<i class="fa fa-exclamation-triangle fa-5x"></i>
<i class="fa fa-exclamation-triangle fa-4x"></i>
<i class="fa fa-exclamation-triangle fa-2x"></i>

查看更多
家丑人穷心不美
3楼-- · 2020-01-29 19:06

The exclamation mark of this icon is a transparent part, so a trick is to add a background behind it to have the needed coloration. Of couse, the background shouldn't cover the whole area so we need to use a gradient to cover only a part of it.

.fa-exclamation-triangle {
  background:linear-gradient(red,red) center bottom/20% 84% no-repeat;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
<i class="fas fa-exclamation-triangle fa-7x"></i>
<i class="fas fa-exclamation-triangle fa-4x"></i>
<i class="fas fa-exclamation-triangle fa-2x"></i>

The same thing with the V4:

.fa-exclamation-triangle {
  background:linear-gradient(red,red) center /20% 70% no-repeat;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<i class="fa fa-exclamation-triangle fa-5x"></i>
<i class="fa fa-exclamation-triangle fa-4x"></i>
<i class="fa fa-exclamation-triangle fa-2x"></i>


Also the SVG version:

.fa-exclamation-triangle {
  background:linear-gradient(red,red) center bottom/20% 84% no-repeat;
}
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js" ></script>
<i class="fas fa-exclamation-triangle fa-7x"></i>
<i class="fas fa-exclamation-triangle fa-4x"></i>
<i class="fas fa-exclamation-triangle fa-2x"></i>

UPDATE

To make the answer more generic we can also consider multiple background and radial-gradient in order to color any kind of shape. The trick is to cover with the background the transparent part without overflowing.

Here is some example of icons:

.fa-exclamation-triangle {
  background:linear-gradient(red,red) center bottom/20% 84% no-repeat;
}
.fa-ambulance {
 background:
  linear-gradient(blue,blue) 25% 30%/32% 45% no-repeat, 
  radial-gradient(green 60%,transparent 60%) 15% 100%/30% 30% no-repeat,
  radial-gradient(green 60%,transparent 60%) 85% 100%/30% 30% no-repeat;
}
.fa-check-circle {
  background:radial-gradient(yellow 60%,transparent 60%);
}
.fa-angry {
 background: 
  radial-gradient(red 60%,transparent 60%) 25% 40%/30% 30% no-repeat,
  radial-gradient(red 60%,transparent 60%) 75% 40%/30% 30% no-repeat;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
<i class="fas fa-exclamation-triangle fa-7x"></i>
<i class="fas fa-ambulance fa-7x"></i>
<i class="fas fa-check-circle fa-7x"></i>
<i class="fas fa-angry fa-7x"></i>

查看更多
登录 后发表回答