Log inSign up free
Mastering Modern CSS: From Zero to Pro
  1. Getting started

    1. CSS Lesson 1: What Is CSS?
    2. CSS Lesson 2: Selectors and Rules
    3. CSS Lesson 3: How CSS Works: The Cascade
    4. CSS Lesson 4: Colour and Backgrounds
    5. CSS Lesson 5: Typography and Text
    6. CSS Lesson 6: The Box Model
    7. CSS Lesson 7: Units and Sizing
    8. CSS Lesson 8: Display and Positioning
    9. CSS Lesson 9: Flexbox
    10. CSS Lesson 10: Grid
    11. CSS Lesson 11: Responsive Design
    12. CSS Lesson 12: Final Project
Mastering Modern CSS: From Zero to Pro

Getting started · Lesson 7 of 12 · 13 min read · By Jack

CSS Lesson 7: Units and Sizing

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.

Downloads for this lesson

Sign up free (or log in) to download these files.

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 em compounds and rem doesn't
  • size boxes with %, min- and max- properties
  • use viewport units, including dvh
  • write fluid sizes with clamp(), min(), max() and calc()
  • make images scale safely

Part 1: Absolute vs relative (3 min)

Every size in CSS needs a unit. They fall into two families:

TypeMeaningExamples
AbsoluteA fixed size that ignores its surroundingspx, pt, cm, in
RelativeCalculated from something elserem, 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, em means the parent's font size.
  • In anything else (padding, margin, width), em means 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 levelSize
116 × 1.25 = 20px
220 × 1.25 = 25px
325 × 1.25 = 31.25px
439.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:

  1. Percentage padding and margin use the parent's width, on all four sides. Even padding-top: 10% is 10% of the parent's width.
  2. 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.

PropertyJob
width, heightSet a fixed size
min-width, min-heightNever smaller than this, but grows with content
max-width, max-heightNever larger than this
aspect-ratioKeeps 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.

UnitMeaning
vw1% of the viewport width
vh1% of the viewport height
vmin / vmax1% 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

  1. 100vw can cause a horizontal scrollbar. It includes the width of the vertical scrollbar, so the element ends up slightly too wide. Use width: 100% instead.
  2. 100vh on phones. Mobile browsers show and hide their address bar, and vh doesn't change when they do. The fix is a newer unit:
UnitMeaning
svhSmall viewport: browser bars visible
lvhLarge viewport: browser bars hidden
dvhDynamic: 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):

Viewport2.5vw + 0.5remResult
375px17.38px17.38px
768px27.2px27.2px
1200px38px32px (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: auto keeps 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
Textrem
Space between thingsrem
Padding that follows the text sizeem
Borders and hairlinespx
A container% with max-width
Line lengthch
A full-screen sectionmin-height: 100dvh
A heading that scales with the screenclamp()
Line spacinga unitless number, like 1.6

Common mistakes

MistakeFix
Nested text getting larger and largerUse rem for font-size, not em
width: 100vw causing a horizontal scrollbarUse width: 100%
height: 50% doing nothingThe parent needs a set height
calc(100%-2rem)Add spaces: calc(100% - 2rem)
Fixed height with text overflowingUse min-height
100vh section cut off on a phoneUse 100dvh
Using width: 100% on imagesUse max-width: 100%; height: auto;
Setting html { font-size: 16px; }Use 100% so user settings are respected
Expecting padding-top: 10% to follow the heightIt follows the parent's width

Key takeaways

  • Relative units adapt to screens and user settings, and absolute units don't.
  • Use rem for text and spacing, px for fine lines, and em for padding that scales with the text.
  • em in font-size compounds when nested, so use rem there.
  • Percentages are relative to the parent, and percentage padding and margin use the parent's width.
  • Prefer max-width and min-height to fixed sizes.
  • dvh fixes full-height layouts on phones, and ch limits line length.
  • clamp(), min(), max() and calc() let you build fluid sizes.
  • Images need max-width: 100%; height: auto;.

Free resources

All of these are free and trustworthy for going deeper:


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) 5vw on 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.

  1. Start article.css with the border-box reset and html { font-size: 100%; }.
  2. Add a header, an article with a heading and 3 paragraphs, and an image.
  3. Give the article max-width: 70rem, width: 100% and margin: 0 auto, with padding in rem.
  4. Limit paragraphs to max-width: 65ch, with line-height: 1.6 (Lesson 5).
  5. Give the h1 a clamp() font size.
  6. Make the image max-width: 100%; height: auto;.
  7. Make the header at least 40dvh tall using min-height.
  8. 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 rem for font sizes and spacing.
  • I can explain why em compounds.
  • I know what a percentage is a percentage of.
  • I use max-width and min-height instead of fixed sizes.
  • I know why 100dvh can be better than 100vh.
  • 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:

  1. width: 100vw can cause a horizontal scrollbar, and margin: 0 auto has nothing to centre. Use width: 100%.
  2. calc(100%-40px) needs spaces: calc(100% - 40px).
  3. height: 100vh can cut off content, and on phones the browser bars matter. Use min-height: 100dvh.
  4. 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);  
}

Keep track of your learning

This lesson comes with 5 downloads. Create a free account to unlock them and track your progress.

  • Track your progress through every course
  • Take the quizzes and see your score
  • Download the worksheets and resources
  • Get a certificate when you finish

Related reading on our blog

Rather have it done for you?

We build websites for small businesses

Everything in this course is what we do every day for local businesses: fast, mobile-friendly sites that are built to be found on Google. See our web design services or our website packages.