Logo hdtoledo
hdtoledo.dev
Módulos_del_Sistema
MÓDULO_TEÓRICO

Relative and Fluid Units (rem, em, clamp)

Relative Units

Saying Goodbye to Rigid Pixels

One of the most severe flaws in web design is hardcoding typography, paddings, and containers with fixed pixels (px). Why? Because if a user with visual impairments increases their browser base font size, a px-locked website completely ignores their setting.

In this lesson, you will master Fluid Typography and Dynamic Spacing using modern relative units.


1. rem vs em: The Core Difference

  • rem (Root EM): Relative to the root <html> font size (default 16px).
    • 1rem = 16px
    • 1.5rem = 24px
    • 2rem = 32px
    • Best for: Typography, paddings, margins, and component widths.
  • em (Element EM): Relative to its immediate parent font size.
    • Best for: Button paddings and icons that should scale proportionally when parent font size changes.

2. Viewport Units: vw and vh

  • 100vw: 100% of the browser window width.
  • 100vh: 100% of the browser window height.
  • Modern Tip: On mobile devices with dynamic address bars, use 100dvh (Dynamic Viewport Height) to prevent Safari/Chrome toolbars from clipping your bottom buttons.

3. The Magic Function: clamp(min, preferred, max)

clamp() calculates a fluid dynamic value that never drops below min or exceeds max, scaling seamlessly across any screen:

css
/* Fluid Typography: Min 24px (1.5rem), scales with 4vw, Max 48px (3rem) */ h1.portal-title { font-size: clamp(1.5rem, 4vw, 3rem); } /* Adaptive card padding without media queries */ .dashboard-card { padding: clamp(1rem, 2.5vw, 2.5rem); }