Learning goals: By the end, you'll be able to:
- tell absolute and relative units apart
- choose the right unit for text, spacing, borders and containers
-
explain why
emcompounds andremdoesn't -
size boxes with
%,min-andmax-properties -
use viewport units, including
dvh -
write fluid sizes with
clamp(),min(),max()andcalc() - make images scale safely
Part 1: Absolute vs relative (3 min)
Every size in CSS needs a unit. They fall into two families:
| Type | Meaning | Examples |
|---|---|---|
| Absolute | A fixed size that ignores its surroundings | px, pt, cm, in |
| Relative | Calculated from something else | rem, em, %, vw, vh, ch |
The web runs on screens of every size, and users can change their default text size. Relative units adapt to both, so you'll use them for most things.
px is the only absolute unit you'll use regularly on screen. A CSS pixel is defined as 1/96th of an inch, and it doesn't always match one physical screen dot. On a high-resolution display, 1px can span several. Units like cm and in are mainly for print stylesheets.
Quick check A. Is rem absolute or relative? What about pt?
Part 2: rem and px (5 min)
You met these in Lesson 5. Here's the quick recap.
rem means "root em", the font size of the <html> element. Browsers default to 16px.
html { font-size: 100%; } /* keeps the user's setting, usually 16px */
h1 { font-size: 2rem; } /* 32px */
p { font-size: 1rem; } /* 16px */
.gap { margin-bottom: 1.5rem; } /* 24px */Formula: rem = px ÷ 16.
Use 100% rather than 16px on html. That way, if a visitor has increased their browser's text size, your layout respects it. css_lesson7_px_to_rem.csv has 15 common conversions.
Quick check B. What is 1.5rem in pixels at the default size?
Quick check C. If html { font-size: 20px; }, what is 2rem?
Part 3: em, and why it compounds (5 min)
em is relative to a font size, but which one depends on the property:
-
In
font-size,emmeans the parent's font size. -
In anything else (padding, margin, width),
emmeans the element's own font size.
.button {
font-size: 1.25rem; /* 20px */
padding: 0.5em 1em; /* 10px top/bottom, 20px left/right */
}Because the padding is in em, it grows or shrinks whenever the button's text size changes. This is the best use of em.
The compounding trap
Because em in font-size is relative to the parent, nested elements multiply:
li { font-size: 1.25em; }| Nesting level | Size |
|---|---|
| 1 | 16 × 1.25 = 20px |
| 2 | 20 × 1.25 = 25px |
| 3 | 25 × 1.25 = 31.25px |
| 4 | 39.06px |
Each nested list gets bigger. Using rem fixes it:
li { font-size: 1.25rem; } /* always 20px */css_lesson7_em_compounding.csv shows the numbers for five levels.
Rule of thumb: use rem for font sizes, and em for padding and margins you want to scale with the text.
Quick check D. The parent has font-size: 20px. A child has font-size: 1.5em and padding: 1em. What are its font size and its padding?
Quick check E. ul li { font-size: 0.9em; } and a list is nested 3 levels deep. Roughly what's the size of the innermost text? (Start from 16px.)
Part 4: Percentages (4 min)
A percentage is always "a percentage of something". For width, that's the parent's width.
.container { width: 600px; }
.child { width: 50%; } /* 300px */Two surprises:
-
Percentage padding and margin use the parent's width, on all four sides. Even
padding-top: 10%is 10% of the parent's width. -
height: 50%only works if the parent has a set height. A parent whose height comes from its content gives the percentage nothing to measure.
.container { width: 400px; }
.child {
width: 25%; /* 100px */
padding: 5%; /* 20px on every side */
}With the border-box reset from Lesson 6, that child's total width stays at 100px.
Quick check F. The parent is 400px wide. A child has width: 25% and padding: 5%, and box-sizing: border-box. What is the child's total width, and what is the padding on each side?
Part 5: Sizing properties (5 min)
An element's size can come from its content (intrinsic), or from you (extrinsic). An image has a natural size from its file. An empty <div> has no height until you put something in it.
| Property | Job |
|---|---|
width, height | Set a fixed size |
min-width, min-height | Never smaller than this, but grows with content |
max-width, max-height | Never larger than this |
aspect-ratio | Keeps proportions, such as 16 / 9 |
.container {
width: 100%;
max-width: 70rem; /* never wider than 70rem */
margin: 0 auto; /* centred, from Lesson 6 */
}
.hero {
min-height: 60vh; /* at least 60% of the screen height, taller if needed */
}
.video {
aspect-ratio: 16 / 9;
}Prefer min-height to height. If you set height: 200px and the text needs more room, the text spills out of the box. min-height lets the box grow.
Quick check G. What does max-width: 70rem combined with width: 100% do on a phone, and what does it do on a wide monitor?
Part 6: Viewport units and ch (4 min)
Viewport units size things relative to the browser window.
| Unit | Meaning |
|---|---|
vw | 1% of the viewport width |
vh | 1% of the viewport height |
vmin / vmax | 1% of the smaller / larger of the two |
.hero { min-height: 100vh; } /* at least the full screen height */
.half { width: 50vw; } /* half the window width */Two warnings
-
100vwcan cause a horizontal scrollbar. It includes the width of the vertical scrollbar, so the element ends up slightly too wide. Usewidth: 100%instead. -
100vhon phones. Mobile browsers show and hide their address bar, andvhdoesn't change when they do. The fix is a newer unit:
| Unit | Meaning |
|---|---|
svh | Small viewport: browser bars visible |
lvh | Large viewport: browser bars hidden |
dvh | Dynamic: updates as the bars appear or hide |
.hero { min-height: 100dvh; }ch: the readability unit
ch is the width of the "0" character in the element's font. It's a convenient way to limit line length (you met the 45 to 75 character rule in Lesson 5).
p { max-width: 65ch; }Quick check H. The viewport is 1000px wide and 700px tall. How wide is 5vw, and how tall is 10vh?
Part 7: clamp(), min(), max() and calc() (6 min)
These functions let CSS do the maths for you.
calc()
Mix units in one value:
.main { width: calc(100% - 2rem); }You must put spaces around + and -. calc(100%-2rem) is invalid.
min() and max()
.sidebar { width: min(100%, 20rem); } /* the smaller of the two */
.panel { width: max(50%, 18rem); } /* the larger of the two */min(100%, 20rem) means "full width, but never more than 20rem", a compact alternative to width plus max-width.
clamp()
clamp(minimum, preferred, maximum) gives a value that moves with the screen but stays between two limits.
h1 {
font-size: clamp(1.75rem, 4vw + 1rem, 3rem);
}A worked example, using clamp(1rem, 2.5vw + 0.5rem, 2rem):
| Viewport | 2.5vw + 0.5rem | Result |
|---|---|---|
| 375px | 17.38px | 17.38px |
| 768px | 27.2px | 27.2px |
| 1200px | 38px | 32px (capped at 2rem) |
The full table is in css_lesson7_clamp_table.csv.
Fluid type with clamp() often removes the need for separate media queries just to change heading sizes, which helps in Lesson 11.
Quick check I. For clamp(1rem, 3vw + 0.5rem, 2rem) at a viewport of 500px wide, what's the result?
Quick check J. A container is 800px wide. What is calc(100% - 2rem) for a child inside it?
Part 8: Images and responsive sizing (3 min)
Images have an intrinsic size. On a small screen, a large image would overflow its container. The standard fix:
img {
max-width: 100%;
height: auto;
}-
max-width: 100%stops the image becoming wider than its container, but doesn't stretch a small image to fill it. -
height: autokeeps the proportions.
If you used width: 100% instead, a small image would stretch and look blurry. Also, don't rely on this to shrink huge image files. Use images no bigger than they need to be, or the page loads slowly.
Which unit should I use?
| I want to size... | Use |
|---|---|
| Text | rem |
| Space between things | rem |
| Padding that follows the text size | em |
| Borders and hairlines | px |
| A container | % with max-width |
| Line length | ch |
| A full-screen section | min-height: 100dvh |
| A heading that scales with the screen | clamp() |
| Line spacing | a unitless number, like 1.6 |
Common mistakes
| Mistake | Fix |
|---|---|
| Nested text getting larger and larger | Use rem for font-size, not em |
width: 100vw causing a horizontal scrollbar | Use width: 100% |
height: 50% doing nothing | The parent needs a set height |
calc(100%-2rem) | Add spaces: calc(100% - 2rem) |
Fixed height with text overflowing | Use min-height |
100vh section cut off on a phone | Use 100dvh |
Using width: 100% on images | Use max-width: 100%; height: auto; |
Setting html { font-size: 16px; } | Use 100% so user settings are respected |
Expecting padding-top: 10% to follow the height | It follows the parent's width |
Key takeaways
- Relative units adapt to screens and user settings, and absolute units don't.
-
Use
remfor text and spacing,pxfor fine lines, andemfor padding that scales with the text. -
eminfont-sizecompounds when nested, so useremthere. - Percentages are relative to the parent, and percentage padding and margin use the parent's width.
-
Prefer
max-widthandmin-heightto fixed sizes. -
dvhfixes full-height layouts on phones, andchlimits line length. -
clamp(),min(),max()andcalc()let you build fluid sizes. -
Images need
max-width: 100%; height: auto;.
Free resources
All of these are free and trustworthy for going deeper:
- MDN: CSS values and units: a clear guide with live examples.
-
MDN: Sizing items in CSS: covers intrinsic vs extrinsic size, percentage padding and
max-widthon images. -
web.dev Learn CSS: Sizing Units: a full reference including
ch, the viewport variants (svh,lvh,dvh) and container units. - CSS Units & Sizes Tutorial for Beginners (YouTube): a beginner video on the common units. I haven't watched it, so treat it as an optional extra.
-
Your browser's DevTools (right-click, then Inspect) show computed pixel values, so you can see what
remand%work out to.
Exercises (15 min guided, plus extras)
1. Convert (2 min). Give each in rem (16px base):
- a) 20px b) 12px c) 36px
2. Compound it (2 min). li { font-size: 1.5em; }, starting at 16px. What is the font size at nesting levels 1, 2 and 3?
3. Percentages (2 min). A parent is 600px wide. A child has width: 40% and padding: 10%, with box-sizing: content-box.
- a) What is the child's content width?
- b) What is the padding on each side?
- c) What is the child's total width?
4. Predict (2 min). What does each give?
-
a)
5vwon a 1200px wide viewport -
b)
clamp(1rem, 3vw + 0.5rem, 2rem)at 1000px wide -
c)
clamp(1rem, 3vw + 0.5rem, 2rem)at 200px wide
5. Spot the errors (4 min). Find four problems:
.wrapper {
width: 100vw;
margin: 0 auto;
}
.main {
width: calc(100%-40px);
}
.hero {
height: 100vh;
}
h1 {
font-size: clamp(3rem, 4vw, 1.5rem);
}6. Build it: a fluid article page (12 min). Create article.html and article.css.
-
Start
article.csswith theborder-boxreset andhtml { font-size: 100%; }. - Add a header, an article with a heading and 3 paragraphs, and an image.
-
Give the article
max-width: 70rem,width: 100%andmargin: 0 auto, with padding inrem. -
Limit paragraphs to
max-width: 65ch, withline-height: 1.6(Lesson 5). -
Give the
h1aclamp()font size. -
Make the image
max-width: 100%; height: auto;. -
Make the header at least
40dvhtall usingmin-height. - Resize your browser window and check that the text is readable and nothing overflows. Open DevTools and compare the computed sizes at two window widths.
7. Challenge (4 min). Make a square that is always 40% of the smaller side of the viewport (hint: Part 6). Then make a 16:9 box without using height. Finally, write one width rule using min() that has the same effect as width: 100%; max-width: 30rem;.
Lesson 7 checkpoint
- I can explain the difference between absolute and relative units.
-
I use
remfor font sizes and spacing. -
I can explain why
emcompounds. - I know what a percentage is a percentage of.
-
I use
max-widthandmin-heightinstead of fixed sizes. -
I know why
100dvhcan be better than100vh. -
I can write a
clamp()value. -
My images scale with
max-width: 100%; height: auto;.
⬇️ ANSWER KEY (only scroll once you've tried the questions)
Quick check A: rem is relative (to the root font size). pt is absolute (1/72 of an inch).
Quick check B: 1.5 × 16 = 24px.
Quick check C: 2 × 20 = 40px.
Quick check D: Font size is 1.5 × 20 = 30px. Padding 1em uses the element's own font size, so it's also 30px.
Quick check E: 16 × 0.9 × 0.9 × 0.9 = about 11.66px. It shrinks at each level.
Quick check F: Total width is 100px (25% of 400px, because border-box includes the padding). Padding is 5% of 400px = 20px on every side.
Quick check G: On a phone, it's the full width of the screen, because that's narrower than 70rem. On a wide monitor, it stops at 70rem (1120px at the default size).
Quick check H: 5vw = 50px. 10vh = 70px.
Quick check I: 3vw = 15px, plus 0.5rem (8px) = 23px. That's between 16px and 32px, so the result is 23px.
Quick check J: 800 − 32 = 768px.
Exercise 1: a) 20 ÷ 16 = 1.25rem; b) 12 ÷ 16 = 0.75rem; c) 36 ÷ 16 = 2.25rem.
Exercise 2: Level 1: 16 × 1.5 = 24px; level 2: 24 × 1.5 = 36px; level 3: 36 × 1.5 = 54px.
Exercise 3:
- a) 40% of 600 = 240px
- b) 10% of the parent's width = 60px on every side
- c) 240 + 60 + 60 = 360px
Exercise 4:
- a) 5% of 1200 = 60px
- b) 3vw = 30px, plus 8px = 38px, which exceeds the maximum, so it's capped at 32px (2rem)
- c) 3vw = 6px, plus 8px = 14px, which is below the minimum, so it's raised to 16px (1rem)
Exercise 5: The four problems:
-
width: 100vwcan cause a horizontal scrollbar, andmargin: 0 autohas nothing to centre. Usewidth: 100%. -
calc(100%-40px)needs spaces:calc(100% - 40px). -
height: 100vhcan cut off content, and on phones the browser bars matter. Usemin-height: 100dvh. -
clamp(3rem, 4vw, 1.5rem)has a minimum (3rem) larger than its maximum (1.5rem), so the result is always 3rem. Swap them:clamp(1.5rem, 4vw, 3rem).
Corrected:
.wrapper {
width: 100%;
margin: 0 auto;
}
.main {
width: calc(100% - 40px);
}
.hero {
min-height: 100dvh;
}
h1 {
font-size: clamp(1.5rem, 4vw, 3rem);
}Exercise 6: Check that the reset and html { font-size: 100%; } are at the top, the article is centred with max-width: 70rem, paragraphs stop at about 65 characters, the heading size changes as you resize, and the image never overflows. In DevTools, the computed h1 size should differ between a narrow and wide window, until it reaches your maximum.
Exercise 7:
.square {
width: 40vmin;
height: 40vmin;
}
.video {
width: 100%;
aspect-ratio: 16 / 9;
}
.panel {
width: min(100%, 30rem);
}