Skip to content

Lesson 6 of 9 · 18m

Responsive Images, Logical Properties, and Units That Scale

srcset/sizes done right, physical-vs-logical properties for internationalization, and the unit system (rem, ch, svh) that keeps layouts honest.

Layout engineering isn't just grid and flexbox — it's the units and primitives underneath. Three areas separate production-grade responsive CSS from tutorial-grade: serving the right image, writing direction-agnostic styles, and choosing units that respect the user's environment.

Responsive images: tell the browser the truth

<img
  src="hero-800.webp"
  srcset="hero-400.webp 400w, hero-800.webp 800w, hero-1600.webp 1600w"
  sizes="(min-width: 60rem) 33vw, (min-width: 40rem) 50vw, 100vw"
  alt="Server rack in the Oslo datacenter"
  width="1600" height="900"
  loading="lazy" decoding="async">

srcset with w descriptors tells the browser what widths are available; sizes tells it how wide the image will actually render at each viewport — and the sizes values are layout facts, not guesses: check what your grid actually does at each breakpoint. Get sizes wrong and mobile users download 1600px images for a 400px slot. width/height attributes aren't decoration — they let the browser reserve the aspect-ratio box before the image loads, eliminating layout shift (CLS), which is a Core Web Vital, which means it's a ranking factor, which means someone in marketing will eventually care very much.

art direction — cropping differently per breakpoint, not just resizing — is what <picture> with <source media> is for. Use srcset for resolution switching, <picture> for crop changes.

Logical properties: stop hardcoding "left"

.card {
  padding-inline: 1rem;       /* left+right in LTR, flipped in RTL */
  padding-block: 0.75rem;     /* top+bottom */
  margin-inline-start: 1rem;  /* margin-left in LTR, margin-right in RTL */
  border-start-start-radius: 8px;
}

inline is the text direction (horizontal in Latin/Arabic, vertical in vertical-writing-mode scripts); block is the perpendicular axis. If your product will ever be translated into Arabic or Hebrew, physical properties (margin-left) are bugs waiting for a localization audit. Logical properties cost nothing today — adopt them as the default.

The units that matter

  • rem — for type and spacing scales, because it respects the user's font-size preference. Root-em based spacing means a user who sets a larger default font gets a proportionally roomier layout — that's correct behavior.
  • ch — width of the 0 glyph; max-width: 65ch on prose is the canonical readable-line-length constraint.
  • svh/dvh/lvh — small/dynamic/large viewport height, fixing mobile's eternal 100vh problem: mobile browsers hide and show chrome, so 100vh overflows. Use min-height: 100dvh (or svh for guaranteed-fit) for full-height layouts.
  • em — component-internal scaling: padding proportional to font-size, so a .btn--large variant scales harmoniously.

Field notes

  • Never use px for media/container query breakpoints — zoomed-in users hit px breakpoints differently than rem-based ones. Use rem in queries.
  • loading="lazy" on above-the-fold hero images hurts performance — lazy is for below the fold only. Heroes want fetchpriority="high".
  • aspect-ratio: 16 / 9 in CSS is the modern replacement for the padding-bottom hack; combine with object-fit: cover for media.

Was this lesson helpful?

Sign in to rate