html - 在上下文字符上应用样式会破坏单词

标签 html css arabic farsi

<分区>

我在阿拉伯语和波斯语字符方面遇到问题,因为它们具有上下文(连接)字符,当我想指示关键字时,包含该关键字的单词会分解为更小的单词。我的问题是如何在不破坏主词的情况下对关键字应用 css 样式?

正如您在以下示例中看到的,当我在其上应用样式时,字符 des 与下一个字符分开。

.redColor{color:red}
.redBg{background:red}
div{font-size:26pt}
<div>
کلمات به هم پیوسته
<br>
کلمات به هم پیوس<span class="redColor">ته</span>
<br>
کلمات به هم پیوس<span class="redBg">ته</span>
</div>

最佳答案

一种解决方案是考虑调整大小/位置的渐变着色以获得所需的着色。缺点是你需要正确地找到不同的值,这些值会根据你想要定位的字符和字体属性而改变:

.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>

关于html - 在上下文字符上应用样式会破坏单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55197540/

相关文章:

javascript - 如何在 JavaScript 中使用日期和年份的英文数字将日期格式化为阿拉伯语?

javascript - &lt;script/> 与 &lt;script&gt;&lt;/script&gt; 使用 webpack 和 Angular

html - 按钮的渐变不起作用

html - 位置 div margin top 30px 但使用 margin auto 将 margin 保持在中心?

html - 为什么html元素只在所有css文件加载完成后才显示

android - 如何在 Android 中显示阿拉伯文文本?

html - 垂直对齐在 td 内部不起作用

html - 设置高度后,FireFox 3.5 上 HTML 输入中的文本保持在顶部

html - 悬停效果使导航栏过渡的背景矩形向上

android - 带有 Android ICS 的 Galaxy note 1 上未以阿拉伯语显示 EditText 提示