Multiple font-awesome icons sharing a background gradient

给你一囗甜甜゛ 提交于 2020-05-17 03:11:08

问题


I've been crawling the web for about 2 hours now, trying to find a solution. So I apologise, my starting code isn't overly helpful.

I've tried using background-clip, multiple.js, fill, and just can't get the effect I'm looking for.

In fact, I currently can't even replicate: https://stackoverflow.com/a/56916981 with Font Awesome 5 and the pesky SVGs.

Here's where I'm currently at:

body{
  font-size:150px;
}
svg {
background: -webkit-linear-gradient(225deg, rgb(251, 175, 21), rgb(251, 21, 242),             
rgb(21, 198, 251)) 0% 0% / 300% 300%;
-webkit-text-fill-color: transparent;
animation: 2s ease 0s infinite normal none running fontgradient;
-webkit-animation: fontgradient 2s ease infinite;
-moz-animation: fontgradient 2s ease infinite;
animation: fontgradient 2s ease infinite;  
}

@-webkit-keyframes fontgradient {
0%{background-position:0% 92%}
50%{background-position:100% 9%}
100%{background-position:0% 92%}
}
@-moz-keyframes fontgradient {
0%{background-position:0% 92%}
50%{background-position:100% 9%}
100%{background-position:0% 92%}
}
@keyframes fontgradient { 
0%{background-position:0% 92%}
50%{background-position:100% 9%}
100%{background-position:0% 92%}
}
<script src="https://use.fontawesome.com/releases/v5.0.1/js/all.js"></script>
<i class="fab fa-stack-overflow"></i>
<i class="fab fa-instagram"></i>
<i class="fab fa-facebook-f"></i>
Even if the clipping above worked, it still wouldn't quite be the right effect. I want a single background (by that I mean it's positioning) to appear across all 3 icons, not 3 gradients with a position that's reset each time. And here's all I'm trying to achieve (Animation is simply a bonus):

回答1:


Well with the version that you are using, you will find the icons displayed as svg and the background-clip effect will not work.

You can use the older version where elements are rendered as content inside pseudo elements (:before), and set a parent for the i tags to get a single gradient across the icons:

body {
  font-size: 150px;
}

#parent {
  display: inline;
  background: -webkit-linear-gradient(225deg, rgb(251, 175, 21), rgb(251, 21, 242), rgb(21, 198, 251)) 0% 0% / 300% 300%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: 2s ease 0s infinite normal none running fontgradient;
}

i { 
  font-size: 5rem;
  font-style: normal;
  font-family: fontawesome;
}

@-webkit-keyframes fontgradient {
  0% {
    background-position: 0% 92%
  }
  50% {
    background-position: 100% 9%
  }
  100% {
    background-position: 0% 92%
  }
}

@-moz-keyframes fontgradient {
  0% {
    background-position: 0% 92%
  }
  50% {
    background-position: 100% 9%
  }
  100% {
    background-position: 0% 92%
  }
}

@keyframes fontgradient {
  0% {
    background-position: 0% 92%
  }
  50% {
    background-position: 100% 9%
  }
  100% {
    background-position: 0% 92%
  }
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div id="parent">
  <i class="fab fa-stack-overflow"></i>
  <i class="fab fa-instagram"></i>
  <i class="fab fa-facebook-f"></i>
</div>



回答2:


Don't use the SVG version and use the CSS one then consider background-position to shift the background for each icon to create a continuous one:

body {
  font-size: 100px;
}

i {
  float:left;
  background: 
     linear-gradient(225deg, rgb(251, 175, 21), rgb(251, 21, 242), rgb(21, 198, 251)) 
     0 0 / 3em 100%;
  -webkit-background-clip:text;
  background-clip:text;
  width:1em;
  text-align:center;
  -webkit-text-fill-color: transparent;
  animation: fontgradient 2s ease infinite;
}
.fa-instagram {
  background-position:-1em 0;
}
.fa-facebook-f {
  background-position:-2em 0;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.12.0/css/all.css">
<i class="fab fa-stack-overflow"></i>
<i class="fab fa-instagram"></i>
<i class="fab fa-facebook-f"></i>



回答3:


Here you go with a solution

body{
  font-size:150px;
}

i.fab {
  font-size: 5rem;
  font-style: normal;
  font-family: fontawesome;
  background: -webkit-linear-gradient(225deg, rgb(251, 175, 21), rgb(251, 21, 242),             
  rgb(21, 198, 251)) 0% 0% / 300% 300%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: 2s ease 0s infinite normal none running fontgradient;
  -webkit-animation: fontgradient 2s ease infinite;
  -moz-animation: fontgradient 2s ease infinite;
  animation: fontgradient 2s ease infinite;  
}
@-webkit-keyframes fontgradient {
  0%{background-position:0% 92%}
  50%{background-position:100% 9%}
  100%{background-position:0% 92%}
}
@-moz-keyframes fontgradient {
  0%{background-position:0% 92%}
  50%{background-position:100% 9%}
  100%{background-position:0% 92%}
}
@keyframes fontgradient { 
  0%{background-position:0% 92%}
  50%{background-position:100% 9%}
  100%{background-position:0% 92%}
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<i class="fab fa-stack-overflow"></i>
<i class="fab fa-instagram"></i>
<i class="fab fa-facebook-f"></i>

Are you looking for this?



来源:https://stackoverflow.com/questions/60871748/multiple-font-awesome-icons-sharing-a-background-gradient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!