XML technologies / CSS / Advanced websites with CSS / Behind the scenes / Measurements / Relative values

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

Given absolute-unit problems, relative values are more flexible. Main units are em and %, though some prefer the obscure ex. em relates to the capital M’s height in the base font and matches font-size, ex relates to lowercase x-height and is less common.

em and % units scale from inherited or default sizes and are equivalent: 1em equals 100%.

When does a value use inheritance rather than browser defaults? It depends.

Before Opera 5, browsers used their default font sizes, overridden freely by designers. Users had to accept them.

CSS introduced client-side preferences. Opera 5 first offered a local user stylesheet editable and savable through menus.

This helped users but complicated design. Previously, designers assumed twelve-point defaults. (Pixels and points differ by browser and system: on most Windows browsers, 12pt equals 16px.) Now a user rule enlarging text by 1.25em could produce fifteen points.

A 1.25em rule scales the normally twelve-point default by 1.25. If the user chooses sixteen points, the result is twenty.

It may be best not to worry: someone enlarging the base font probably needs it. If your design enlarges twelve to fourteen points for a reason, that user similarly benefits from enlarging sixteen to twenty.

Font size is the preference users most want to control. Avoid interfering and ask whether each explicit size is necessary. I think nine times out of ten it is not.

Relative sizes also have traps: values can combine or multiply unexpectedly.

For example, both bold and italic text use 1.5em:

.bold {
   font-weight: bold;
   font-size: 1.5em;
}
.italic {
   font-style: italic;
   font-size 1.5em;
}

These rules combine in different ways:

<p>Das ist normal, <span class="bold">das ist fett</span>, <span class="italic">das ist kursiv</span>, <span class="bold italic">das ist fett und kursiv</span>, und zum Schluss, <span class="bold">das ist fett, <span class="italic">dann kursiv</span></span>.</p>

Nested elements, meaning one inside another such as span inside span, produce 2.25em (1.5em x 1.5em). Sizes multiply because children inherit calculated parent values rather than relative values. Such errors are easy to prevent but also easy to overlook, producing striking results as below.

Chaotic relative font sizes

Figure: Chaotic relative font sizes.

  

 

 

 

back to top