Fonts and Typography

There are four main font properties that you can style using CSS: family, style, size, and weight. Between them you can fine-tune the way text displays in your web pages and/or when printed, and so on.

This property assigns the font to use. It also supports listing a variety of fonts in order of preference from left to right, so that styling can fall back gracefully when the user doesn’t have the preferred font installed. For example, to set the default font for paragraphs you might use a CSS rule such as this:

p { font-family:Verdana, Arial, Helvetica, sans-serif; }

Where a font name is made up of two or more words, you must enclose the name in quotation marks, like this:

p { font-family:"Times New Roman", Georgia, serif; }

Figure 18-6 shows these two sets of CSS rules being applied.

With this property you can choose to display a font normally, in italics, or obliquely (a similar effect to italics, usually used with sans-serif typefaces). The following rules create three classes (normal, italic, and oblique) that can be applied to elements to create these effects:

.normal  { font-style:normal;  }
.italic  { font-style:italic;  }
.oblique { font-style:oblique; }

As described in the earlier section on measurements, there are a large number of ways you can change a font’s size, but these all boil down to two main types: fixed and relative. A fixed setting looks like the following rule, which sets the default paragraph font size to 14 points:

p { font-size:14pt; }

Alternatively, you may wish to work with the current default font size, using it to style various types of text such as headings. In the following rules relative sizes of some headers are defined, with the <h4> tag starting off 20 percent bigger than the default, and with each greater size another 40 percent larger than the previous one:

h1 { font-size:240%; }
h2 { font-size:200%; }
h3 { font-size:160%; }
h4 { font-size:120%; }

Figure 18-7 shows a selection of font sizes in use.

Using this property, you can specify the weight, or boldness, of a font. It supports a number of values but the main ones you will use are likely to be normal and bold, like this:

.bold { font-weight:bold; }