如何使用Webkit渐变的Text-Shadow

时间:2018-06-04 14:43:27

标签: html css css3 shadow

当我使用带有Webkit渐变的Text-Shadow时,它似乎位于文本的顶部 - 而不是后面。我增加了阴影不透明度以显示它。



#title {
  font-size: calc(15px + 9vw);
  text-align: center;
  background: linear-gradient(to right, red 0%, blue 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-top: 8vh;
  text-shadow: -10px 15px 5px rgba(0,0,0,0.1),
                 -10px 20px 5px rgba(0,0,0,1),
                 -10px 20px 5px rgba(0,0,0,1);
}

<h3 id="title">Example</h3>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

想法是创建另一个带有伪元素的图层。

#title {
  font-size: calc(15px + 9vw);
  text-align: center;
  background: linear-gradient(to right, red 0%, blue 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-top: 8vh;
  position:relative;
}
#title:before {
  content:attr(data-text);
  position:absolute;
  top:0;
  right:0;
  left:0;
  bottom:0;
    text-shadow: -10px 15px 5px rgba(0,0,0,0.1),
                 -10px 20px 5px rgba(0,0,0,1),
                 -10px 20px 5px rgba(0,0,0,1);
  z-index:-1
}
<h3 id="title" data-text="Example">Example</h3>

如果您不想复制文本,则可以同时使用伪元素并仅在属性中定义文本:

#title {
  position: relative;
  text-align: center;
  margin-top: 8vh;
  font-size: calc(15px + 9vw);
  z-index: 0;
}

#title:before {
  content: attr(data-text);
  position: relative;
  background: linear-gradient(to right, red 0%, blue 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  z-index: 1;
}

#title:after {
  content: attr(data-text);
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  text-shadow: 
    -10px 15px 5px rgba(0, 0, 0, 0.1), 
    -10px 20px 5px rgba(0, 0, 0, 1), 
    -10px 20px 5px rgba(0, 0, 0, 1);
  z-index: -1;
}
<h3 id="title" data-text="Example"></h3>