XML technologies / XML / XML in a Nutshell / Cascading Style Sheets (CSS) / Pixels, points, picas, and other length units

(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)

Many CSS properties represent lengths. Some of the most important, though by no means all, are:

CSS provides many length units, divided into two groups:

Absolute units suit printed media, meaning paper, but should be avoided in other media, where relative units are appropriate. Pixels are an exception and are best avoided altogether. For example, this style rule makes the element gericht exactly half a centimeter tall:

gericht { height: 0.5 cm }

Documents intended for screens, such as televisions or computer monitors, should not have fixed sizes. The size of an inch or another absolute unit can vary with monitor resolution. Users also have different defaults: what looks good on one monitor may be unreadable on another. Instead, use relative units, such as em, which is relative to the width of the uppercase letter M in the current font, or ex, relative to the height of the lowercase x in the current font. For example, this rule sets the property line-height of the element geschichte to one and a half times the height of x:

geschichte { line-height: 1.5ex }

The pixel is also a relative unit, relative to a pixel on the current display. This is generally close to a point but may vary between systems. We generally recommend avoiding pixel units unless aligning something to a bitmap graphic in an exact 1:1 ratio. Web pages formatted with pixel lengths inevitably look too small or too large on many users’ monitors.

Another useful technique specifies lengths as percentages of other lengths. The reference length depends on the property. For example, if line-height is given as a percentage, it is calculated from the property font-height of the same element. These two rules set the property font-height of the element gericht to 0.5 centimeters and the property line-height of the element gericht to 0.75 centimeters:

gericht { font-height: 0.5cm }
gericht { line-height: 150%  }

  

 

 

 

back to top