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

Home > Articles > Web Design & Development > CSS

  • Print
  • + Share This
This chapter is from the book

Exploring Font Families

In the world of print, you can use just about any font family you want; you purchase and install the font on your computer if it's not already there, and you use it in the design of your document. When your design is complete, you then send the font to the printer along with the document, or you PDF your document so the fonts are converted to vectors (outlines), and you're done. In print, you can work with any of the thousands of available fonts because you have control over them all the way to the printing press.

On the Web, you simply don't have this freedom of choice as far as fonts are concerned. This is one of the most disappointing aspects of the Web for transitioning print designers; you must trust that the viewers have the fonts in which you want your document to be displayed installed on their computers. Fonts aren't part of the browser; they are served up for the all applications on a computer from the system software where they reside. Furthermore, it is impossible to know what flavor of any given font (Times, Times Regular, or Times Roman, for example) a user might have on their machine.

As much as you might want your Web pages' headlines to be displayed in Univers 87 Oblique, the odds of a user having that particular font are, to quote Elvis (Costello), less than zero. In fact, there is only a very short list of fonts that you can be sure users of both Windows and Macintosh have installed on their systems: Times, Arial, Verdana, and Courier.

Even Helvetica, perhaps the most popular sans-serif font of all time, is not included with Windows, but Windows has its own almost identical font, Arial.

"What about automatically downloading fonts as needed from my server to the user?" you ask (you did ask, right?). Good question. Although CSS 3 specifies a way you can request a font from your server in which to display the document, browsers do not currently support this capability. But it is nice to think about, and one day it may be a reality. (Even then, the browser will never install the font on the user's computer but merely use it to display the page).

Until the happy day when fonts are available on demand, to use specific fonts with CSS, you need to list the fonts, in order of preference, in which you would like the document to be displayed. This list must only be by family name—by that I mean you must use Helvetica or Times, not Helvetica Condensed or Times Expanded.

It is accepted practice to write a CSS declaration specifying a of either serif or sans-serif fonts starting with the one you prefer first and ending with a generic font name such as serif or sans-serif like this (in this case, I'm working with sans-serif fonts)

font-family {"trebuchet ms", helvetica, arial, sans-serif}

Because Trebuchet MS is more than one word, it has to be in quotes. If you do this in an inline style that's already inside double quotes, use single quotes on the name, like this

<p style="font-family:'trebuchet ms', helvetica, arial,  sans-serif">

or like this in the case of serif fonts

font-family: {charcoal, times, serif}

In the first example using the font-family property above I am saying to the browser, "Display this document in Trebuchet MS, and if you don't have it, use Helvetica. If you don't have either of those, use Arial, and if all else fails, use whatever generic sans-serif font you do have. It is very important to make the last item of a font-family declaration a generic declaration of either serif or sans-serif as a final fallback. This ensures that, at a minimum, your document at least displays in the right type (no pun intended) of font.

In the second example using the font-family property, I first declare Charcoal, a font only available on Macintosh, because I want Macintosh users to have the pleasure of viewing my document in that lovely font. However, because Windows users don't usually have Charcoal, they see the document displayed in the second choice, Times.

Using Embedded Styles (for Now)

To keep things simple, I'm going to show you how to write your CSS styles in a style element in the head of the document. I'll also show you how to remove the link to the external style sheet for now. Doing this means that you won't have to manage a separate style sheet, but the styles that you write will only be available to this one document. That's ideal for developing the layout of a specific page; later you'll create a separate style sheet that can supply styles to multiple pages. Review the start of Chapter 2 if this doesn't make complete sense.

Let's use the HTML document you created in Chapter 1 and modify it to include the style element in the document head, as illustrated by the highlighted code

<head>
<title>A Sample XHTML Document</title>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<style type="text/css">
       
   <-- a

   </style>
</head>

The blank line between the opening and closing tag of the style element is where you add your CSS. When the browser encounters the opening tag of the style element, it stops interpreting the code as XHTML and starts interpreting it as CSS. When it encounters the closing tag of the style element, the browser reverts to treating the code as XHTML again. So anything you write within the style element must be in CSS syntax, the same syntax you use if the CSS is in a separate style sheet. This means any code within the style element is formatted like this:

selector {property1:value; property2:value; etc.}

You need to be aware of whether you are writing CSS or XHTML at any given moment during the development of your projects and make sure you format your code accordingly. In the case of the sample XHTML documents I provide at my Web site, www.bbd.com/stylin, the XHTML is already written, so once you add the style element using XHTML, you are working entirely in CSS.

Setting the Font Family for the Entire Page

To set the font family for the entire page, you first need to set it for the body of the document

<style type="text/css">
body {font-family: verdana, arial, sans-serif;}
</style>

Save your changes, flip to the browser, and refresh the page. What you see should look like Figure 3.6 .

03fig06.jpg

Figure 3.6 Setting the font-family property of the body element affects the whole document.

Because font-family is an inherited property, its value is passed to all its descendants, which, since body is the top-level element, are all the other elements in the markup. So with one line, you've made it so everything is in the desired font; bathe for a moment in that glow of CSS magic. OK, moving right along . . .

  • + Share This
  • 🔖 Save To Your Account