CSS: Gradient text colour?

2019-02-25 08:48发布

问题:

I'm trying to get some text to have a gradient colour, but I haven't a clue how. I'm trying to get it like this:

The only thing I have tried is -webkit-mask, but I couldn't get that working how I wanted it to. The other thing would be -webkit-gradient in the color property - is that possible?

Cross browser compatibility is fairly important, but not the end of the world. I'd prefer the text to be a solid colour in IE than to convert the entire thing into an image (there is quite a bit of text like this, some of it dynamic).

回答1:

For webkit based browsers (Chrome & Safari) :

.text {
  font-size: 20px;
  background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#111));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

For IE & others:

http://www.webdesignerwall.com/demo/css-gradient-text/