PHP前端开发

方法中为您的网站提供渐变文本

百变鹏仔 3天前 #JavaScript
文章标签 您的

在本文中,我将向您展示几种使用 css 获取渐变文本的方法(以及一些不使用 css 的方法)。您可能已经熟悉其中一种方法。

方法 1、2 和 3 的 html

<h1>sub to axorax on yt!</h1>

方法 - 1(css)

h1 {  background: -webkit-linear-gradient(#e28bfc, #8bb8fc);  -webkit-background-clip: text;  -webkit-text-fill-color: transparent;}

方法 - 2(css)

h1 {  background: linear-gradient(#e28bfc, #8bb8fc);  background-clip: text;  color: transparent;}

方法 - 3(css 剪辑路径)

h1 {  position: relative;  display: inline-block;}h1::before {  content: "sub to axorax on yt!";  position: absolute;  top: 0;  left: 0;  width: 100%;  height: 100%;  background: linear-gradient(#e28bfc, #8bb8fc);  -webkit-background-clip: text;  color: transparent;  clip-path: text;}

方法 - 4 (svg)

<svg width="100%" height="100%"><defs><lineargradient id="gradient" x1="0%" y1="0%" x2="100%" y2="0%"><stop offset="0%" style="stop-color:#e28bfc;stop-opacity:1"></stop><stop offset="100%" style="stop-color:#8bb8fc;stop-opacity:1"></stop></lineargradient></defs><text x="10" y="50" font-size="72" fill="url(#gradient)">gradient text</text></svg>

方法 - 5(html 画布)

<canvas id="canvas" width="800" height="200"></canvas><script>  const canvas = document.getElementById('canvas');  const ctx = canvas.getContext('2d');  ctx.font = '3rem sans-serif';  const gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);  gradient.addColorStop(0, '#e28bfc');  gradient.addColorStop(1, '#8bb8fc');  ctx.fillStyle = gradient;  ctx.fillText('Sub to Axorax on YT!', 10, 100);</script>

希望您找到有用的东西!