针对不同语言更改元素的 CSS
有没有办法使用 CSS 根据语言设置文本样式?
例如,字体 Tahoma 广泛用于阿拉伯文本,但对于具有相同文本大小的英文文本来说,其尺寸非常小。
那么,当我们在阿拉伯语文本中包含一些英语文本时,有没有一种方法可以自动检测文本语言的变化,然后为阿拉伯语文本中的英语文本使用更大的字体大小。
谢谢。
Is there a way to use CSS to style text depending on the language?
For example the font Tahoma is widely used for Arabic text, but its size is very tiny when it comes to English text with the same text size.
So, when we have some English text within an Arabic text, is there a way to automatically detect the change in text language and then use a bigger font size for the English text that is withing the Arabic one.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CSS 没有执行此操作的机制,但您可以通过迭代 JavaScript 中的每个文本节点并不敏感地测试字符 AZ 来试试运气(
match(/^[az]+$/i)
(尽管这并不能保证文本将是英语)。如果为真,您可以将一个类附加到父元素,例如
appears-english
,然后就可以了。 CSS 类似.appears-english { font-size: 120% }
更新
根据请求,这里是一些代码...
JavaScript
CSS
jsFiddle。
从fiddle中可以看到,如果有数字则不匹配 - 你可以通过 将正则表达式中的
[az]
更改为\w
。顺便说一句,如果您想更新包含阿拉伯字符的元素中的英文字符,则需要使用
replace()
并将它们用span
元素包装,然后在其中添加出现-english
。CSS doesn't have a mechanism to do this, but you could try your luck by iterating over every text node in JavaScript and testing for characters A-Z insensitively (
match(/^[a-z]+$/i)
(though this doesn't guarantee the text will be English).If true, you could attach a class to the parent element, something like
appears-english
and then have CSS like.appears-english { font-size: 120% }
.Update
As per request, here is some code...
JavaScript
CSS
jsFiddle.
You can see from the fiddle it doesn't match if there are numbers - you can make it match numbers, letters and underscore by changing
[a-z]
in the regex to\w
.BTW, if you want to update English characters within an element that also contains Arabic characters, you will need to use
replace()
and wrap them withspan
elements, of which you then addappears-english
to.