Lesson 17 of 23

In progress

Course progress

HTML and CSS

Units, fluid sizes and responsive defaults

Choose between rem, em, percentage, vw and ch, size text fluidly with clamp, and make media behave on small screens.

A layout that only works at one width is not finished. Units decide whether text respects a reader’s settings, whether an image overflows its column and whether a hero section fits a phone screen.

What you will learn

  • Pick the right unit for text, spacing, containers and media.
  • Size text fluidly with min, max and clamp.
  • Apply the responsive defaults that prevent most overflow.

The units that matter

  • px is an absolute CSS pixel. Fine for borders and hairlines, poor for text.
  • rem is relative to the root font size, so it follows the reader’s preference.
  • em is relative to the current element’s font size, useful for component-relative padding.
  • % is relative to the parent’s size, useful for widths.
  • vw and vh are percentages of the viewport, useful for full-bleed sections.
  • ch is the width of a zero in the current font, useful for line length.
HTML
Editable. Runs in your browser.





Press Run to see the output.

Percentage padding is calculated from the container’s width, not its height, which surprises people trying to make a box taller. Use padding-block with rem for vertical space in most cases.

80% with max-width is a robust pattern: fluid on small screens, capped on large ones. width: 80vw would ignore the scrollbar and cause horizontal overflow on some platforms.

Fluid type with clamp

clamp(minimum, preferred, maximum) keeps a value inside a range while letting it scale.

HTML
Editable. Runs in your browser.





Press Run to see the output.

The preferred value uses vw plus a rem base so it still scales when the user zooms text. A pure vw value does not respond to zoom, which is an accessibility problem.

Do not make body text fluid across a huge range. Sizes between 1rem and 1.125rem are plenty; headings can move more.

Media defaults

Two rules prevent most responsive breakage:

img, video, svg { max-width: 100%; height: auto; }

max-width: 100% stops media overflowing its container. height: auto preserves the aspect ratio when the width shrinks. With width and height attributes in the HTML, the browser can still reserve space.

For a full-height section on mobile, 100vh is unreliable because browser chrome changes the visible area. 100dvh uses the dynamic viewport height and is the better choice where supported, with 100vh as a fallback.

Breakpoints come from content

Do not pick device widths from a list. Resize the window and watch for the point where the layout becomes cramped or the line length becomes unreadable. That is your breakpoint.

A common pair is around 40rem for a single-column-to-two-column change and 64rem for wider spacing, but the numbers matter less than the reason. Media queries are covered properly in lesson 20.

Text, zoom and overflow

Test at 200 percent zoom. Fixed widths, fixed heights and white-space: nowrap are the usual culprits when content is clipped.

HTML
Editable. Runs in your browser.





Press Run to see the output.

overflow-wrap: anywhere lets an unbroken string break. It is the right fix for user-generated content such as URLs and IDs, where you cannot insert spaces.

Using vw for font sizes without a rem component means the text does not grow when the user zooms, because viewport units are tied to the viewport rather than the font setting. Always include a rem term in fluid type, as the clamp example does.

Write a hero section with a fluid heading between 2rem and 3.5rem, body text at 1rem, padding that scales with the viewport but stays between 1rem and 3rem, and an image that never exceeds its container.

Show solution
HTML
Editable. Runs in your browser.





Press Run to see the output.

Every value has a floor and a ceiling, and the heading includes a rem term so zoom still works. The image is capped at its container’s width.

Recap

  • rem for text and spacing, % with max-width for containers, px for hairlines.
  • clamp() gives fluid sizes with a floor and a ceiling; include a rem term so zoom works.
  • max-width: 100% on media and overflow-wrap on long strings prevent most horizontal overflow.

Next, you will build two-dimensional layouts with CSS Grid.

Should I use 100vh for a full-screen hero?
Use `min-height: 100dvh` where supported, with `100vh` as a fallback. `100vh` includes area hidden by mobile browser chrome, so content can appear cut off.[/tim_faq]
What is the difference between em and rem?
`rem` always refers to the root font size. `em` refers to the current element’s font size, so nested elements compound it, which is useful locally and confusing globally.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.