一种解决方案是考虑调整大小/位置的渐变着色以获得所需的着色。缺点是您需要正确找到将根据您要定位的字符和字体属性而改变的不同值:
.redBg {
background:
linear-gradient(red,red) left/23px 100% no-repeat;
}
.blueBg {
background:
linear-gradient(green,green) 40px 0/32px 100% no-repeat;
}
.redColor {
background:
linear-gradient(red,red) left/23px 100% no-repeat,
#000;
background-clip: text;
color: transparent;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.blueColor {
background:
linear-gradient(green,green) 40px 0/32px 100% no-repeat,
#000;
background-clip: text;
color: transparent;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
div {
font-size: 26pt;
display:inline-block;
}
<div class="redBg">
کلمات به هم پیوسته</div>
<br>
<div class="blueBg">
کلمات به هم پیوسته</div>
<br>
<div class="redColor">
کلمات به هم پیوسته</div>
<br>
<div class="blueColor">
کلمات به هم پیوسته</div>
您还可以轻松扩展到多个背景,以便在同一个句子中定位更多字符:
.redBg {
background:
linear-gradient(red,red) left/23px 100%,
linear-gradient(pink,pink) 80px 0/25px 100%;
background-repeat:no-repeat;
}
.blueBg {
background:
linear-gradient(green,green) 40px 0/32px 100% no-repeat;
}
.redColor {
background:
linear-gradient(red,red) left/23px 100%,
linear-gradient(blue,blue) right/45px 100%,
#000;
background-repeat:no-repeat;
background-clip: text;
color: transparent;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.blueColor {
background:
linear-gradient(green,green) 40px 0/32px 100% no-repeat,
#000;
background-clip: text;
color: transparent;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
div {
font-size: 26pt;
display:inline-block;
}
<div class="redBg">
کلمات به هم پیوسته</div>
<br>
<div class="blueBg">
کلمات به هم پیوسته</div>
<br>
<div class="redColor">
کلمات به هم پیوسته</div>
<br>
<div class="blueColor">
کلمات به هم پیوسته</div>
另一个想法是复制文本,您可以轻松地应用所需样式并使用溢出剪切其中一个文本(使用此方法不会换行)
.redColor:after {
color:red;
width:25px;
}
.blueColor:after {
color:blue;
width:30px;
text-indent:-42px;
left:42px;
}
div {
font-size: 26pt;
display:inline-block;
position:relative;
}
div:before,
div:after{
content:attr(data-text);
}
div:after {
position:absolute;
top:0;
left:0;
white-space:nowrap;
overflow:hidden;
background:#fff;
}
<div class="redColor" data-text=" کلمات به هم پیوسته">
</div>
<br>
<div class="blueColor" data-text=" کلمات به هم پیوسته">
</div>