Course topics

By WebNest Studio Engineering Team

CSS Tutorial

CSS Units of Measurement

CSS offers several categories of units for sizing elements, spacing, and typography, and choosing the right one has a direct impact on how well a layout adapts across devices. Units broadly split into absolute units, which always represent the same physical size, and relative units, which scale based on some other value like the parent's font size or the viewport.

Modern, responsive CSS leans heavily on relative units — em, rem, %, vw, and vh — because they let layouts and text scale gracefully with user preferences (like a larger default browser font size) and different screen sizes, without a developer needing to hardcode a value for every breakpoint.

Why it matters

A layout built in fixed pixels looks right on one screen and wrong on most others, and ignores users who have set a larger default text size. Relative units make a design scale. Choosing between px, rem, em, percentages and viewport units is a decision you make on almost every declaration.

Absolute Units

px (pixels) is the only absolute unit used in everyday CSS. A pixel does not scale with the user's font-size settings, which makes it predictable for things like borders and box-shadows, but less ideal for text, since it ignores user accessibility preferences set at the browser level.

Relative Units Based on Font Size

em is relative to the font-size of the current element (or its parent, when used for font-size itself), which means em values can compound unpredictably when nested. rem ("root em") is relative to the font-size of the root <html> element only, so it never compounds — this makes rem the preferred unit for consistent, predictable spacing and typography across a whole site.

Percentage and Viewport Units

% sizes an element relative to its containing block (usually the parent), which is common for fluid widths like width: 50%. Viewport units size relative to the browser window itself: vw is 1% of the viewport width, and vh is 1% of the viewport height — useful for full-screen sections (height: 100vh) or text that scales with screen size.

Choosing the Right Unit

A practical rule of thumb: use rem for font-size and consistent spacing (margin, padding, gap), use % for fluid container widths, use vw/vh sparingly for full-viewport sections or hero text, and reserve px for things that genuinely should not scale, like a 1px border or a small fixed icon.

Examples

rem staying consistent while nested em compounds unexpectedly

CSS
html { font-size: 16px; }

.parent { font-size: 1.5em; }       /* 16px * 1.5 = 24px */
.parent .child { font-size: 1.5em; } /* 24px * 1.5 = 36px, compounding! */

.parent .child-rem { font-size: 1.5rem; } /* always 16px * 1.5 = 24px */
Output
The nested .child element renders at 36px because em compounds through each nesting level, while .child-rem stays at a predictable 24px regardless of nesting depth.

A full-viewport hero section using vh alongside a fluid width in percent

CSS
.hero {
  width: 100%;
  height: 100vh;
  padding: 2rem;
  font-size: clamp(1.5rem, 4vw, 3rem);
}
Output
The hero section fills the full height of the browser window and the full width of its container, while the heading text scales smoothly between 1.5rem and 3rem depending on viewport width.

Common Mistakes

  • Using em for font-size on deeply nested elements without realizing the value compounds with each ancestor, producing unexpectedly large or small text.
  • Hardcoding font sizes in px, which ignores a user's browser-level font-size preference and can hurt accessibility.
  • Using 100vh on mobile browsers and being surprised when it exceeds the visible area due to dynamically appearing address bars.
  • Setting a percentage width on an element whose parent has no defined size, resulting in unexpected or collapsed dimensions.

Exercise

Size everything with relative units: the heading at twice the root font size; the paragraph at 1.125 times the root font size; main at 90% of the width of its parent but never wider than 40rem; and button padding that scales with the button's own font size, 0.75 of it vertically and 1.5 of it horizontally.

Expected result: the layout keeps its proportions when the browser's default font size is changed.

CSS · starter code
/* The page contains: <main> with an <h1>, a <p> and a <button> */

/* TODO: h1 font size relative to the root */

/* TODO: p font size relative to the root */

/* TODO: main width as a percentage, with a maximum in rem */

/* TODO: button padding relative to its own font size */
Show hints
  • rem is relative to the font size of the root element, and em to the font size of the element itself.
  • Padding with two values is written vertical then horizontal: padding: 0.75em 1.5em.
Show solution
CSS · solution
/* The page contains: <main> with an <h1>, a <p> and a <button> */

h1 {
  font-size: 2rem;
}

p {
  font-size: 1.125rem;
}

main {
  width: 90%;
  max-width: 40rem;
}

button {
  padding: 0.75em 1.5em;
}

Quick quiz

1. What is 1rem equal to?

  • A. The font size of the parent element
  • B. Always 10px
  • C. The font size of the root (html) element
  • D. 1% of the viewport width
Show answer

C. By default that is 16px, unless the user or the stylesheet changes it.

2. A div with font-size: 1.5em is nested inside another div with the same rule, and the root size is 16px. What is the inner font size?

  • A. 24px
  • B. 48px
  • C. 16px
  • D. 36px
Show answer

D. em is relative to the parent's font size, so it compounds: 16 × 1.5 × 1.5.

3. What does 50vw mean?

  • A. Half the width of the viewport
  • B. 50 pixels
  • C. Half the width of the parent element
  • D. Half the font size
Show answer

A. One vw is 1% of the viewport width. A percentage, by contrast, is relative to the parent.

Interview questions

What is the difference between em and rem?

Show answer

rem is always relative to the font size of the root element, so 1rem means the same everywhere on the page. em is relative to the font size of the element itself, or of its parent when used for font-size, so nested elements compound. rem is the usual choice for font sizes and spacing; em is useful when something should scale with the text of its own component, such as button padding.

Why use rem instead of px for font sizes?

Show answer

Users can change the default font size of their browser, and many people with low vision do. Sizes in rem scale with that setting, while sizes in px ignore it. Using rem for type and for spacing related to type therefore respects the user's preference and makes the whole layout scale consistently. Pixels remain fine for things that should not scale, such as a 1px border.

Key Points to Remember

  • px is an absolute unit; em, rem, %, vw, and vh are relative units that scale based on context.
  • rem is relative only to the root font-size and never compounds, unlike em.
  • % sizes relative to the containing block; vw/vh size relative to the viewport.
  • Prefer rem for typography and spacing, % for fluid widths, and px for things that should stay fixed.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering