From what we covered in the previous lesson, you can see that there are a number of things that can affect how much space a box takes up. But let’s also take a moment to talk about how to quantify this space, and dig a bit deeper into CSS length units.
Just like there are numerous different units of measurement for lengths in the physical world, e.g. centimetres, inches, feet, light year etc., there are various length units when it comes to CSS generated boxes as well. The specification that defines how these units work is the CSS Values and Units Module.
CSS length units are split into 2 broad categories:
- Absolute lengths
- Relative lengths
Absolute lengths
Absolute lengths are fixed lengths which are anchored to some physical measurements. There are a number of units which fall into this category:
| unit | name | equivalence |
|---|---|---|
| px | pixels | 1px = 1/96th of 1in |
| cm | centimeters | 1cm = 96px/2.54 |
| mm | millimeters | 1mm = 1/10th of 1cm |
| Q | quarter-millimeters | 1Q = 1/40th of 1cm |
| in | inches | 1in = 2.54cm = 96px |
| pc | picas | 1pc = 1/6th of 1in |
| pt | points | 1pt = 1/72th of 1in |
The specification mentions that absolute units are mainly useful when the output environment is known. In other words, if you are certain of the size of the canvas your element is going to appear in.
Practically speaking, such a situation is not common when it comes to web pages. Unless your web page is not freely and publicly accessible and only appears on specific screens with no way to resize the window, maybe a shopping mall kiosk or something like that.
Relative lengths
Relative lengths are dependent on the size of something else. There are a number of “something else”s used for sizing in CSS, hence the corresponding number of relative length units.
| unit | relative to |
|---|---|
| em | font size of the element |
| rem | font size of the root element |
| vw | 1% of viewport’s width |
| vh | 1% of viewport’s height |
| vmin | 1% of viewport’s smaller dimension |
| vmax | 1% of viewport’s larger dimension |
| ex | x-height of the element’s font |
| ch | width of the “0” (ZERO, U+0030) glyph in the element’s font |
Percentages (%) are not considered a CSS length unit because it can be used for more than just lengths. But percentages are also relative values because they indicate a value that is some fraction of another value.
Using relative units makes it easier for a design to adapt and scale across different output environments, hence use of relative units is encouraged when building for the web. Ideally, a robustly designed web layout will not “break” even in the most awkward or unexpected environments.
Many of us who work on the web are working hard to dispel the notion that a design must look the same across every device. It is simply not practical to have such an expectation.
That being said, it is not that we should never use absolute lengths. Absolute concepts like “always” or “never” do not make sense in general. There will inevitably be some case where something considered unorthodox under normal circumstances ends up being the best choice when faced with specific constraints.
Arguably, the most important unit of all is still the pixel (px) because browsers resolve length values to pixels when it comes to the final rendering of elements on the screen, and pixels are generally handled consistently across browsers.
The pixel is the canonical unit of the web. This is why we can mix our use of CSS length units without issue. You can have an element sized with px sat next to another element sized with % adjacent to something else sized with em units.
There is a difference between a device pixel and a CSS pixel, where the device pixel is the single spot of light on a digital screen, while the CSS pixel is, according to the specification:
The reference pixel is the visual angle of one pixel on a device with a pixel density of 96 DPI and a distance from the reader of an arm’s length.
In the grand scheme of things, you don’t really have to fully grasp the ins and outs of the relationship between a pixel on the screen maps to a physical length you can measure with a ruler. But if you are interested, in addition to the specification, Tim Chien provides an in-depth look into CSS lengths in his article: CSS Length Explained.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson