Relative units such as em, rem and % are used constantly in front-end work, yet their behavior is only meaningful in context: they evaluate against whatever they reference. The same declared value can produce different results depending on the surrounding environment.

How relative units resolve

The distinction between em and rem is easy to gloss over, but relative units always measure against the thing they point at. A value like 2rem evaluates one way on a site whose root font size is 16px and another on a site set at 24px. Percentages behave the same way: 50% inside a 400px container is not the same result as 50% inside a 1200px container.

Because of that, assigning a size to an element starts with a question about its reference point: an element set to 5.25em only means something once you know what it is relative to.

Context changes meaning inside components

Thinking relatively also requires perspective — stepping out of your own vantage point to see things from another's. In code, this matters most when composing the pieces of a component, since each piece means something different depending on the context it sits in. That context is itself relative to the component that contains it.

An <h2> is hardly ever just an <h2>. It could be a post name, a widget heading or the heading of a card component, and front-enders name those things according to the perspective of the component. From the perspective of a post, an <h2> means — and likely looks — different than it does from the perspective of a card.

.post {}
.post__title {}

.widget {}
.widget__title {}

.card {}
.card__title {}

Naming follows the same logic. Deciding what to call something is hard enough, and the relative framing helps scope the decision: a name makes sense only against the context it will live in.

Relativity beyond the codebase

The same habit of mind applies well outside CSS. Several common frustrations read differently once you supply the missing frame of reference:

  • "This is stressing me out." Relative to what?
  • "I have so much to do." Relative to when?
  • "I suck as JavaScript." Relative to whom?
  • "I hate the place I work." Relative to where?

Absolutes are easy to get caught up in. Relativity forces us to see things differently — in a game of 20 Questions, in the code we write and elsewhere.