Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

How big is your box?

4m:4s

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:

  1. Absolute lengths
  2. 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.