XML technologies / CSS / Advanced websites with CSS / Using fonts with CSS / The property font-family in CSS
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
A font family is simply a group of fonts differing only in features such as weight or style. For example, the family Times contains bold and italic variants based on the regular font Times.
Using font families on web pages always raises the question of support. A whole section of this chapter addresses that issue, so I will touch on it only briefly here and return to it in detail later.
With font-family you can display individual HTML elements or entire page areas in chosen typefaces. The property accepts either a single value or a list of alternative fonts. Always end such a list with the generic family to which the preceding fonts belong. For example:
font-family: Helvetica, Arial, sans-serif;Fonts are always considered in the order listed. In the example above, the browser first tries Helvetica. If it is unavailable on the user’s system, it tries the next font, Arial. If that too is absent, it uses its default for the sans-serif family (sans-serif).
If a value in a list for font-family contains spaces, put it in single or double quotation marks; otherwise it will not be accepted:
font-family: "New Century Schoolbook", Baskerville, serif;In general, only list fonts from the same generic family. Mixing entirely different typefaces loses control of the text design and provides no basis for the rest of the page design.
Generic font families
CSS supports five generic families. A browser has a different preconfigured font for each, available on the user’s system.
serifsans-serifmonospacecursivefantasy
When a browser encounters a generic family, it substitutes a suitable font. On Windows, for example, it replaces serif with Times New Roman, sans-serif with Arial, and monospace with Courier New.
Serif fonts, such as Times New Roman, have small feet at the beginnings or ends of many strokes. Sans-serif fonts, such as Arial, have none; their letters are typically straight with uniformly thick strokes. In monospace fonts, every letter is equally wide, as on old typewriters. Cursive fonts suggest connected handwritten letters. A fantasy font is decorative and unusual.
Opinions differ on which fonts suit web pages best. Many believe serif fonts are more readable because the serifs guide the eye along the line. Others argue that sans-serif fonts are easier to read on a monitor. A discussion of that question is beyond this book’s scope.
The following figure shows the three most commonly used generic families and their obvious differences.
Figure: Comparing three generic fonts: sans-serif, serif and monospace.
Users can change default font assignments in browser settings. Anything may then happen: even the generic family sans-serif may no longer correspond to a sans-serif font. If a user overrides it with a monospace font, the browser uses that whenever it encounters the declaration font-family: sans-serif.
How the browser obtains its generic defaults does not matter. What matters is that it uses that font only if the fonts earlier in the list are unavailable.
Normally, a list contains fonts of one type (serif, sans-serif, monospace), but that is not mandatory. You may mix serif and sans-serif fonts and give a monospace default. I do not know why you would, but it is possible.
What is fantasy?
A good question with no simple answer.
Current CSS recommendations give no guidance on what generic fonts should look like, just a few simple examples. This is no real problem for the families serif, sans-serif and monospace, nor much of one for cursive, as all are self-explanatory. fantasy is described merely as “decorative.” (Note: this description was added in CSS version 3 documentation, still designated a working draft. It may become more specific in future and make results more predictable for designers.) Its meaning is left to individual interpretation, and browser developers have made fairly arbitrary choices. Internet Explorer uses for fantasy a font resembling Blackadder ITC, an Office font difficult to read at small sizes. Firefox uses Showcard Gothic, another Microsoft Office font, producing a completely different appearance. Safari uses Papyrus, slightly better known than the other two but very different from them.
For now it is probably best to avoid the generic family fantasy unless you want exceptionally decorative, large lettering.
Copyright © 2006 of the German translation, dpunkt.verlag GmbH
You may print the online version for your private use.
Otherwise, this chapter from “CSS − Anspruchsvolle Websites mit Cascading Stylesheets” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
dpunkt.verlag GmbH, Ringstraße 19, 69115 Heidelberg