Chapter 13 of 17 · CSS for Beginners — Complete Free Tutorial

CSS Units & Values

Every CSS value needs a unit — and choosing the right one decides whether your layout adapts or breaks. This chapter compares absolute and relative units, shows when each shines, and introduces calc() and clamp() for fluid designs.

Absolute units

UnitMeaning
pxDevice-independent pixels (1px = 1/96in)
pt, cm, inPrint units — mostly for @media print and documents

px is exact and predictable: borders, shadows, and fine details want it. But sizes of type in px ignore a user's browser font preference — use relative units there.

Relative units

UnitRelative toTypical use
remRoot (<html>) font size (16px default)Font sizes, spacing — scales with user settings
emThe element's own font sizePadding that should follow text size; nested scaling
%The parent's matching dimensionFluid widths, padding relative to width
vw / vhViewport width / height (1% each)Full-screen heroes, viewport spacing
vmin / vmaxSmaller / larger viewport sideOrientation-proof sizing
chWidth of the "0" glyphReadable line length (max-width: 65ch)
exHeight of "x"Rare; optical tweaks

em vs rem: the classic confusion

html { font-size: 16px; }

.card       { font-size: 1.25rem; }   /* 20px — of the ROOT, stable */
.card .note { font-size: 0.8em; }     /* 16px — 80% of .card's 20px */

.gutter     { padding: 1em; }         /* follows the ELEMENT's font size */
.gutter-big { font-size: 2rem; padding: 1em; }  /* padding doubles too! */

Expected output: the same 1em padding renders at different pixel sizes wherever the element's font size differs — that is em. rem stays constant (of the root), which is why it is the default choice for component sizing.

The nested-scaling power of em

Because em compounds, a container can scale everything inside it — handy for accessibility toggles or zoom UIs:

.zoom-ui      { font-size: 1rem; }
.zoom-ui.lg   { font-size: 1.25rem; }   /* every em-based child grows 25% */

Percentages: know your reference

.child       { width: 50%; }           /* of parent's WIDTH (even for height %) */
.padded      { width: 300px; padding: 10%; }  /* padding % is of parent's WIDTH */
.inline-pad  { padding: 1em 5%; }      /* horizontal vs vertical reference differs */

Height percentages only work when the parent has a definite height — a frequent surprise. For equal-height children prefer flex/grid stretch (the default).

Viewport units

.hero    { min-height: 100svh; }   /* small viewport height — mobile URL bar safe */
.sidebar { width: 18rem; }
.banner  { font-size: 3vw; }        /* scales with width — use clamp instead (below) */

Prefer the svh/lvh/dvh family over plain vh on mobile: the classic 100vh hero hides behind expanding browser bars; 100svh is stable.

calc(): arithmetic with mixed units

.content {
  width: calc(100% - 3rem);
  padding-left: calc(2rem + 4vw);
}

You can freely mix %, rem, and vw — something plain CSS values cannot do. Spaces around + and - are mandatory.

clamp(): min, ideal, max

h1 {
  font-size: clamp(2rem, 1rem + 2.5vw, 3.5rem);
}
.container {
  width: clamp(320px, 90%, 1200px);
  padding-inline: clamp(1rem, 4vw, 3rem);
}

Expected output: headings start at 32 px on phones, grow smoothly with the viewport, and stop at 56 px on large screens — a complete fluid-type rule in one line, no media queries. (Middle value must combine a viewport unit with a static one to be truly fluid.)

Unitless values

  • line-height: 1.6 — a multiplier of the element's font size (preferred).
  • z-index: 10, font-weight: 700, flex-grow: 1 — plain numbers by definition.

Which unit? Quick decisions

TaskUse
Body text, headings, component font sizesrem
Padding that should track text (buttons, chips)em
Borders, shadows, fine detailpx
Column widths, gutters of a layout% / fr / rem
Full-viewport sectionssvh/vh
Fluid type and spacingclamp()

Common mistakes

  • em padding on rem-sized parents — recompute carefully or use rem for stable spacing.
  • vw font sizes without clamp — huge on desktop, tiny on tablets.
  • px font sizes everywhere — breaks users who enlarged their default font.
  • Forgetting spaces in calc() — calc(100%-3rem) is invalid and silently ignored.

Practice checklist

  1. Convert one page's font sizes from px to rem and check it with a 20 px browser default.
  2. Write a fluid h1 with clamp() and resize the window.
  3. Rebuild a sidebar layout using calc(100% - 18rem) for the content column.

Units matter most when screens change size — next: Responsive Design & Media Queries.

Quiz: CSS Units & Values

5 questions · 70% to pass

Frequently Asked Questions

CSS (Cascading Style Sheets) is the language that controls how HTML content looks — colours, fonts, spacing, layout, responsiveness and animation. HTML provides structure and meaning; CSS provides presentation. One stylesheet can restyle an entire website at once.

The fundamentals (selectors, box model, Flexbox, media queries) take about 2–3 weeks of daily practice. Grid, animations and architecture come with a little more practice. This 17-chapter series is designed for 3–4 weeks at a relaxed pace — faster if you type out every example.

Yes. HTML first, then CSS, then JavaScript. CSS and HTML together let you build complete, good-looking static pages, and almost every JavaScript framework assumes you already know CSS layout and selectors.

No — CSS is a styling language with selectors and property-value declarations, not variables, loops or conditionals. You can get far without any programming logic, and JavaScript adds the behaviour later.

Flexbox distributes space along one axis (a row or a column) and is ideal for components — toolbars, navbars, card rows. Grid controls rows and columns together and is ideal for page structure and card grids. They are complementary: many layouts use a grid page shell with flex components inside.

Usually one of three reasons: the selector does not match (check class names), a more specific rule wins (use the browser inspector to see computed styles and specificity), or the file is not linked/loads in the wrong order. The specificity chapter explains how the cascade decides.

Every element is a box of four layers: content, padding (inner space), border, and margin (outer space). With box-sizing: border-box, the width you set includes padding and border — the universal reset every project uses.

Use relative units, flexible grids like repeat(auto-fit, minmax(260px, 1fr)), images with max-width: 100%, and mobile-first media queries. The goal is zero horizontal scrolling from 320px up — the responsive chapters show the full workflow.

Not to learn. Frameworks are productivity tools built on the same CSS you are learning here; understanding the fundamentals makes any framework easier to use (and to debug when it fights you). Learn vanilla CSS first, then evaluate frameworks with an informed eye.

JavaScript for interactivity, then a build tool and version control (Git). With HTML, CSS and JavaScript you can build complete websites; after that, frameworks like React or server-side tools like Laravel Blade build on these fundamentals.