最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set a font specifically for all numbers on the page - Stack Overflow

programmeradmin3浏览0评论

For my webpage, I chose a font that works well for all the letters. However, for all numbers I'd like to use a different font.

Is there a way that I can set a CSS rule to target all the numbers on the page?

If I can't do it strictly with CSS, my first thought is to use regular expressions to surround all numbers with a span with a "numbers" class and apply a font for that class. Is there a better way to do this?

For my webpage, I chose a font that works well for all the letters. However, for all numbers I'd like to use a different font.

Is there a way that I can set a CSS rule to target all the numbers on the page?

If I can't do it strictly with CSS, my first thought is to use regular expressions to surround all numbers with a span with a "numbers" class and apply a font for that class. Is there a better way to do this?

Share Improve this question asked Nov 28, 2012 at 17:54 dmrdmr 22.3k37 gold badges104 silver badges139 bronze badges 2
  • 1 I don't know if this will be useful, but have a look at it. johndoesdesign.com/blog/2012/jquery/… – Kakalokia Commented Nov 28, 2012 at 18:04
  • 1 You can do it with JavaScript as you've described, or you can create a custom font by replacing the number glyphs in your "letters" font with the number glyphs in your "numbers" font. – thirtydot Commented Nov 28, 2012 at 18:10
Add a comment  | 

2 Answers 2

Reset to default 21

You can do it in JavaScript relatively simply by traversing the document so that you wrap any sequence of digits in a span element with a class attribute and declare font-family for it in CSS.

It’s possible in principle in pure CSS, too, though only WebKit browsers currently support this:

@font-face {
  font-family: myArial;
  src: local("Courier New");
  unicode-range: U+30-39;
}
@font-face {
  font-family: myArial;
  src: local("Arial");
  unicode-range: U+0-2f, U+40-10FFFF;
}
body { font-family: myArial; }

Finally, and most importantly, don’t do it. If you are dissatisfied with digits in a font, don’t use that font.

Surrounding your numbers with a <span class="number"> sounds like a good, semantically sound approach.

In fact, CSS3 doesn't offer an alternative, and I don't see anything forthcoming in CSS4. If you look at the latest CSS3 recommendation, it doesn't specify any content selector, but the old 2001 candidate recommendation was the last version to provide the :contains() pseudoclass.

This would let you match an element that contained numbers:

/* Deprecated CSS3 */
p:contains(0), p:contains("1"), p:contains("2") {
    background-color: red;
}

Even if this were actually available, it matches the p, not the number, so the whole p would be styled...not what you wanted. The CSSWG is kicking around these ideas for formatting numerical content...still not what you wanted.

To apply CSS to the numbers, there is no avoiding some additional markup.

发布评论

评论列表(0)

  1. 暂无评论