外链论坛

 找回密码
 立即注册
搜索
查看: 80|回复: 1

CSS 实现文字颜色渐变的 3 种办法

[复制链接]

2587

主题

5122

回帖

9957万

积分

论坛元老

Rank: 8Rank: 8

积分
99570685
发表于 2024-6-30 08:15:05 | 显示全部楼层 |阅读模式

近期博主的老大弥雅在研发运用的 wordpress 主题的时候,打算部分元素中文字采用 CSS 实现颜色渐变。在查阅有些资料和求助有些前端大佬后总结出比较实用的三个办法此刻博主就分享给大众

办法

bac公斤round-image: -webkit-linear-gradient(...) 为文本元素供给渐变背景。 -webkit-text-fill-color: transparent 运用透明颜色填充文本。 -webkit-bac公斤round-clip: text 用文本剪辑背景,用渐变背景做为颜色填充文本。 缺点:webkit 内核浏览器特有
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>渐变色</title> <style> .box { position: relative; text-align: left; text-indent:30px; line-height: 50px; font-size: 40px; font-weight: bolder; bac公斤round-image: -webkit-linear-gradient(bottom, red, #fd8403, yellow); -webkit-bac公斤round-clip: text; -webkit-text-fill-color: transparent; } </style> </head> <body> <div> Hello World ~ </div> </body> </html>

办法

运用:mask-image

缺点:webkit 内核浏览器特有
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>渐变色</title> <style> .box { position: relative; text-align: left; text-indent:30px; line-height: 50px; font-size: 40px; font-weight: bolder; color: red; -webkit-mask-image: -webkit-gradient(linear, 0 0, 0 bottom, from(yellow), to(rgba(0, 0, 255, 0))); } </style> </head> <body> <div> Hello World ~ </div> </body> </html>

办法

采用 svg 方式

<html> <head> <meta charset="UTF-8"> <title>渐变色</title> <style> .box-text { fill: url(#SVGID_1_); font-size: 40px; font-weight: bolder; } </style> </head> <body> <svg viewBoxs="0 0 500 300"> <defs> <linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="0" y1="10" x2="0" y2="50"> <stop offset="0" style="stop-color: yellow"/> <stop offset="0.5" style="stop-color: #fd8403"/> <stop offset="1" style="stop-color: red"/> </linearGradient> </defs> <text text-anchor="middle" x="110px" y="30%">盖世神功</text> </svg> </body> </html>
回复

使用道具 举报

7

主题

472

回帖

-21

积分

限制会员

积分
-21
发表于 2024-9-1 19:49:42 | 显示全部楼层
请问、你好、求解、谁知道等。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

站点统计|Archiver|手机版|小黑屋|外链论坛 ( 非经营性网站 )|网站地图

GMT+8, 2024-10-4 06:31 , Processed in 0.064703 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.