不喜欢,但是设计要求,也只能干了。
方法一:
background: linear-gradient(to right, red, blue)
这行是给背景设置为渐变色,这样是简写了,
其实是给background-image
设置为渐变色,不是 background-color;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
h1{
background: linear-gradient(to right, red, blue);
-webkit-background-clip: text;
color: transparent;
}
</style>
</head>
<body>
<h1>亮瞎眼</h1>
</body>
</html>
-webkit-background-clip: text;
目前还不是能所有浏览器都支持。
语法 :
background-clip: border-box|padding-box|content-box;
取值为text的意思,就是以区块内的文字作为裁剪区域向外裁剪,文字的背景即为区块的背景,文字之外的区域都将被裁剪掉。
所以,我们最后写color: transparent; 让文字为透明色,就是让后面背景色显示出来。
方法二:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<style type="text/css">
h1{
position: relative;
color: yellow;
}
h1:before{
content: attr(text);
position: absolute;
z-index: 10;
color:pink;
-webkit-mask:linear-gradient(to left, red, transparent );
}
</style>
</style>
</head>
<body>
<h1>亮瞎眼</h1>
</body>
</html>