Chapter 1 of ?
css 8 min read

CSS3 Mastery — Chapter 3: CSS Colors, Units, PX-EM Converter & Math Functions

Chapter 3: CSS Colors, Units, PX-EM-REM Converter & Math Functions

Master modern CSS color spaces, responsive measurement units, dynamic viewport and container query units, interactive unit conversions, and mathematical layout functions to create fluid, adaptive user interfaces.

3.1 CSS Color Models & Modern Color Spaces

CSS offers multiple ways to express colors, ranging from classic HEX and RGB to modern wide-gamut spaces like HSL, HWB, and OKLCH:

Color Model Syntax Example Description & Best Use Cases
Named Colors color: crimson; 148 predefined W3C color keywords (e.g. coral, dodgerblue).
HEX / Hexadecimal #6366f1 / #6366f180 Base-16 red, green, blue values (00-FF). 8-digit HEX includes 2-digit alpha channel.
RGB / RGBA rgb(99 102 241 / 0.8) Red, Green, Blue intensity (0-255). Modern CSS syntax uses space-separated values with / opacity.
HSL / HSLA hsl(239deg 84% 67%) Hue (0-360°), Saturation (0-100%), Lightness (0-100%). Intuitive for creating color palettes and theme variations.
HWB hwb(239 10% 20%) Hue, Whiteness, Blackness. Human-friendly model for tinting and shading base hues.
OKLCH (Modern) oklch(0.65 0.24 260) Perceptually uniform color space covering high-gamut Display-P3 screens. Eliminates unpredictable brightness shifts when altering hues.
Interactive Color Picker & Multi-Model Converter

Pick any color using your mouse or adjust the opacity slider to generate instant CSS color codes across all color spaces!

Color Preview Box
HEX / HEXA #6366f1cc
RGB / RGBA rgb(99 102 241 / 0.8)
HSL / HSLA hsl(239deg 84% 67% / 0.8)
HWB (Hue, Whiteness, Blackness) hwb(239 39% 5% / 0.8)
OKLCH (Perceptual Gamut) oklch(0.58 0.23 264 / 0.8)

Color Opacity vs opacity Property

Setting opacity: 0.5 on a container makes the entire element and all its children transparent. To make only the background transparent while keeping text fully opaque, use semi-transparent background colors: background: rgba(99, 102, 241, 0.2); or background: oklch(0.6 0.2 250 / 20%);.

<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: system-ui, sans-serif; background: #0f172a; color: #f8fafc; padding: 24px; }
    .swatch-row { display: flex; flex-wrap: wrap; gap: 12px; margin: 16px 0; }
    .swatch {
      flex: 1 1 140px; padding: 16px 12px; border-radius: 10px;
      font-size: 12px; font-family: monospace; color: #fff; font-weight: 600;
      min-height: 70px; display: flex; flex-direction: column; justify-content: flex-end;
    }
    h3 { margin-top: 0; color: #818cf8; }
  </style>
</head>
<body>
  <h3>CSS Color Models — Live Comparison</h3>
  <div class="swatch-row">
    <div class="swatch" style="background: crimson;">Named: crimson</div>
    <div class="swatch" style="background: #6366f1;">HEX: #6366f1</div>
    <div class="swatch" style="background: #6366f180;">HEXA: #6366f180 (50% alpha)</div>
    <div class="swatch" style="background: rgb(99 102 241 / 0.8);">RGB: rgb(99 102 241 / 0.8)</div>
    <div class="swatch" style="background: hsl(239deg 84% 67%);">HSL: hsl(239deg 84% 67%)</div>
    <div class="swatch" style="background: hwb(239 10% 20%);">HWB: hwb(239 10% 20%)</div>
    <div class="swatch" style="background: oklch(0.65 0.24 260);">OKLCH: oklch(0.65 0.24 260)</div>
  </div>
  <p style="font-size:13px;color:#94a3b8;">
    All swatches target the same indigo hue — different syntaxes, same result. Try editing the values!
  </p>
</body>
</html>

3.2 Absolute vs Relative CSS Units

Units control sizing, spacing, typography, and viewport adaptation. Choosing between absolute and relative units determines how responsive your design will be across different screen sizes and accessibility font settings:

Absolute Units

Fixed dimensions that do not scale relative to screen resolution or user browser settings.

  • px — Pixels (1px = 1/96th of an inch). Standard for thin borders or fixed icons.
  • pt / pc — Points & Picas (used for print stylesheets).
  • cm / mm / in — Physical real-world measurements.
Relative Units (Best Practice)

Dynamic measurements that scale based on parent elements, root font sizes, or viewport dimensions.

  • rem — Relative to root element (html) font size (Default 1rem = 16px). Essential for accessible typography!
  • em — Relative to current element's or parent's font size. Great for padding buttons relative to their text size.
  • vw / vh — Viewport Width (1vw = 1% of viewport width) & Viewport Height.
  • cqw / cqh — Container Query Width & Height (1cqw = 1% of parent container's width).
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: system-ui, sans-serif; background: #0f172a; color: #f8fafc; padding: 24px; }
    .unit-row { display: flex; flex-direction: column; gap: 10px; }
    .unit-bar {
      background: #1e293b; border-radius: 8px; padding: 10px 14px;
      display: flex; justify-content: space-between; align-items: center;
    }
    .unit-label { font-family: monospace; font-size: 13px; color: #818cf8; }
    .unit-note  { font-size: 11px; color: #64748b; }
    h3 { margin-top: 0; color: #10b981; }

    /* Absolute unit — fixed regardless of settings */
    .demo-px   { font-size: 20px;   border-left: 4px solid #ef4444; }

    /* Relative to root html font size (16px default) */
    .demo-rem  { font-size: 1.25rem; border-left: 4px solid #10b981; }

    /* Relative to parent element font size */
    .demo-em   { font-size: 1.25em;  border-left: 4px solid #3b82f6; }

    /* Relative to viewport width */
    .demo-vw   { font-size: 2.5vw;   border-left: 4px solid #f59e0b; }
  </style>
</head>
<body>
  <h3>Absolute vs Relative Units Demo</h3>
  <div class="unit-row">
    <div class="unit-bar demo-px">
      <span class="unit-label">font-size: 20px</span>
      <span class="unit-note">Absolute — fixed at 20px always</span>
    </div>
    <div class="unit-bar demo-rem">
      <span class="unit-label">font-size: 1.25rem</span>
      <span class="unit-note">Relative to root (16px × 1.25 = 20px)</span>
    </div>
    <div class="unit-bar demo-em">
      <span class="unit-label">font-size: 1.25em</span>
      <span class="unit-note">Relative to parent font size</span>
    </div>
    <div class="unit-bar demo-vw">
      <span class="unit-label">font-size: 2.5vw</span>
      <span class="unit-note">Scales with viewport width — try resizing!</span>
    </div>
  </div>
</body>
</html>

3.3 Interactive Multi-Dimension Live Converter

Convert any dimension value live across PX, REM, EM, VW, VH, PT, CM, and IN based on your custom base root font-size and screen viewport width:

Live Multi-Dimension Unit Converter

Type a measurement value in any unit field below to instantly convert it across all CSS unit dimensions!

px
px
px

3.4 Modern CSS Math Functions

Modern CSS includes native mathematical functions that perform dynamic calculations directly inside stylesheets without requiring JavaScript:

1. calc() — Dynamic Expression Evaluation

Performs calculations mixing different unit types (e.g. percentages minus pixel offsets).

<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: system-ui, sans-serif; background: #0f172a; color: #f8fafc; padding: 24px; margin: 0; }
    :root { --spacing-unit: 8px; }

    /* Full width minus a 48px sidebar margin on each side */
    .content-panel {
      width: calc(100% - 48px);
      background: #1e293b;
      border: 1px solid #6366f1;
      border-radius: 10px;
      /* Padding calculated from a CSS variable */
      padding: calc(var(--spacing-unit) * 3);
      margin: 12px auto;
    }

    /* Height as a mix of viewport and fixed units */
    .hero-banner {
      height: calc(50vh - 80px);
      background: linear-gradient(135deg, #4f46e5, #06b6d4);
      border-radius: 10px;
      display: flex; align-items: center; justify-content: center;
      margin-bottom: 12px;
    }
  </style>
</head>
<body>
  <div class="hero-banner">
    <p style="color:#fff;font-weight:700;">height: calc(50vh - 80px) — half viewport minus 80px</p>
  </div>
  <div class="content-panel">
    <strong>width: calc(100% - 48px)</strong>
    <p style="margin:4px 0 0;font-size:13px;color:#94a3b8;">padding: calc(var(--spacing-unit) * 3) = 24px from CSS variable</p>
  </div>
</body>
</html>
2. min() & max() — Boundary Constraints

min(val1, val2) selects the smallest value, setting a maximum limit. max(val1, val2) selects the largest value, setting a minimum floor.

<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: system-ui, sans-serif; background: #0f172a; color: #f8fafc; padding: 24px; }

    /* Width is 80% of screen but NEVER wider than 600px */
    .container {
      width: min(80%, 600px);
      background: #1e293b;
      border: 1px solid #3b82f6;
      border-radius: 10px;
      padding: 20px;
      margin: 12px auto;
    }

    /* Font size scales with viewport but NEVER shrinks below 14px */
    .body-text {
      font-size: max(2vw, 14px);
      color: #94a3b8;
    }

    .label { font-size: 12px; color: #6366f1; font-family: monospace; margin-bottom: 6px; display: block; }
  </style>
</head>
<body>
  <div class="container">
    <span class="label">width: min(80%, 600px) — never exceeds 600px</span>
    <p class="body-text">font-size: max(2vw, 14px) — scales with viewport width, but never below 14px minimum!</p>
    <p style="font-size:12px;color:#64748b;">Resize the preview pane to see how the font adapts with min() and max() boundaries.</p>
  </div>
</body>
</html>
3. clamp() — Fluid Responsive Typography & Sizing

clamp(MIN, VAL, MAX) clamps a value between a lower and upper bound. Perfect for fluid headings that scale seamlessly without media queries!

<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: system-ui, sans-serif; background: #0f172a; color: #f8fafc; padding: 24px; }

    /* Fluid hero title: min 1.5rem, preferred 5vw, max 3.2rem */
    h1.hero-title {
      font-size: clamp(1.5rem, 5vw + 0.5rem, 3.2rem);
      color: #818cf8;
      margin: 0 0 12px;
    }

    /* Fluid padding that adapts between 12px and 40px */
    .fluid-card {
      background: linear-gradient(135deg, oklch(0.45 0.22 260), oklch(0.55 0.18 200));
      border-radius: 1rem;
      padding: clamp(12px, 3vw, 40px);
      margin-bottom: 12px;
    }

    p { font-size: clamp(0.875rem, 2vw, 1.125rem); color: rgba(255,255,255,0.8); }
    .label { font-size: 11px; font-family: monospace; color: #34d399; display: block; margin-top: 10px; }
  </style>
</head>
<body>
  <div class="fluid-card">
    <h1 class="hero-title">Fluid Typography with clamp()</h1>
    <p>Resize the preview pane to watch this heading scale fluidly between 1.5rem and 3.2rem — zero media queries needed!</p>
    <span class="label">font-size: clamp(1.5rem, 5vw + 0.5rem, 3.2rem)</span>
    <span class="label">padding: clamp(12px, 3vw, 40px)</span>
  </div>
</body>
</html>

3.5 Interactive Sandbox — Colors, Units & Math Functions

Experiment with OKLCH colors, rem typography, and dynamic clamp() fluid sizing live in the code playground below:

Live Playground — Chapter 3 Sandbox
Done with this chapter?
Mark it complete to track your progress and unlock your certificate.
Next Up

Learner Reviews

Write a Review
Share your experience to help other learners.
Your Rating *