是否有生成器,或生成像this这样的文本的简单方法,但无需定义每个字母
所以像这样:
.rainbow {
background-image: -webkit-gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
color:transparent;
-webkit-background-clip: text;
background-clip: text;
}
<span class="rainbow">Rainbow text</span>
但不是彩虹色而是用其他颜色生成(例如白色到灰色/浅蓝色渐变等)我找不到一个简单的解决方案。有解决方案吗
我不知道停止的东西是如何工作的。但我有一个渐变文本示例。也许这会帮助你!
_如果需要,您还可以为渐变添加更多颜色,或者只选择color generator中的其他颜色
.rainbow2 {
background-image: -webkit-linear-gradient(left, #E0F8F7, #585858, #fff); /* For Chrome and Safari */
background-image: -moz-linear-gradient(left, #E0F8F7, #585858, #fff); /* For old Fx (3.6 to 15) */
background-image: -ms-linear-gradient(left, #E0F8F7, #585858, #fff); /* For pre-releases of IE 10*/
background-image: -o-linear-gradient(left, #E0F8F7, #585858, #fff); /* For old Opera (11.1 to 12.0) */
background-image: linear-gradient(to right, #E0F8F7, #585858, #fff); /* Standard syntax; must be last */
color:transparent;
-webkit-background-clip: text;
background-clip: text;
}
.rainbow {
background-image: -webkit-gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
color:transparent;
-webkit-background-clip: text;
background-clip: text;
}
<span class="rainbow">Rainbow text</span>
<br />
<span class="rainbow2">No rainbow text</span>
这种效果的工作方式非常简单。元素的背景是渐变。它根据颜色和颜色停止百分比从一种颜色到另一种颜色。
例如,在彩虹文本示例中(请注意我已将渐变转换为标准语法):
#f22
的颜色0%
(即元素的左边缘)。始终假设第一种颜色从0%
开始,即使未明确提及百分比。0%
和14.25%
之间,颜色从#f22
逐渐变为#f2f
。 percenatge设置为14.25
,因为有七种颜色变化,我们正在寻找相等的分裂。14.25%
(容器的大小),颜色将根据指定的渐变精确地为#f2f
。14.25%
的一步。因此,我们最终获得了如下面的代码段中的渐变。现在这就意味着背景适用于整个元素而不仅仅是文本。
.rainbow {
background-image: linear-gradient(to right, #f22, #f2f 14.25%, #22f 28.5%, #2ff 42.75%, #2f2 57%, #2f2 71.25%, #ff2 85.5%, #f22);
color: transparent;
}
<span class="rainbow">Rainbow text</span>
由于渐变只需要应用于文本而不是整体应用于元素,因此我们需要指示浏览器剪切文本外部区域的背景。这是通过设置background-clip: text
来完成的。
(请注意,background-clip: text
是一个实验性属性,并且不受广泛支持。)
现在,如果您希望文本具有简单的3色渐变(即红色 - 橙色 - 棕色),我们只需要更改线性渐变规范,如下所示:
to right
方向。如果它应该是右边的红色和左边的棕色那么给出方向为to left
。red
指定为第一种颜色(假设百分比为0%)。50%
颜色应该是orange
然后最终的颜色将是brown
。最终颜色的位置始终假定为100%。因此,梯度的规范应如下所示:
background-image: linear-gradient(to right, red, orange 50%, brown).
如果我们使用上述方法形成渐变并将它们应用于元素,我们就可以获得所需的效果。
.red-orange-brown {
background-image: linear-gradient(to right, red, orange 50%, brown);
color: transparent;
-webkit-background-clip: text;
background-clip: text;
}
.green-yellowgreen-yellow-gold {
background-image: linear-gradient(to right, green, yellowgreen 33%, yellow 66%, gold);
color: transparent;
-webkit-background-clip: text;
background-clip: text;
}
<span class="red-orange-brown">Red to Orange to Brown</span>
<br>
<span class="green-yellowgreen-yellow-gold">Green to Yellow-green to Yellow to Gold</span>
你可以使用CSS linear-gradient和mix-blend-mode的组合来实现这种效果。
HTML
<p>
Enter your message here...
To be or not to be,
that is the question...
maybe, I think,
I'm not sure
wait, you're still reading this?
Type a good message already!
</p>
CSS
p {
width: 300px;
position: relative;
}
p::after {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(45deg, red, orange, yellow, green, blue, purple);
mix-blend-mode: screen;
}
这样做是在段落的::after
伪元素上添加一个线性渐变,并使其覆盖整个段落元素。但是使用mix-blend-mode: screen
时,渐变仅显示在有文本的部分。
这是一个jsfiddle在工作中显示这一点。只需修改linear-gradient
值即可实现您想要的效果。
CSS文本渐变的示例
background-image: -moz-linear-gradient(top,#E605C1 0%,#3B113B 100%);
background-image: -webkit-linear-gradient(top,#E605C1 0%,#3B113B 100%);
background-image: -o-linear-gradient(top,#E605C1 0%,#3B113B 100%);
background-image: -ms-linear-gradient(top,#E605C1 0%,#3B113B 100%);
background-image: linear-gradient(top,#E605C1 0%,#3B113B 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
position:relative;
display:inline-block; /*required*/
在线发电机textgradient.com
body{ background:#3F5261; text-align:center; font-family:Arial; }
h1 {
font-size:3em;
background: -webkit-linear-gradient(top, gold, white);
background: linear-gradient(top, gold, white);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
position:relative;
margin:0;
z-index:1;
}
div{ display:inline-block; position:relative; }
div::before{
content:attr(data-title);
font-size:3em;
font-weight:bold;
position:absolute;
top:0; left:0;
z-index:-1;
color:black;
z-index:1;
filter:blur(5px);
}
<div data-title='SOME TITLE'>
<h1>SOME TITLE</h1>
</div>
.gradient_text_class{
font-size: 72px;
background: linear-gradient(to right, #ffff00 0%, #0000FF 30%);
background-image: linear-gradient(to right, #ffff00 0%, #0000FF 30%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
<div class="gradient_text_class">Hello</div>