Stylin' Fonts and Text in CSS

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

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.

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.

Aren’t Fonts and Text the Same Thing?

The answer is “No,” and here’s why.

Fonts are the different kinds of typefaces. Each font is a set of letters, numbers, and symbols with a unique visual appearance. Fonts are categorized into collections, based on their general look, such as serif, sans-serif, or monospace. Fonts are made up of families, with names such as Times and Helvetica. A font family in turn can be broken down into font faces, which are variations on the basic design of the font, such as Times Roman, Times Bold, Helvetica Condensed, and Bodoni Italic.

Text simply describes words and characters, like this sentence or the heading of a chapter, regardless of the font in which it is set.

CSS has a set of properties relating to fonts and a set of properties relating to text. Font properties relate to the size and appearance of collections of type. What is its family (Times or Helvetica, for example)? What size is it? Is it bold or italic? Text properties relate to the font’s treatment. What is its line height and letter spacing? Is it underlined or indented? And so on.

Here’s a way I think about this perhaps seemingly subtle distinction. You can apply font styles, such as bold and italic, to a single character, but text properties, such as line height and text indent, only really make sense in the context of a block of text, such as a headline or a paragraph.

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.

From my own testing I’ve observed thatfont-familynames are not case sensitive, but do not alter the case of a font name generated by Google or another hosted font service or your font may not display.

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.

Because the font name, Trebuchet MS, is more than one word, it has to be in quotes.

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:

x-height is the main area of the letters, excluding the ascenders and descenders of letters like d and p, which the letter x does not have, hence the name.

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.

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.

So, Which Fonts Are Available to All Users’ Browsers?

That is a common question which has no definitive answer but you have a high probability that any Mac or PC will have these fonts installed:

Serif

Georgia

Palatino/Book Antiqua

Times New Roman

Sans-serif

Arial

Arial Black

Arial Narrow

Tahoma

Trebuchet MS

Verdana

Monospace

Courier New

Lucida Console/Monaco

Cursive

Comic Sans MS

Fantasy

Impact

Because of the often obscure fonts on today’s phones and tablets, it is more important than ever to include fallback generic font families in your font stacks. If you want to specify a specific font, use a hosted Web font or one that is downloaded from your Web server—see Web Fonts Demystified, later in the chapter.

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.

Fonts can also be sized using keywords such as x-small, medium, and x-large. Medium is equal to the baseline size and the other keywords produce smaller or larger text. Because keywords produce a limited set of sizes, they are not widely used, but you can learn more about them at http://css-discuss.incutio.com/wiki/Using_Keywords.

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.

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.

If you want to use ems but also need to set specific pixels sizes, a good trick is to setbody’sfont-sizeto 62.5%. By doing this, the baseline size is changed from 16 to 10 pixels (16 × 62.5% = 10). Now it’s simple to translate ems to pixels: 1em equals 10px, 1.5em equals 15px, 2em equals 20px, and so on.

Set your font sizes working down the hierarchy when using relative sizes.

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

IE9 and earlier will only scale text set in relative units (not absolute units such as pixels) when the user changes the text size of the layout using the browser’s View > Text Size menu. This means that the minor downside of using rems is that if IE7 and IE6 users want larger type, they have to use View > Zoom and increase the text size of the entire page. Just another reason for them to upgrade to a modern browser.

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.

The Normal Value

normal causes any of the possible effects of a property not to be applied. Why might you want to do this?

The reason this option is available is so you can selectively override a default or a global property you have set. Headlines h1 through h6 are bold by default, so if you want to unbold the h3 element, for example, you need to write h3 {font-weight:normal;}. If your style sheet states a {font-variant:small-caps;} so that all links are in small caps, and you want one special set of links to be in regular upper- and lowercase type, you might write a declaration such as a.speciallink {font-variant:normal;}.

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.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.

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.

Jumping ahead in this chapter somewhat, you can write thefont-sizeproperty to also include theline-heightproperty (which is a text property rather than a font property) by writing the size as12px/1.5or similar. You’ll learn more about theline-heightproperty in the “Text Properties” section next.