XML technologies / CSS / CSS Cookbook / Typography for the web / Better control of font sizes

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want font sizes independent of browser and operating system.

Solution

Set font-size in the element body to 62.5%:

body {
 font-size: 62.5%;
}

Then set font-size, for example in an inherited form, to 1em. For Internet Explorer for Windows, this looks like:

input, select, th, td {
 font-size: 1em;
}

Now the font size 1em in your document equals ten pixels. For a paragraph with 19-pixel text, use the declaration body from the first part of the solution. This rule does the rest:

p {
 font-size: 1.9em // stellt Text mit einer Größe von 19 Pixeln dar
}

Discussion

Browser rendering can differ greatly with operating system and video settings, making fixed, or absolute, values of little use. Ideally, avoid absolute lengths in web documents unless their output dimensions are predetermined. Fixed lengths are preferable in print, however. For print stylesheet information, see Printing.

Using pixels

Pixels provide consistent typography across most systems and browsers. Pixels are widely used for web typography, but take particular care with these browsers:

  • Netscape Navigator 4.x does not render pixel values correctly.

  • Opera 5 for Macintosh displays pixel values smaller than specified in the stylesheet.

If most visitors use neither Netscape Navigator 4.7 nor Opera 5, pixel values are largely unproblematic.

Accessibility and web typography

The main issue with pixels is accessibility, rather than precise sizing. People with impaired vision may need to resize text. Unfortunately, Internet Explorer for Windows cannot resize pixel-based text. As the most widespread browser, it therefore makes pixels a problem whenever users try to change text size.

If you need absolute units, prefer pixels to points, even though print designers know points better. Windows and Macintosh render point sizes differently, while pixels have the same size on both.

Note:
Although pixels are technically relative, designers call them absolute. Physically relative, a pixel is absolute in relation to a web page’s dimensions, which is what matters to designers.

If accessibility is especially important, use em units. This recipe’s solution defines paragraph text as 1.9em, equivalent to 190% of the default font size in browser preferences.

However, em units introduce another usability problem. Although users can resize text, setting a size below the browser default, such as 0.7em, makes Internet Explorer for Windows display tiny, almost unreadable lines (see the next figure). Use relative font sizes carefully, as text can easily become illegible.

Nearly unreadable text sized with em units

Figure: Nearly unreadable text sized with em units.

Using font-size keywords

Another solution uses font-size keywords. The CSS 2.1 specification defines seven absolute-size keywords (see the next figure): xx-small, x-small, small, medium, large, x-large and xx-large.

Two further keywords change font size relatively: larger and smaller. If the parent’s font size is small and the child has larger, the browser can increase the child to the absolute value medium.

Font-size keywords rendered

Figure: Font-size keywords rendered.

Keywords have two advantages: most browsers easily enlarge or shrink the text, and CSS-compliant browsers never go below nine pixels, preserving basic readability. For small text, use a sans-serif face such as Verdana to improve legibility.

The main drawback is Internet Explorer 4 through 5.5 using, instead of the recommended medium, the default size small. Consequently, these IE versions display every size one step smaller. The value xx-large in IE corresponds to x-large in all other browsers, x-large corresponds to large, and so on.

To give Internet Explorer 5 and 5.5 for Windows the correct keyword values, use the voice-family workaround below. See the recipe Hacks, workarounds and troubleshooting for details.

#content {
 /*
  Schriftgrößen-Workaround für WinIE 5: 
  1) WinIE 5/5.5 Wert zuerst:
 */
 font-size: x-small;
 voice-family: "\"}\"";
 voice-family: inherit;
 /*
  2) Dann zweimal den richtigen Wert:
 */
 font-size: small;
}
html>#content
 font-size: small;
}

Controlling font sizes with em

Keywords give general sizes, but designers usually want finer control. The solution in this recipe, by Richard Rutter, provides that control.

The default browser size is 16 pixels, corresponding to medium. Setting the font size of the element body to 62.5% of the default reduces it to ten pixels:

(16 pixels)62.5% = 10 pixels

As discussed, an em equals the browser default font size. Changing that size on the element body makes 1em equal ten pixels.

1em = 10px

Designers thus gain pixel-like control without the browser restrictions associated with pixels.

For example, a designer wanting a 24-pixel heading and 15-pixel paragraph text can use these styles:

body {
 font-size: 62.5%;
}
input, select, th, td {
 font-size: 1em;
}
h2 {
 font-size: 2.4em;
}
p {
 font-size: 1.5em;
}

Another advantage is preserving usability and accessibility by avoiding absolute units.

See also

  

 

 

 

back to top