Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS Units

CSS Units

CSS has several different units for expressing length.

Some CSS properties that set length include width, margin, padding, font-size, border-width, etc.

A length consists of a number and a unit, such as 10px, 2em, etc.

No space may appear between the number and the unit. If the length value is 0, the unit may be omitted.

For some CSS properties, lengths can be negative.

There are two types of length units: relative and absolute.


Browser support

The numbers in the table below indicate the minimum browser version that supports the length unit.

Length Units Chrome IE Firefox Safari Opera
em, ex, %, px, cm, mm, in, pt, pc 1.0 3.0 1.0 1.0 3.5
ch 27.0 9.0 1.0 7.0 20.0
rem 4.0 9.0 3.6 4.1 11.6
vh, vw 20.0 9.0 19.0 6.0 20.0
vmin 20.0 9.0* 19.0 6.0 20.0
vmax 26.0 Not supported 19.0 Not supported 20.0

Note: Internet Explorer 9 supports vmin through the non-standard name vm.


Relative Lengths

A relative length unit specifies a length relative to another length. Relative lengths are more suitable for different devices.

Unit Description Online example
em It is described relative to the font size applied to the current element, so it is also a relative length unit. Generally the default browser font size is 16px, so 2em == 32px; Try it
ex Depends on the height of the lowercase letter x Try it
ch The width of the digit 0
rem rem is short for root em. When rem is applied to a non-root element, it is relative to the root element's font size; when rem is applied to the root element's font size, it is relative to its initial font size. Try it
vw Viewport Width, 1vw = 1% of the viewport width Try it
vh Viewport Height, 1vh = 1% of the viewport height Try it
vmin The smaller of vw and vh. Try it
vmax The larger of vw and vh. Try it
%    
Note Tip: What is the difference between rem and em? The difference is that when using rem to set the font size of an element, it is still a relative size, but it is relative only to the HTML root element.

Absolute Lengths

An absolute length unit is a fixed value that reflects a real physical dimension. Absolute length units depend on the output medium and do not depend on the environment (monitor, resolution, operating system, etc.).

Unit Description Online example
cm Centimeters Try it
mm Millimeters Try it
in Inches (1in = 96px = 2.54cm) Try it
px * Pixels (1px = 1/96th of 1in) Try it
pt point, about 1/72 inch; (1pt = 1/72in) Try it
pc pica, about 12pt, 1/6 inch; (1pc = 12 pt) Try it

A pixel might be considered the best "device pixel", and this pixel length is unrelated to the text screen pixels you see on a monitor. px is actually a unit measured by angle.