liveBook · Manning

Rem

As explained in Tiny CSS Projects

The CSS rem unit expresses a length relative to the font size of the document's root element.

A rem is a CSS length relative to the font size of the root element, usually `<html>`. Unlike an em, which uses the parent element’s font size as its base, a rem uses the root’s size. If no root font size is set, most browsers default to 16px. section 4.1.1

Size a newspaper headline

Set `h1 { font-size: 4rem; }` to make the headline four times the root font size. With a 16px root size, it renders at 64px. section 4.1.1

Listing 6.11 Styling the name
h1 {                            #1
  font-size: 2rem;              #1
  margin: 36px 0 0;             #1
  color: var(--primary);        #1
}                               #1
 
.title {                        #2
  font-size: 1.25rem;           #2
  font-weight: bold;            #2
  color: var(--secondary);      #2
}                               #2

The profile card uses `2rem` for the name and `1.25rem` for the job title. At a 16px root size, the name is 32px. Relative font sizes help text scale with user settings or device needs. section 6.7.1

Rems also work beyond headings. A form input set to `font-size: 1rem` matches the application’s text size; the input needs explicit font styling because it does not inherit those styles by default. The same rule uses `.25rem` for padding but `1px` for its border. section 10.3.1

How Tiny CSS Projects covers it

How deep it goes
Defines rem, contrasts it with em, and calculates sizes from a 16px default; stops short of browser internals.
Examples
Shows a 4rem newspaper headline, a 2rem profile name, and 1rem form inputs.
What it assumes
Introduces relative units using familiar HTML elements and CSS font-size declarations; assumes no custom root font size.
What you can do after
Apply it via CSS to size headings and inputs relative to the root font size.
How it connects
Connects rem to em, CSS font-size, accessibility, and font inheritance in form controls.

Also explained in

Compare how 6 books explain rem →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny CSS Projects. The section links point to the parts of the book it draws on.