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

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

Many CSS properties are defined using measurements. In a style rule, these determine how large or wide something should appear. Measurements are mainly used for fonts, spacing and page positions.

There are two kinds of measurements: absolute and relative. An absolute measurement, such as setting a font to 18px, tells the browser to use an average character size of 18 pixels. (Note: strictly speaking, even a pixel can be considered a relative unit because its size depends on the target computer’s screen resolution and monitor size. Here, “relative” means a relationship to a value in a style rule or HTML attribute. In this context, a pixel is an absolute unit.) Technically, the browser is instructed to display the font at a height of 18 pixels. Font height and width are described in Using fonts with CSS.

With a relative measurement, a style rule tells the browser to determine the size of a font or another HTML element relative to a starting value. The following rule makes the font size of all text marked up as paragraphs 150% of the default value:

p {
  font-size: 150%;
}

Assuming that most browsers display characters at 12 pixels by default, the following produces the same result: 150% of 12 pixels is 18 pixels.

p {
  font-size: 18px;
}

Use relative measurements whenever possible. They adapt to the user’s browser settings and offer advantages for alternative stylesheets. They also let visually impaired users adjust the font size more easily.

In CSS, both horizontal and vertical measurements are specified as length values. They consist of an optional plus or minus sign, a number with or without a decimal point, and the unit. There must be no spaces between the number and unit.

  

  

 

 

 

back to top