Tutorials Bugs Masterclass Free stuff Test pages Proposals

Basic concepts

Advanced concepts

RichInStyle.com CSS2 tutorial - @font-face

Contents

@font-face

Font selection properties

Font-family

Font-stretch, font-size, font-weight, font-stretch, font-style, font-variant

Font retrieval properties

src

unicode-range

Descriptor needed for font synthesis, matching and alignment

units-per-em

Font matching descriptors

panose-1

stemv and stemh

slope

cap-height

x-height

ascent

descent

Font synthesis descriptors

widths

bbox

definition-src

Font alignment descriptors

baseline

centerline

mathline

topline

@font-face

The @font-face at-rule is used for a variety of purposes, but essentially it is used to specify the correct behavior when a font is not available locally.

In general, the @font-face at-rule is used for this purpose. This follows a form similar to this @font-face {property: value; property: value}.

There may be any number of @font-face rules in a stylesheet.

Font selection properties

font-family

This specifies the name of a font that you are describing. For example, @font-face {font-family: Arial}. Multiple font families can be specified; e.g., @font-face {font-family: sans-serif, Arial}. It takes the same values as

font-style, font-size, font-weight, font-stretch, font-variant

This specifies the type of font that is being described.

They take the same values as their equivalent normal properties, except they can take a comma-separated list.

For font-style, font-size, font-stretch and font-weight, however, the 'all' value is also valid, which indicates that all values are matched.

However, values that relate to another value are not valid (e.g., smaller and bolder), and neither are the font size keywords 'large', 'small', etc.

E.g., font-weight: 100, 300, 500, 600, 700, 800, 900.

The initial value for font-size, font-weight and font-style is all. For font-variant and font-stretch it is normal.


Font retrieval properties

If it is important that a specific font is used in viewing your document, you should use the properties that specify its location and characteristics.

src

The src property specifies the location of a font: It follows this basic syntax: src: url(address1). Thus to be specify that you can find the 'foo' font at www.foo.com, you would use @font-face {font-family: foo; src: url(www.foo.com)}.

If you wish, you can specify the format of the font. E.g., @font-face {font-family: foo; src: url(http://www.foo.com) format("footype")}. To be specify several uris for browsers that support different font types, you separate them with commas. E.g., @font-face {font-family: foo; src: url(http://www.foo.com) format("footype"), src(http://www.foo2.com) format("foo4")}.

Note that the format must be in quotes.

Finally, you can specify the name of a local font to match. E.g., to specify that when an element is specified as being in Times, Times New Roman should be used, you would type:

@font-face {font-family: Times;
src: local("Times New Roman")}

Note again that the font name must be in quotes, even if it doesn't include whitespace.

Here's another example, which specifies the location of italic Foo.

@font-face {font-family: Foo;
src: url(http://www.foo.com);
font-style: italic}

Since font-size is initially all, the font will be used at all sizes; but will not be used for small-caps or stretched text, since font-variant and font-stretch are initally set to normal.

unicode-range property

The unicode-range is used to ensure that fruitless downloads are not made. It specifies the characters that are matched by glyphs in the font.

The syntax of the unicode-range is the literal string 'U+' followed by a hexadecimal number.

Wildcard characters ? (indicates any value for that digit) and * (indicates any number of digits with any value) are permissible. Thus U+12?? matches 12AC or 1209, etc.

To specify a range, a hyphen is used - e.g., U+1200-1210. The first number is the start of the range, the second the end of the range.

To specify several unicode ranges, you separate them with commas - e.g., U+12??, U+1400-1500 matches 1200-12FF and 1400-1500.

A typical declaration is unicode-range: U+0-1F, U+10CF*.

Hexadecimal numbers refer to codes in ISO 10646.

The initial value for unicode-range is U+0-7FFFFFFF.


Descriptor needed for font synthesis, matching and alignment

units-per-em

In order that numeric descriptors have meaning, it is necessary to specify the number of units that the em square is divided into. Typical values include 2048 (Truetype) and 250 (Intellifont).

Once declared, a unit that requires it will divide by this value to get the true measurement. E.g., stemh: 200 would divided by units-per-em to get the true height.


Font matching descriptors

If you want intelligent font matching or font size adjustment, you need to specify technical details about the font. Here are the properties that are used:

panose-1

This can be followed by exactly 10 space-separated decimal integers. This indicates the font's panose number for font matching. It is initially set to panose-1: 0 0 0 0 0 0 0 0 0 0. This initial value matches all fonts. The panose-1 descriptor allows you to ensure that a font similar to the one you want is used. Its use is highly recommended, since it generally results in similar fonts. However, not all fonts contain panose information.

stemv and stemh

This indicates the font's vertical (stemv) and horizontal (stemh) stem width as a number. If declared, units-per-em must be declared. This will allow intelligent font size adjustment.

Their initial value is undefined.

slope

This specifies the vertical slope angle, and is intially set to 0.

cap-height

The cap-height descriptor specifies the height of uppercase glyphs. Its initial value is undefined. If declared, units-per-em must be declared.

x-height

The x-height (essential if font-size-adjust is used) descriptor specifies the x-height of the font. If declared, units-per-em must be declared. Its initial value is undefined.

ascent

The ascent descriptor specifies the maximum unaccented height of the font. If declared, units-per-em must be declared. Its initial value is undefined.

descent

The descent descriptor specifies the maximum unaccent depth of the font. If declared, units-per-em must be declared. Its initial value is undefined.


Font synthesis descriptors

Since fonts can be very large, when you specify a remote font, it is preferable to use a synthesized font. Crucial to this are the widths and bbox descriptors. These descriptors are required to avoid reflow.

Further to this, the font matching descriptors may be used to give a better appearance of the true font.

widths

Here you specify a unicode range followed by a number for the widths of the glyph. E.g., widths: U+1* 1490. Or widths: U+1-3 1200 1456 1357, U+1? 1167.

If not enough glyph widths are provided for the list, the last in the list is assumed to cover all omitted values. E.g., widths: U+1200-120A 1700 1701 1702 would give the glyph for the character corresponding to hex 1200 a width of 1700, that with hex 1201 1701, and 1202-120A a width of 1702.

Note that it is only of use when a given character only has one matching glyph in the font.

If declared, units-per-em must be declared.

Widths' initial value is undefined.

bbox

This is used to specify the maximal bounding box of the font. It is exactly four comma-delimited numbers specifying lower left x, lower left y, upper right x, upper right y in that order of the font.

Bbox's initial value is undefined.

definition-src

This provides the url of a separate font definition resource so that if multiple stylesheets use the same definition, download time is reduced. E.g., definition-src: url(url.url).


Font alignment descriptors

In order that different scripts can be aligned with each other, these descriptors are used.

baseline

This is specified as a number, initially 0 that describes the lower baseline (used for alignment in Latin, Greek and Cyrillic fonts) of a font. It is the position in the em square, and as such if declared, so must units-per-em.

centerline

This defines the central baseline (used for alignment in ideographic scripts) of the font, as a number. It is the position in the em square, and as such if declared, so must units-per-em.

Its initial value is undefined, but approximations to it may be used if undeclared.

mathline

This defines the mathematical baseline (used for alignment in mathematical fonts) of the font. It is the position in the em square, and as such if declared, so must units-per-em.

Its initial value is undefined, but approximations to it may be used if undeclared.

topline

This defines the top baseline of a font (used for alignment in Sanscrit-based fonts). It is the position in the em square, and as such if declared, so must units-per-em.

Its initial value is undefined, but approximations to it may be used if undeclared.

The next section deals with aural style sheets.