【问题标题】:Multiple font-awesome icons sharing a background gradient共享背景渐变的多个字体真棒图标
【发布时间】:2020-07-07 08:25:23
【问题描述】:

我已经在网上爬了大约 2 个小时,试图找到解决方案。所以我很抱歉,我的起始代码并没有太大帮助。

我尝试过使用background-clip、multiple.js、fill,就是无法得到我想要的效果。

事实上,我目前甚至无法复制:https://stackoverflow.com/a/56916981 使用 Font Awesome 5 和讨厌的 SVG。

这是我目前所在的位置:

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>
即使上面的剪辑有效,它仍然不是正确的效果。我想要一个单一的背景(我的意思是它的定位)出现在所有 3 个图标上,而不是每次都重置位置的 3 个渐变。 这是我想要实现的全部(动画只是一种奖励):

【问题讨论】:

  • 一个背景横跨所有图标。对吗?
  • 没错@ZohirSalak

标签: html css font-awesome linear-gradients font-awesome-5


【解决方案1】:

不要使用 SVG 版本并使用 CSS 版本,然后考虑 background-position 为每个图标移动背景以创建一个连续的:

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>

【讨论】:

  • 感谢 Temani! 5.12是否可以达到类似Anurag的效果?当我试图将它包装在一个 div 中时,我没有成功。我倾向于他们的答案纯粹是因为它允许无限的图标 - 因为你的解决方案依赖于他们只有 3
【解决方案2】:

在你使用的版本中,你会发现图标显示为svg,而background-clip 效果将不起作用。

您可以使用旧版本,其中元素在伪元素 (:before) 内呈现为 content,并为 i 标签设置父级以在图标上获得单一渐变:

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>

【讨论】:

  • 5.13可以做到吗?当我尝试切换样式表时,图标消失了(即使它们仍然显示为 而不是 codepen.io/liambbarr/pen/LYVXoNa
  • @Larm 尝试将 &lt;i&gt; 的显示更改为内联
  • 谢谢佐希尔!完美运行 - 我最初尝试过,但没有意识到 fontawesome css 正在将其改回 inline-block
【解决方案3】:

给你一个解决方案

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>

你在找这个吗?

【讨论】:

  • 这个答案与问题中链接的 OP 有什么不同吗?
  • @AnuragSrivastava,他们将 font-awesome 从 SVG 版本更改为字体版本。但是仍然不是我想要的。
猜你喜欢
  • 1970-01-01
  • 2014-07-09
  • 2019-01-01
  • 2014-12-18
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 2021-09-05
  • 2021-06-14
相关资源
最近更新 更多