Using CSS, is there any way to display different characters using different font?

Go To StackoverFlow.com

5

My HTML text is just like this:

<p>abcdefghijklmnopqrstuvwxyz</p>

What I want is to display a-n using "Times New Roman", and display o-z using "Courier New", and this should be done using CSS, say, with no change to the HTML text.

Simply stated, I want CSS to automatically choose the specified font corresponding to which character it is.
a-n should be displayed using "Times New Roman";
o-z shoule be displayed using "Courier New".

Is there any way to accomplish this?

If this problem can be solved, another problem can be solved: display different language using different font.

2012-04-05 02:36
by Yishu Fang


7

Looks like you can, at least in some browsers, using something called unicode-range Welll, this Works in Chrome, and surprisingly, IE. No such luck with Firefox and Opera.

BTW, more info about this from http://24ways.org/2011/unicode-range

Live example: http://jsfiddle.net/jfcox/3LQyr/

<style>
@font-face {
    font-family: Foobar;
    src: local('Times New Roman');
    unicode-range: U+61-6E;
}
@font-face {
    font-family: Foobar;
    src: local('Arial');
    unicode-range: U+6F-7A;
}
body{
  font-family:Foobar;
}
</style>
<p>abcdefghijklmnopqrstuvwxyz</p>​
2012-04-05 03:13
by JayC
oops, not the fonts asked... well I did give you a JSFiddle to mess with :- - JayC 2012-04-05 03:15
Thank you, very much. Will this feature be supported with all the browser in recent future - Yishu Fang 2012-04-05 03:41
It's also mentioned in the W3C Working Draft 4 October 2011 (http://www.w3.org/TR/css3-fonts/ ), so there are attempts to make it standard. Will it be a standard, of course, is another issue, and I can't predict the future, but I'd call the inclusion a good indication - JayC 2012-04-05 13:49


3

If the characters belong to different writing systems, such as Latin and Hebrew, or Cyrillic and Greek, browsers often automatically use different fonts for them. However, this only happens to the extent that the page does not specify fonts, i.e. this relates to default fonts only, and the fonts used are determined by browser defaults and browser settings controlled by the user.

Although the technique described in JayC’s answer gives a partial solution, you get much better browser coverage by distinguishing the texts in different languages in markup. In a bilingual document, it suffices to use markup for texts in one of them (the one used less, for practical reasons). Using class as in gutch’s answer gives best coverage, but nowadays support to language selectors in CSS is so widespread that you might consider using the more logical lang attribute instead, e.g.

<h1>Hello − <a lang=ru>Привет</а></h1>

Then you would use rules like

[lang=ru] { font-family: ...; }

(My example uses an <a> element effectively as a shorter variant of <span>. Formally, this is possible only when the text is not inside an outer <a> element.)

However, for visual style, just the opposite of font selection by language would be needed. It really looks odd if the “e” in “Hello” is different from the Cyrillic “е” in “Привет” on the same line. It is almost always better to use the same font for all languages in a document, if possible. This means selecting a font that works for all of them.

2012-04-05 04:33
by Jukka K. Korpela


1

You can't use CSS to change the font of particular characters as you describe, because the CSS selectors don't select individual characters — they select HTML elements.

So you would need to create elements around the blocks of text that need specific fonts. Ideally you would do that in server-side code, though I don't know whether that's practical for you. Your server would need to output HTML like this:

<p><span class="languageOne">abcdefghijklmn</span><span class="languageTwo">opqrstuvwxyz</span></p>

Then you apply the fonts as appropriate in your CSS:

.languageOne { font-family: "Times New Roman", serif; }
.languageTwo { font-family: "Courier New", monospace; }
2012-04-05 02:57
by gutch
I know this way, but I just want CSS to do this, not HTML, or the HTML text will looks ugly,but thank you all the same. It's a small static site and I write all the HTML text by myself. : - Yishu Fang 2012-04-05 03:12
Sometimes the reality of web development is that you have to accept some ugliness just to get things working! :- - gutch 2012-04-05 05:08
Ads