效果图如下:实现文字的字体颜色由 #ad4bd7 向 #6351fe 的颜色渐变效果。
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>css如何实现颜色渐变的字体</title>
<style>
.gradient-text {
color: transparent;
display: inline-block;
position: relative;
white-space: nowrap;
font-size: 20px;
font-weight: 600;
}
.gradient-text::before {
content: attr(data-text);
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(to right, #ad4bd7, #6351fe);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
</style>
</head>
<body>
<p class="gradient-text" data-text="程序猿小野">程序猿小野</p>
</body>
</html>