使用 CSS 仅更改单个字符的字体大小

发布于 2024-09-15 10:53:25 字数 142 浏览 4 评论 0原文

是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

东走西顾 2024-09-22 10:53:25

否。除了 :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.

眼藏柔 2024-09-22 10:53:25

我不相信你可以,考虑使用文本编辑器用 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; }

瞄了个咪的 2024-09-22 10:53:25

抱歉挖出这个 11 年前的帖子,但我现在也遇到了这个问题。

我的用例是在特定网站上使藏文字符变大,因为与相同字体大小的拉丁字符相比,它们几乎难以辨认。

据我了解,这里的所有答案都已过时,因为我发现 @font-face css at-rule 涵盖了这一点。它接受 Unicode 范围,因此也应该适用于单个字符。所有现代浏览器都支持。

因此,我需要做的就是将以下内容添加到我的 css 中,这将定义一个名为 'YangpoTibetanUni' 的新字体(当然,根据您的喜好修改 url 参数):

@font-face {
  font-family: 'Yangpo Tibetan Uni';
  src: url("./util/fonts/YagpoTibetanUni-x3jnj.ttf") format("truetype");
  unicode-range: U+0F00-0FFF;
}

然后使用您的像这样新定义的字体:

body {
  font-family: /* main font */ 'Raleway', /* and then your override */ 'Yangpo Tibetan Uni';
}

好的,替换整个字体是一回事,但是如何使一个字符变大呢? @font-face 还接受 尺寸调整 参数(注意!这个参数在 Safari 中不起作用,但还有其他参数,例如 font-stretch - 看看哪个参数适合您的需求)

大小调整 CSS 描述符定义了与该字体关联的字形轮廓和规格的乘数。这使得在以相同字体大小渲染时更容易协调各种字体的设计。

因此,使 @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):

@font-face {
  font-family: 'Yangpo Tibetan Uni';
  src: url("./util/fonts/YagpoTibetanUni-x3jnj.ttf") format("truetype");
  unicode-range: U+0F00-0FFF;
}

And then use your newly defined font like so:

body {
  font-family: /* main font */ 'Raleway', /* and then your override */ 'Yangpo Tibetan Uni';
}

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):

The size-adjust CSS descriptor defines a multiplier for glyph outlines and metrics associated with this font. This makes it easier to harmonize the designs of various fonts when rendered at the same font size.

So make the @font-face url point to the original and manipulate the size-adjust value (or other parameters, as per docs).

韬韬不绝 2024-09-22 10:53:25

不可以。您只能定位元素(例如包含单个字母的范围)和伪元素(例如 :first-letter)。

No. You can only target elements (such as a span that contains a single letter) and pseudo-elements (such as :first-letter).

篱下浅笙歌 2024-09-22 10:53:25

如果没有 JavaScript,您无法以跨浏览器一致且简单的方式完成此操作。
我推荐 jquery 的“captify”。

You can't do this in a cross-browser-consistent and simple way without javascript.
I recommend 'captify' for jquery.

凉城凉梦凉人心 2024-09-22 10:53:25

另外,为了可访问性和兼容性以及所有这些,最好不要定义特定字体(尝试字体系列)和大小,然后在其上使用 % ,并将它们全部定义为自定义 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%);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文