前端實現(xiàn)文字漸變的三種方式
前言
最近開發(fā)的時候發(fā)現(xiàn)很多ui圖上面的標(biāo)題都是帶有漸變效果的,這里就記錄一下前端實現(xiàn)文字漸變的幾種方式。
完整效果如下

CSS 方式
通過給文字容器的背景設(shè)置漸變顏色,并使用background-clip屬性,將其以文字內(nèi)容進(jìn)行裁切。最后使用text-fill-color屬性,給文字設(shè)置透明填充來實現(xiàn)
| 屬性名稱 | 值 | 效果 |
|---|---|---|
| background | linear-gradient(to top, #b1495a, #c71a44) | 給文字容器設(shè)置漸變背景色 |
| background-clip | text | 背景被裁切成文字的前景色 |
| text-fill-color | transparent | 文字的填充顏色 |
效果如下

- 具體樣式代碼
.up-gradient {
background: linear-gradient(to top, #b1495a, #c71a44);
/* 背景被裁剪成文字的前景色。 */
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
/* 文字透明填充 */
text-fill-color: transparent;
}
.down-gradient {
background: linear-gradient(to bottom, #b1495a, #c71a44);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
/* 文字透明填充 */
text-fill-color: transparent;
}
.left-gradient {
background: linear-gradient(to left, #b1495a, #c71a44);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
/* 文字透明填充 */
text-fill-color: transparent;
}
.right-gradient {
background: linear-gradient(to right, #b1495a, #c71a44);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
/* 文字透明填充 */
text-fill-color: transparent;
}
/* 多顏色漸變 */
.multi-gradient {
background: linear-gradient(90deg, #b1495a 10%, #c71a44 50%, #ffb86c 80%);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
/* 文字透明填充 */
text-fill-color: transparent;
}
html 結(jié)構(gòu)
<body>
<div class="container">
<h1>CSS實現(xiàn)文字漸變</h1>
<!-- css版本 -->
<article class="panel">
<div class="panel-box-title">CSS版:</div>
<div class="box">
<div class="content-text up-gradient">向上漸變</div>
<div class="content-text down-gradient">向下漸變</div>
<div class="content-text left-gradient">向左漸變</div>
<div class="content-text right-gradient">向右漸變</div>
<!-- 設(shè)置多個顏色 -->
<div class="content-text multi-gradient">多顏色漸變</div>
</div>
</article>
</div>
</body>
Canvas 方式
canvas中的文字漸變的實現(xiàn)方式就很簡單了,因為canvas可以直接給文字設(shè)置漸變樣式。
主要用到createLinearGradient方法,用來創(chuàng)建一個線性漸變,addColorStop設(shè)置漸變的色標(biāo),就像是這個效果

最后再用fillStyle指定使用我們創(chuàng)建的漸變對象即可
效果如下

核心代碼
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>文字漸變</title>
<link rel="stylesheet" href="index.css" rel="external nofollow" rel="external nofollow" />
</head>
<body>
<div class="container">
<!-- canvas版本 -->
<article class="panel">
<div class="panel-box-title">Canvas版:</div>
<div class="box">
<canvas id="canvas" height="180" width="900"></canvas>
</div>
</article>
</div>
</body>
<script>
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
ctx.font = '32px Arial'
// 從左到右的漸變文字
const leftToRightGradient = ctx.createLinearGradient(0, 0, canvas.width, 0)
leftToRightGradient.addColorStop(0, '#fff')
leftToRightGradient.addColorStop(1, '#000')
ctx.fillStyle = leftToRightGradient
ctx.fillText('Canvas 從左到右漸變', 20, 40)
// 從上到下的漸變文字
const topToBottomGradient = ctx.createLinearGradient(0, 0, 0, canvas.height)
topToBottomGradient.addColorStop(0, '#fff')
topToBottomGradient.addColorStop(1, '#000')
ctx.fillStyle = topToBottomGradient
ctx.fillText('Canvas 從上到下漸變', 20, 80)
// 從右到左的漸變文字
const rightToLeftGradient = ctx.createLinearGradient(canvas.width, 0, 0, 0)
rightToLeftGradient.addColorStop(0, '#fff')
rightToLeftGradient.addColorStop(1, '#000')
ctx.fillStyle = rightToLeftGradient
ctx.fillText('Canvas 從右到左漸變', 20, 120)
// 從下到上的漸變文字
const bottomToTopGradient = ctx.createLinearGradient(0, canvas.height, 0, 0)
bottomToTopGradient.addColorStop(0, '#fff')
bottomToTopGradient.addColorStop(1, '#000')
ctx.fillStyle = bottomToTopGradient
ctx.fillText('Canvas 從下到上漸變', 20, 160)
</script>
</html>
SVG 方式
SVG 文字漸變的核心原理是使用 SVG 的<linearGradient>定義漸變,然后通過fill="url(#gradientId)"將漸變應(yīng)用到文字上。
漸變效果如下
核心代碼如下
<svg width="900" height="180" xmlns="http://www.w3.org/2000/svg">
<defs>
<!-- 從左到右漸變 -->
<linearGradient
id="leftToRight"
x1="0%"
y1="0%"
x2="100%"
y2="0%"
>
<stop
offset="0%"
style="stop-color: #b1495a; stop-opacity: 1"
/>
<stop
offset="100%"
style="stop-color: #c71a44; stop-opacity: 1"
/>
</linearGradient>
<!-- 從上到下漸變 -->
<linearGradient
id="topToBottom"
x1="0%"
y1="0%"
x2="0%"
y2="100%"
>
<stop
offset="0%"
style="stop-color: #b1495a; stop-opacity: 1"
/>
<stop
offset="100%"
style="stop-color: #c71a44; stop-opacity: 1"
/>
</linearGradient>
<!-- 從右到左漸變 -->
<linearGradient
id="rightToLeft"
x1="100%"
y1="0%"
x2="0%"
y2="0%"
>
<stop
offset="0%"
style="stop-color: #b1495a; stop-opacity: 1"
/>
<stop
offset="100%"
style="stop-color: #c71a44; stop-opacity: 1"
/>
</linearGradient>
<!-- 從下到上漸變 -->
<linearGradient
id="bottomToTop"
x1="0%"
y1="100%"
x2="0%"
y2="0%"
>
<stop
offset="0%"
style="stop-color: #b1495a; stop-opacity: 1"
/>
<stop
offset="100%"
style="stop-color: #c71a44; stop-opacity: 1"
/>
</linearGradient>
</defs>
<!-- 從左到右漸變文字 -->
<text
x="20"
y="40"
font-family="Arial"
font-size="32"
font-weight="bold"
fill="url(#leftToRight)"
>
SVG 從左到右漸變
</text>
<!-- 從上到下漸變文字 -->
<text
x="20"
y="80"
font-family="Arial"
font-size="32"
font-weight="bold"
fill="url(#topToBottom)"
>
SVG 從上到下漸變
</text>
<!-- 從右到左漸變文字 -->
<text
x="20"
y="120"
font-family="Arial"
font-size="32"
font-weight="bold"
fill="url(#rightToLeft)"
>
SVG 從右到左漸變
</text>
<!-- 從下到上漸變文字 -->
<text
x="20"
y="160"
font-family="Arial"
font-size="32"
font-weight="bold"
fill="url(#bottomToTop)"
>
SVG 從下到上漸變
</text>
</svg>
完整示例代碼
index.css
樣式代碼
html,
body {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #000;
color: #fff;
font-family: 'Segoe UI', 'Arial', sans-serif;
font-size: 16px;
line-height: 1.5;
display: flex;
flex-direction: column;
align-items: center;
user-select: none;
}
/* 外層容器 */
.container {
width: 80%;
max-width: 900px;
margin: 40px auto;
padding: 24px;
background: #181c24;
border-radius: 18px;
box-shadow: 0 8px 40px rgba(0, 0, 0, 0.45);
border: 1.5px solid #232936;
display: flex;
flex-direction: column;
align-items: center;
gap: 12px;
}
.panel {
position: relative;
width: 100%;
display: flex;
flex-direction: column;
gap: 12px;
}
.panel-box-title {
font-weight: bold;
color: #ffb86c;
text-shadow: 0 2px 8px #181c24cc;
}
/* 通用文字樣式 */
.content-text {
font-size: 32px;
font-weight: bold;
}
.box {
background: #191b22;
border-radius: 14px;
padding: 24px;
box-shadow: 0 4px 32px rgba(0, 0, 0, 0.32);
display: flex;
flex-direction: column;
border: 1.5px solid #232936;
transition: box-shadow 0.2s, border 0.2s;
position: relative;
overflow: hidden;
z-index: 1;
}
.box:hover {
box-shadow: 0 8px 48px 0 rgba(0, 0, 0, 0.76);
border: 1.5px solid #3a3f4b;
}
.up-gradient {
background: linear-gradient(to top, #b1495a, #c71a44);
/* 背景被裁剪成文字的前景色。 */
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
/* 文字透明填充 */
text-fill-color: transparent;
}
.down-gradient {
background: linear-gradient(to bottom, #b1495a, #c71a44);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
/* 文字透明填充 */
text-fill-color: transparent;
}
.left-gradient {
background: linear-gradient(to left, #b1495a, #c71a44);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
/* 文字透明填充 */
text-fill-color: transparent;
}
.right-gradient {
background: linear-gradient(to right, #b1495a, #c71a44);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
/* 文字透明填充 */
text-fill-color: transparent;
}
/* 多顏色漸變 */
.multi-gradient {
background: linear-gradient(90deg, #b1495a 10%, #c71a44 50%, #ffb86c 80%);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
/* 文字透明填充 */
text-fill-color: transparent;
}
index.html
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>文字漸變</title>
<link rel="stylesheet" href="index.css" rel="external nofollow" rel="external nofollow" />
</head>
<body>
<div class="container">
<h1>前端實現(xiàn)文字漸變的幾種方式</h1>
<!-- css版本 -->
<article class="panel">
<div class="panel-box-title">CSS版:</div>
<div class="box">
<div class="content-text up-gradient">向上漸變</div>
<div class="content-text down-gradient">向下漸變</div>
<div class="content-text left-gradient">向左漸變</div>
<div class="content-text right-gradient">向右漸變</div>
<!-- 設(shè)置多個顏色 -->
<div class="content-text multi-gradient">多顏色漸變</div>
</div>
</article>
<!-- canvas版本 -->
<article class="panel">
<div class="panel-box-title">Canvas版:</div>
<div class="box">
<canvas id="canvas" height="180" width="900"></canvas>
</div>
</article>
<!-- svg版本 -->
<article class="panel">
<div class="panel-box-title">SVG版:</div>
<div class="box">
<svg width="900" height="180" xmlns="http://www.w3.org/2000/svg">
<defs>
<!-- 從左到右漸變 -->
<linearGradient
id="leftToRight"
x1="0%"
y1="0%"
x2="100%"
y2="0%"
>
<stop
offset="0%"
style="stop-color: #b1495a; stop-opacity: 1"
/>
<stop
offset="100%"
style="stop-color: #c71a44; stop-opacity: 1"
/>
</linearGradient>
<!-- 從上到下漸變 -->
<linearGradient
id="topToBottom"
x1="0%"
y1="0%"
x2="0%"
y2="100%"
>
<stop
offset="0%"
style="stop-color: #b1495a; stop-opacity: 1"
/>
<stop
offset="100%"
style="stop-color: #c71a44; stop-opacity: 1"
/>
</linearGradient>
<!-- 從右到左漸變 -->
<linearGradient
id="rightToLeft"
x1="100%"
y1="0%"
x2="0%"
y2="0%"
>
<stop
offset="0%"
style="stop-color: #b1495a; stop-opacity: 1"
/>
<stop
offset="100%"
style="stop-color: #c71a44; stop-opacity: 1"
/>
</linearGradient>
<!-- 從下到上漸變 -->
<linearGradient
id="bottomToTop"
x1="0%"
y1="100%"
x2="0%"
y2="0%"
>
<stop
offset="0%"
style="stop-color: #b1495a; stop-opacity: 1"
/>
<stop
offset="100%"
style="stop-color: #c71a44; stop-opacity: 1"
/>
</linearGradient>
</defs>
<!-- 從左到右漸變文字 -->
<text
x="20"
y="40"
font-family="Arial"
font-size="32"
font-weight="bold"
fill="url(#leftToRight)"
>
SVG 從左到右漸變
</text>
<!-- 從上到下漸變文字 -->
<text
x="20"
y="80"
font-family="Arial"
font-size="32"
font-weight="bold"
fill="url(#topToBottom)"
>
SVG 從上到下漸變
</text>
<!-- 從右到左漸變文字 -->
<text
x="20"
y="120"
font-family="Arial"
font-size="32"
font-weight="bold"
fill="url(#rightToLeft)"
>
SVG 從右到左漸變
</text>
<!-- 從下到上漸變文字 -->
<text
x="20"
y="160"
font-family="Arial"
font-size="32"
font-weight="bold"
fill="url(#bottomToTop)"
>
SVG 從下到上漸變
</text>
</svg>
</div>
</article>
</div>
</body>
<script>
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
ctx.font = '32px Arial'
// 從左到右的漸變文字
const leftToRightGradient = ctx.createLinearGradient(0, 0, canvas.width, 0)
leftToRightGradient.addColorStop(0, '#fff')
leftToRightGradient.addColorStop(1, '#000')
ctx.fillStyle = leftToRightGradient
ctx.fillText('Canvas 從左到右漸變', 20, 40)
// 從上到下的漸變文字
const topToBottomGradient = ctx.createLinearGradient(0, 0, 0, canvas.height)
topToBottomGradient.addColorStop(0, '#fff')
topToBottomGradient.addColorStop(1, '#000')
ctx.fillStyle = topToBottomGradient
ctx.fillText('Canvas 從上到下漸變', 20, 80)
// 從右到左的漸變文字
const rightToLeftGradient = ctx.createLinearGradient(canvas.width, 0, 0, 0)
rightToLeftGradient.addColorStop(0, '#fff')
rightToLeftGradient.addColorStop(1, '#000')
ctx.fillStyle = rightToLeftGradient
ctx.fillText('Canvas 從右到左漸變', 20, 120)
// 從下到上的漸變文字
const bottomToTopGradient = ctx.createLinearGradient(0, canvas.height, 0, 0)
bottomToTopGradient.addColorStop(0, '#fff')
bottomToTopGradient.addColorStop(1, '#000')
ctx.fillStyle = bottomToTopGradient
ctx.fillText('Canvas 從下到上漸變', 20, 160)
</script>
</html>
結(jié)尾
日常開發(fā)中還是css版本的比較常用。另外兩種,只有在特定環(huán)境下才有用。
相關(guān)文章
JavaScript重定向URL參數(shù)的兩種方法小結(jié)
關(guān)于JavaScript重定向URL參數(shù)的實現(xiàn)方法網(wǎng)站有很多,這篇文章的主要內(nèi)容是從網(wǎng)上查找,并進(jìn)行了修改,簡單粗暴的實現(xiàn)使用JavaScript重置url參數(shù),文中給出了詳細(xì)的示例代碼和調(diào)用代碼,對大家的理解和學(xué)習(xí)很有幫助,感興趣的朋友們下面來一起看看吧。2016-10-10
基于javascript實現(xiàn)根據(jù)身份證號碼識別性別和年齡
這篇文章主要介紹了基于javascript實現(xiàn)根據(jù)身份證號碼識別性別和年齡的相關(guān)資料,需要的朋友可以參考下2016-01-01
動態(tài)加載iframe時get請求傳遞中文參數(shù)亂碼解決方法
這篇文章主要介紹了動態(tài)加載iframe時get請求傳遞中文參數(shù)亂碼解決方法,需要的朋友可以參考下2014-05-05
新手學(xué)習(xí)前端之js模仿淘寶主頁網(wǎng)站
淘寶網(wǎng)大家在熟悉不過了,那么淘寶網(wǎng)首頁模板是怎么做的呢?今天小編抽時間給大家分享新手學(xué)習(xí)前端之js模仿淘寶主頁網(wǎng)站的相關(guān)資料,需要的朋友可以參考下2016-10-10
setTimeout和setInterval的區(qū)別你真的了解嗎?
setTimeout和setInterval這兩個函數(shù), 大家肯定都不陌生, 但可能并不是每個用過這兩個方法的同學(xué), 都了解其內(nèi)部的實質(zhì)2011-03-03
flash調(diào)用js中的方法,讓js傳遞變量給flash的辦法及思路
前幾天發(fā)表了 將FlashVars寫在JS函數(shù)中,實現(xiàn)與后臺的實時變量更新,但是僅支持 IE,隨后與 Luckyer 進(jìn)行了交流,發(fā)現(xiàn)用 SetVariable 可以很方便的實現(xiàn)多瀏覽器兼容。舉例如下。2013-08-08

