css – fontawesome 5.x将颜色设置为图标

前端之家收集整理的这篇文章主要介绍了css – fontawesome 5.x将颜色设置为图标前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法使用这些代码着色font-awesome5图标.我尝试了“填充”css属性来设置颜色,但它没有用.

HTML代码

<div class="container mt200 icons">
  <div class="col-md-3">
    <div class="bggray2 text-center">
      <i class="fas fa-microphone fa-5x"></i>
      <div class="title">LOREM</div>
      <div class="text">Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.</div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="bggray2 text-center">
      <i class="far fa-edit fa-5x"></i>
      <div class="title">LOREM</div>
      <div class="text">Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.
      </div>
    </div>
  </div>
</div>

CSS代码

.icons i {
  color: #2759AE;
}

解决方法

Font Awesome 5使用svg作为图标,里面的路径设置为fill:currentColor,所以只需更改svg的颜色:
.icons svg {
 color:#2759AE;
}
<script defer src="https://use.fontawesome.com/releases/v5.5.0/js/all.js"></script>
<div class="container mt200 icons">
  <div class="col-md-3">
    <div class="bggray2 text-center">
      <i class="fas fa-microphone fa-5x"></i>
      <div class="title">LOREM</div>
      <div class="text">Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.</div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="bggray2 text-center">
      <i class="far fa-edit fa-5x"></i>
      <div class="title">LOREM</div>
      <div class="text">Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.
      </div>
    </div>
  </div>
</div>

正如您在代码中看到的,当您加载JS版本时,i将替换为svg:

如果您使用的是CSS版本,可以将颜色应用于i.

.icons i {
 color:#2759AE;
}
<link href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" rel="stylesheet"><div class="container mt200 icons">
  <div class="col-md-3">
    <div class="bggray2 text-center">
      <i class="fas fa-microphone fa-5x"></i>
      <div class="title">LOREM</div>
      <div class="text">Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.</div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="bggray2 text-center">
      <i class="far fa-edit fa-5x"></i>
      <div class="title">LOREM</div>
      <div class="text">Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.
      </div>
    </div>
  </div>
</div>

因此,为了确保它在所有情况下都能工作,只需使用两个选择器:

.icons i,.icons svg {
   color: #2759AE;
}
原文链接:https://www.f2er.com/css/242341.html

猜你在找的CSS相关文章