Common Size Units

CSS offers many different unit types for sizing elements and type, each with its own strengths and weaknesses. The unit types listed are only some of the most common, and many other less common units may be used as well.

Pixels

The px unit is an absolute measurement. It is analogous to Points or Picas in print design.

A pixel is equal to one dot on the display device. For low resolution devices, this is equal to one literal pixel on the screen. For high resolution devices, such as Retina or 4k displays, a pixel is set so that there are roughly 96 pixels per inch of screen.

By default, the base size of type in the browser is set to 16px.

Pixels are good for situations where you want to define an exact dimension and don’t want the element to scale with the page.

Rems

The rem is a type-relative measurement such that one rem is equal to the base type size (the font-size of the html element).

This means that, by default, 1rem is equal to 16px. Similarly, 2rem equals 32px.

But the size of 1rem can be changed by setting the font size on the html element. So if you wrote html { font-size: 20px}, 1rem would then be equal to 20px and 2rem would be equal to 40px. This flexibility allows you to resize everything set in rem units by changing a single value.

Rems are often the best and most powerful choice, especially for type.

Though rems are defined relative to type, they can be used to size any property.

Percentages

% units are relative units based on a 100% scale.

When used for things like font size, percentages are relative to the containing element type size. So setting a font size of 200% of default body type would result in a font size equal to 32px.

When used for length measurements like width or margin, percentages are relative to the containing element. So a div that is a direct child of body with a width set to 50% would have a width equal to half of the current width of the browser window.

Percentage lengths are great for sizes that scale relative to a user’s browser size. They are very useful in fluid / responsive design.

Viewport Units

Viewport Units are percentages of the size of the browser window. There are two different viewport unit value types:

vw (View Width): which is relative to the width of the browser window.

vh (View Height): which is relative to the height of the browser window.

So in the following example:

The element would be exactly as tall (100%) as the height of the browser window, and 50% of the width of the window.

Ems

For most purposes, it is best to avoid ems and use rems instead. They are mentioned here because they were common before rems became available.

Like rems, the em is a type-relative measurement, but such that one em is equal to the current type size (the type size of whatever HTML element you are targeting).

This means that, by default, in any base-size html element, 1em is equal to 16px. Similarly, 2em equals 32px.

However, ems are relative to the containing element, so if you set the type size of a ul to .5em, it will equal 8px. But if you nest a second ul inside another ul, the type in the nested ul will be .5em of the first one, in other words, 4px. If you nested yet another ul inside that one, you would get a type size of 2px. This is usually not what you want and can lead to frustration.

Practice Exercise