使用 CSS 仅更改单个字符的字体大小
是否可以使用 CSS 更改特定字符的大小?
例如,在一个段落中我使用 font-face: Arial;字体大小:12pt;
。我希望 10 号尺寸中只出现字母“a”。
有办法做到这一点吗?
谢谢。
Is it possible to change the size of a specific character using CSS?
For example, in a paragraph I am using font-face: Arial; font-size: 12pt;
. I would like that only the letter "a" would appear in size 10.
Is there a way to do that?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
否。除了
:first-letter
和其他伪类,您无法使用 CSS 定位单个字符。您需要将字符包装到一个元素(例如)中以为其指定样式。
您可以使用 Javascript 解决这个问题 - SO 上有基于 jQuery 的解决方案。但这很糟糕。
No. Except for
:first-letter
and other pseudo-classes, you can't target single characters using CSS. You'd need to wrap the character into an element (e.g. a<span>
) to specify a style for it.You can work around this using Javascript - there are jQuery based solutions for this here on SO. But it's kludgy.
我不相信你可以,考虑使用文本编辑器用
a
查找/替换 'a',然后你可以使用css来做.a-xxx { font-size: 10px; }
I don't believe you can, consider using a text editor to do a find/replace 'a' with
<span class='a-xxx'>a</span>
and then you can use css to do.a-xxx { font-size: 10px; }
抱歉挖出这个 11 年前的帖子,但我现在也遇到了这个问题。
我的用例是在特定网站上使藏文字符变大,因为与相同字体大小的拉丁字符相比,它们几乎难以辨认。
据我了解,这里的所有答案都已过时,因为我发现 @font-face css at-rule 涵盖了这一点。它接受 Unicode 范围,因此也应该适用于单个字符。所有现代浏览器都支持。
因此,我需要做的就是将以下内容添加到我的 css 中,这将定义一个名为 'YangpoTibetanUni' 的新字体(当然,根据您的喜好修改 url 参数):
然后使用您的像这样新定义的字体:
好的,替换整个字体是一回事,但是如何使一个字符变大呢? @font-face 还接受 尺寸调整 参数(注意!这个参数在 Safari 中不起作用,但还有其他参数,例如 font-stretch - 看看哪个参数适合您的需求):
因此,使 @font-face url 指向原始 URL 并操作大小调整值(或其他参数,根据文档)。
Sorry for digging up this 11 year old thread, but I just now ran into this problem as well.
My use case was to make Tibetan characters bigger on a specific website, because they were barely readable compared to latin characters of the same font size.
As I understand, all the answers here are outdated, as I found the @font-face css at-rule that covers this. It accepts a Unicode range, so should work for a single character as well. Supported by all modern browsers.
So all I needed to do is add the following to my css, which will define a new font called 'Yangpo Tibetan Uni' (of course, modify the url parameter to your liking):
And then use your newly defined font like so:
OK, replacing the whole font is one thing, but how to make one character bigger? @font-face also accepts size-adjust parameter (BEWARE!!! This one parameter will not work in Safari, but there are others like font-stretch - take a look what fits your needs):
So make the @font-face url point to the original and manipulate the size-adjust value (or other parameters, as per docs).
不可以。您只能定位元素(例如包含单个字母的范围)和伪元素(例如
:first-letter
)。No. You can only target elements (such as a span that contains a single letter) and pseudo-elements (such as
:first-letter
).如果没有 JavaScript,您无法以跨浏览器一致且简单的方式完成此操作。
我推荐 jquery 的“captify”。
You can't do this in a cross-browser-consistent and simple way without javascript.
I recommend 'captify' for jquery.
另外,为了可访问性和兼容性以及所有这些,最好不要定义特定字体(尝试字体系列)和大小,然后在其上使用 % ,并将它们全部定义为自定义 span/div 样式,
例如
bigletter (字体大小:150%);
Also for accessibility and compatibility and all that it is best not to define specific fonts (try font-family) and sizes in terms of large, larger then use % ontop of that, and define them all as custom span/div styles
e.g
bigletter (font-size:150%);