如何同时使用文本阴影和线性渐变的文本?

时间:2019-03-20 19:58:54

标签: html css css3 linear-gradients

我尝试同时使用它们,但是失败了。

var leads = $('#leads');
const total = leads.children('td').toArray().map(x=>Number(x.innerHTML)).reduce((sum, x) => sum + x)
leads.append(`<th>${total}</th>`);
h1 {
  font-size: 72px;
  background: linear-gradient(to top, black 50%, orange 50%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}

仅使用线性渐变而没有文本阴影: enter image description here

仅使用文本阴影而不使用线性渐变: enter image description here

同时使用: enter image description here

2 个答案:

答案 0 :(得分:0)

基于此previous answer,这里有一个您需要复制文本的想法:

h1 {
  font-family:sans-serif;
  font-size:60px;
  font-weight: bold;
  position:relative;
  margin:20px;
}
h1::before,
h1::after {
  content:attr(data-text);
}
h1::after {
  color:#fff; /*use white*/
  /*create the stroke around text*/
  text-shadow:
    1px 0  0px #000,
    0 1px 0px #000,
    1px 1px 0px #000,
    -1px 0 0px #000,
    0 -1px 0px #000,
    -1px -1px 0px #000,
    -1px 1px 0px #000,
    1px -1px 0px #000;
  mix-blend-mode: darken; /*everything is more dark than white so we always see the background */
}
h1::before {
  position:absolute;
  top:0;
  left:0;
  background:linear-gradient(to bottom,yellow 50%, red 51%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color:transparent;
}
<h1 data-text="Heading 1"></h1>

答案 1 :(得分:-1)

如果有background: linear-gradient(to top, black 50%, orange 50%);,则应将其替换为bottom,
以及有1px 0 black的地方,都应该摆脱black

<style>   
h1 {
  font-size: 72px;
  background: -webkit-linear-gradient(bottom, black 50%, orange 50%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: -1px 0, 0 1px, 1px 0, 0 -1px
}
</style>
<h1>Heading 1</h1>

更改:

background: -webkit-linear-gradient( bottom, black 50%, orange 50%); text-shadow: -1px 0 black , 0 1px black , 1px 0 black , 0 -1px black `;