Units of Measurement in CSS

In theory CSS understands linear dimensions specified in a variety of different ways. Broadly speaking, there are two categories of units, absolute and relative. We describe the various options each each category below

The most extensively used unit in your site design should be em followed by percent, %. In a complex layout made up of a number of positioned divs it may be necessary to size one residual div in other units, such as pixels, and then to resize it at display time using JavaScript. Should the use of em units prove to be impractical consider using percentage units instead.

There is one very subtle point regarding the use of em units that must be appreciated

Failing to understand this issue can lead to apparently incorrect page layouts such as this one.

A final note: When specifying sizes in CSS the unit of measurement should immediately follow the number. Thus width:5em; is correct but width:5 em; is not. The only time when it is not necessary to specify the unit of measurement is when the number is a zero. Thus padding:0em; and padding:0; are both correct.

Jump To...

Colophon