Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > CSS

Stylin' Fonts and Text in CSS

  • Print
  • + Share This
In this chapter, you’ll learn about fonts and text, and the respective CSS properties you can use to style them. You'll also learn about the wonderful world of Web fonts, which download to your user along with your pages.
This chapter is from the book

Much of Web design deals with text, in paragraphs, headings, lists, menus, and forms, so understanding the CSS properties in this chapter is essential to making a site that looks professional. Almost more than any other factor, type makes the clearest visual statement about the quality of your site’s offerings. Graphics are the icing on the cake; typography is where good design begins.

In this chapter, you’ll learn about fonts and text, and the respective CSS properties you can use to style them. I’ll also introduce you to the wonderful world of Web fonts, which download to your user along with your pages. Now you no longer have to rely on the user having your font choices installed on his device, and you can be confident that every user will see your typography in the way you intend.

Let’s start with fonts.


The fonts you specify in your Web pages can come from three sources.

  • The fonts that are installed on the user’s device. (Until recently, these have been the only fonts reliably available to your Web pages.)
  • Fonts that are hosted on third-party sites, most notably Typekit and Google, and linked to your page using the link tag.
  • Fonts that are hosted on your Web server and served to the user’s browser along with the page, using the @font-face rule.

In the font property descriptions that follow, the examples will show the first of these sources: the fonts that are installed on the user’s computer. See Web Fonts Demystified later in this chapter for a discussion of the other two sources.

Now let’s look at the six properties that relate to font styling:

  • font-family
  • font-size
  • font-style
  • font-weight
  • font-variant
  • font (shorthand)

Font-Family Property

Example: h2 {font-family:times, serif;}

font-family determines the font in which an element is displayed. Typically, you set a primary font for the entire page, and then only add font-family styles to elements that you want to display in a different font. To specify the font for the entire page, you set the font-family of the body element:

font-family is an inherited property, so its value is passed to all its descendants, which in the case of body is all the other elements in the markup.

Because fonts must either be on the user’s computer, or delivered over the Web, there is always a possibility that a particular font you specify might not be available to a page. For this reason, fonts are always specified in lists called font stacks.

Specifying Installed Fonts Using Font Stacks

Fonts are installed in the operating system of a device, which allows all resident applications to share them. Only a limited set of fonts come installed in the typical operating system, and fonts can be added and removed by the user, so you can never be absolutely certain what fonts will be available to display your pages. Because of this, when stating the font in which you want text to display, you must also list additional “fallback” fonts in case your first choice isn’t available on the user’s system. This list of choices is called a font stack.

In short, font stacks ensure that the user sees your page text in the intended font if it is installed on her device, and if it is not, then in a font that you specify as an acceptable substitute.

This font stack effectively tells the browser “Display this document in Trebuchet MS, and if the system doesn’t have it, use Tahoma, and if neither is installed, use whatever generic sans-serif font is available.” It is very important to make the last item of a font-family declaration a generic declaration, typically “serif” or “sans-serif”, as a final fallback.

There are five generic font-family names:

Serif—serif fonts have small details at the terminals (tips) of the characters (like this text)

Sans-serif—sans-serif fonts have no details at the terminals (like the headings of this book)

Monospace—every monospace font character occupies the same amount of horizontal space (like the code examples in this book)

Cursive—cursive fonts look like handwriting (like the headline of The Hound of the Baskervilles example later in this chapter).

Fantasy—fonts that don’t fit the other categories (typically the strange and bizarre)

The purpose of these generic fonts is to ensure, that if none of your choices are available that, at a minimum, your document displays in the right type (no pun intended) of font.

It’s worth taking some care when selecting the fonts that you put in a font stack. For example, Dreamweaver offers a list of selectable font stacks that pop up every time you type font-family: in your CSS file, but these fonts are not ideal substitutes for one another. For example, here is a font stack that Dreamweaver offers:

Verdana is a bulky font that has a much larger x-height than Arial, so if a user does not have Verdana installed, your page will be displayed in Arial, a font that is smaller than the one you intended. More words will fit on each line, and the vertical height of text blocks may be shorter.

A good test is to view your pages with each font in the stack as the first choice so that you can see how the layout changes if it displays in one of the fallback fonts.

A better fallback for Verdana might be Tahoma, which has the same large x-height.


For a stack of lighter sans-serif fonts, you might use


Here’s a stack of serif fonts starting with a font that the user may not have.

In a case like this, always complete the stack with fonts that are supplied with most computer’s operating systems, here Georgia and Times, and end with the generic, serif.

Font-Size Property

Example: h2 {font-size:18px;}

Every HTML text element has a default font-size set by the browser style sheet, so when you set an element’s font-size, you are changing its font size from that default. Font sizing can appear to act unpredictably if you don’t understand how the inheritance of font sizes down the hierarchy is affected by which font size units you use. There are two types of units that you can use to set the font-size: absolute units, such as pixels or points, and relative units, such as percentages or ems. Let me explain the difference between them.

font-size is an inherited property, so a change to the font size of an element will result in a proportional change of size in the font sizes of its descendant elements. This means that if you set the font-size of the body element to 200%, then the text of all the elements on your page will double in size.

This effect occurs because in the browser style sheet, all element font sizes are set in the relative unit, em. For example, the h1 element is 2em, the h2 element is 1.5em, and p (paragraph) is 1em. By default, 1em is equivalent to 16 pixels—this is known as the font-size baseline. So by default, h1 is 32 pixels (16 × 2em = 32 pixels), h2 is 24 pixels, and p is 16 pixels.

If you set the body text to 20px, you are resetting the baseline, so now h1 would be 40px (20 pixels × 2em = 40 pixels), h2 would be 30 pixels, and p would be 20 pixels. However, font-size inheritance will not occur in descendant elements that have been sized with absolute units such as pixels—these elements will always display at their specified size.

Let’s learn more about font sizing by looking at each method of sizing fonts in turn.

Absolute Font Sizing

Sizing text with absolute units such as pixels, picas, or inches is simple; when you set the size of an element using absolute units, it stays that size no matter what font sizing is applied to its ancestors. The downside of absolute sizing is that if you decide to proportionally change the overall size of the text on your page, you have to change every absolute font-size in the style sheet; an absolutely-sized page requires more effort to fine tune.

In short, if you change the size of the body tag’s font, any absolutely-sized elements do not change size, but elements that have not been sized in your CSS will change proportionally to the size stated on body.

Relative Font Sizing

Sizing text with relative units such as percentages, ems, or rems is slightly more complex; when you set the size of an element using relative units, the size of the text is set relative to the size of the nearest “sized” ancestor.

Let’s consider this simple markup

and this CSS


In this example, the p tag text would be 12 pixels (the body tag’s 16 pixel baseline × .75 = 12). Because strong is a child of p, its point size would be 9 points. What you see is that relative sizes compound down through the hierarchy—strong is 16 pixels × .75 × .75 = 9 pixels. Relative units can take practice to master, as unlike absolute sizes, changing the relative font size of an element also changes all the child elements by the same proportion.

However, with relative sizing, you have the ability to tweak the size of all elements proportionally by resizing body, or a number of elements by changing a shared ancestor element. This can be time saving as you experiment with your layout, but it also takes planning for the same reason; a change to an element’s font-size affects all its descendant elements, too.

You cannot tweak font sizes like this if you work in absolute font-size units—each absolutely-sized element must be resized individually. Of course, if you do size in absolute units, you can size an element without getting the often-unwanted “knock-on” effect of a change of size in its ancestors.

However, with today’s wide range of screen sizes, from massive monitors to tiny phones, the need for text that can be easily scaled makes relative sizing the preferred approach.

A Note on Rem Units

The new relative rem (root em) unit is a CSS3 addition that is generating a lot of excitement in the Web community. When you size an element in rems, the size is relative, but only to the root HTML element. This gives you the best of both relative and absolute worlds; you can use relative sizing to proportionally change the overall font size by changing the font size of the HTML element, but unlike ems, font sizes are not compounded down through the hierarchy. Rems are supported by all the current browsers, but not by IE8 and earlier. The fallback is simple, however, and that is to provide absolute pixel sizing to browsers that don’t understand, and therefore ignore, rem declarations, like this

Let’s now look at the other font-related CSS properties.

Font-Style Property

Values: italic, oblique, normal

Example: h2 {font-style:italic;}

font-style determines whether a font is italicized or not. You can also write oblique instead of italic—the result is the same.

There are only two useful settings for the font-style property: italic to make regular text italicized, and normal to make a section within italicized type regular “upright” text. In this example

the code produces the result in Figure 4.1.

Figure 4.1

Figure 4.1. The normal value for the font-style property causes a specified section of text to appear normal within a bit of italicized text.

Note that the main purpose of italic text is to indicate emphasis, as in “It’s very hot today!” If you want to indicate emphasis, use the em tag, which styles the text as italic by default.

Font-Weight Property

Possible values: 100, 200, and so on to 900, or lighter, normal, bold, and bolder.

Example: a {font-weight:bold;}

Despite all the numerical options listed here, browsers only display two visual results for all font-weight values—bold or normal. Because interpretation of the numerical values differs among browsers, you’ll see the switch from normal to bold at various values—typically around 400. It’s best to avoid using all values except bold and normal, as illustrated in Figure 4.2.

Figure 4.2

Figure 4.2. The normal value for the font-weight property causes a specified section of text to appear normal within the bolded text.

Note that the primary purpose of bold text is to indicate importance, as in “Danger!” Mark up important text with the strong tag, which styles the text as bold by default.

Font-Variant Property

Values: small-caps, normal

Example: blockquote {font-variant:small-caps;}

This property accepts just one value (besides normal), and that is small-caps. This causes all lowercase letters to be set in small caps, like this:


The code above produces the result in Figure 4.3.

Figure 4.3

Figure 4.3. Here is a heading styled in small caps. Note the first letter of this text is in uppercase in the markup and remains unchanged.

I often use small-caps with the ::first-line pseudo-element as I demonstrate in The Hound of the Baskervilles example at the end of this chapter. Use this styling sparingly because text in all uppercase is harder to read as it lacks the visual cues provided by the ascenders and descenders of lowercase type.

Font Property

Example: p {font: bold italic small-caps .9em helvetica, arial, sans-serif;}

The code above produces the result in Figure 4.4.

Figure 4.4

Figure 4.4. Bolded, italicized, small-capped, sized, and font-family specified—all in a single CSS rule.

The font property is a shorthand styling that lets you apply all of the font properties in a single declaration, reducing the amount of CSS you have to write. You must follow two rules, however, so that the browser can interpret the properties correctly.

Rule 1: Values for font-size and font-family must always be declared.

Rule 2: The sequence for the values is as follows:

  1. font-weight, font-style, font-variant, in any order, then
  2. font-size, then
  3. font-family
  • + Share This
  • 🔖 Save To Your Account