SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
CSS Values and Responsive SizingCSS UnitsCSS Custom PropertiesCSS FunctionsFluid SizingDesign Tokens Choosing units and tracing values
Values and Units

CSS Units

Use absolute, relative, viewport, and container units.

A unit gives a numeric length its meaning. In 20px, the number is 20 and the unit is px. The useful question is not only “how large is the number?” but also “what does this number measure against?”

Write a length without a space between its number and unit. For zero lengths, the unit can usually be omitted, as in margin: 0.

Choose a reference first

Use the reference that should control the size:

If the size should follow...Start with...Why
A fixed CSS measurementpxThe value stays the same CSS length as its context changes.
A font sizeem or remThe value scales from a local or root font size.
The browser viewportvw, vh, svh, or dvhThe value follows the viewport rather than a component.
An ancestor container’s inline sizecqiThe value follows a size-query container around the component.

Percentages are another relative option, but their reference depends on the property. The preceding lesson’s rule still applies: ask “a percentage of what?” before calculating one.

Absolute lengths: px

.note {
  border: 2px solid green;
}

px is an absolute length unit in CSS. A CSS pixel is an abstract CSS measurement, so it is not necessarily one physical screen pixel; device resolution and zoom affect how it is displayed.

Other absolute units include cm, mm, in, and pt. They have fixed relationships to each other in CSS.

Font-relative lengths: em and rem

  • em: Uses the element's computed font size for most properties. On font-size itself, it uses the inherited font size instead.
  • rem: Uses the root element's font size, which is the font size on html in an HTML document.
html {
  font-size: 16px;
}

.theme {
  font-size: 20px;
}

.note {
  font-size: 1.5em;
  padding: 1em;
  margin-top: 1rem;
}

For a .note directly inside .theme:

  • Font size: 1.5 × 20px = 30px.
  • Padding: 1em uses the note's own 30-pixel font size, so it is 30px.
  • Top margin: 1rem uses the root's font size, so it is 16px.

A note with a 20px parent resolves 1.5em to a 30px font. Its 1em padding uses that 30px, while its 1rem top margin uses the 16px root.

These results use the font sizes set in the example. A visitor’s root font size may differ. Nested em font sizes can multiply at each level, while rem always refers to the root.

Change one reference

Raise the parent’s font size to 24px and keep the root at 16px. Which two measurements change?

html {
  font-size: 16px;
}

.theme {
  font-size: 24px;
}

.note {
  font-size: 1.5em;
  padding: 1em;
  margin-top: 1rem;
}

The note’s font size becomes 36px (1.5 × 24px). Its padding becomes 36px too, because 1em now reads that new font size. The top margin stays at 16px because its reference is the root.

Viewport-relative lengths

The viewport is the area through which the browser displays the document:

  • 1vw: One percent of the viewport width.
  • 1vh: One percent of the large viewport height in current browsers.
  • 1svh: One percent of the small viewport height, accounting for expanded browser controls.
  • 1dvh: One percent of the dynamic viewport height, which can change as browser controls appear or disappear.
.banner {
  width: 50vw;
  min-height: 100dvh;
}

At a viewport width of 1000 pixels, 50vw is 500 pixels. Dynamic height can resize as mobile browser controls appear or disappear, while the small viewport gives a stable reference when those controls are expanded. Viewport units and percentages have different bases.

Check the reference before calculating:

  • At a 1000-pixel viewport width, 25vw is 250px.
  • At a 600-pixel large viewport height, 50vh is 300px.

The calculation is always “number × 1% of the named reference.”

Container-relative lengths

Container units measure a size-query container around the element. 1cqi is 1% of that container’s inline size, which is its width in horizontal writing:

.panel {
  container-type: inline-size;
}

.panel h2 {
  font-size: 5cqi;
}

The panel establishes the size-query container. If its inline size is 400 pixels, 5cqi gives the heading a 20-pixel font size. This responds to the panel, rather than the whole browser window.

If there is no suitable container, these units fall back to the small viewport size for that direction. You will use containers more fully in responsive design.

The important sequence is: create the container, identify the axis, then calculate the percentage. In the example, inline-size is the width because the writing direction is horizontal. 5cqi therefore means 5% × 400px = 20px.

Try it yourself

Try it yourself

Trace each size reference

Preview width

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

Key Takeaways

  • Units: A unit connects a number to a particular measurement or reference.
  • Fonts: em can follow a local font size; rem follows the root font size.
  • Viewports: Viewport units follow browser dimensions, with small and dynamic variants for changing controls.
  • Containers: Container units follow a suitable size-query container around the element.

How is this lesson?

Previous lessonCSS Values and Responsive SizingNext lessonCSS Custom Properties
Edit on GitHubReport on GitHub
On this page
%

On this page

Choose a reference firstAbsolute lengths: pxFont-relative lengths: em and remChange one referenceViewport-relative lengthsContainer-relative lengthsTry it yourselfKey Takeaways