Values, Units, and Modern CSS Data Types

1. Length Units (px, em, rem, ch, ex, vw, vh, vi, vb)

Unit Type Description Relative To Use Case
px Absolute Pixels - 1px = 1/96th of 1 inch Device pixel ratio Borders, shadows, fixed layouts
em Relative Relative to parent element's font-size Parent font-size Spacing within components
rem Relative Relative to root (html) element font-size Root font-size (16px default) Consistent spacing, typography
% Relative Percentage of parent element Parent dimension Fluid layouts, responsive sizing
vw Viewport 1% of viewport width Viewport width Full-width elements, responsive typography
vh Viewport 1% of viewport height Viewport height Full-height sections, hero images
vmin Viewport 1% of smaller viewport dimension min(vw, vh) Square elements, responsive sizing
vmax Viewport 1% of larger viewport dimension max(vw, vh) Cover backgrounds, overlays
ch Font-relative Width of "0" character in font Font character width Input fields, monospace layouts
ex Font-relative Height of "x" character (x-height) Font x-height Vertical alignment, typography
vi NEW Logical Viewport 1% of viewport inline direction Inline axis (width in LTR) Logical responsive design
vb NEW Logical Viewport 1% of viewport block direction Block axis (height in LTR) Logical responsive design
svw/svh NEW Small Viewport Small viewport units (mobile UI visible) Small viewport size Mobile with UI bars visible
lvw/lvh NEW Large Viewport Large viewport units (mobile UI hidden) Large viewport size Mobile with UI bars hidden
dvw/dvh NEW Dynamic Viewport Dynamically updates with viewport changes Current viewport size Handles mobile browser UI changes

Example: Length unit comparisons

/* Absolute */
.box { width: 300px; } /* Fixed size */

/* Font-relative */
.text { 
    font-size: 1.5rem;    /* 24px if root is 16px */
    padding: 1em;         /* 24px (relative to own font-size) */
    margin-bottom: 2rem;  /* 32px (relative to root) */
}

/* Viewport units */
.hero { height: 100vh; }    /* Full viewport height */
.banner { width: 100vw; }   /* Full viewport width */
.square { width: 50vmin; height: 50vmin; } /* Square */

/* Font-based */
input { width: 20ch; }  /* ~20 characters wide */

/* New viewport units for mobile */
.mobile-hero {
    height: 100dvh;  /* Dynamic - handles browser UI */
}
Note: Use rem for typography and spacing (consistent, accessible). Use em for component-scoped spacing. Use px for borders and shadows. Use dvh/dvw for mobile viewport issues.

2. Color Values (hex, rgb, hsl, hwb, lab, lch, oklab)

Format Syntax Description Example Support
Named Colors name 147 predefined color keywords red, blue, tomato All
Hexadecimal #RRGGBB / #RGB RGB values in hex (optional alpha) #ff0000, #f00, #ff0000ff All
RGB rgb(r g b / a) Red, Green, Blue (0-255 or 0-100%) rgb(255 0 0 / 0.5) All
HSL hsl(h s l / a) Hue (0-360°), Saturation, Lightness hsl(0 100% 50%) All
HWB NEW hwb(h w b / a) Hue, Whiteness, Blackness (intuitive) hwb(0 0% 0%) Modern
LAB NEW lab(l a b / alpha) Perceptual color space (wide gamut) lab(50% 40 -50) Modern
LCH NEW lch(l c h / alpha) Lightness, Chroma, Hue (perceptual) lch(50% 70 180) Modern
OKLAB NEW oklab(l a b / alpha) Improved LAB with better hue uniformity oklab(0.5 0.1 -0.1) Modern
OKLCH NEW oklch(l c h / alpha) Improved LCH (best for manipulation) oklch(0.5 0.2 180) Modern
color() NEW color(space r g b) Specify color space explicitly color(display-p3 1 0 0) Modern

Example: Color format usage

/* Traditional formats */
.hex { color: #ff0000; }
.rgb { color: rgb(255 0 0); }
.rgba { color: rgb(255 0 0 / 0.5); }
.hsl { color: hsl(0 100% 50%); }
.hsla { color: hsl(0 100% 50% / 0.5); }

/* Modern formats - better for manipulation */
.hwb { color: hwb(0 0% 0%); }  /* Pure red */
.lch { color: lch(50% 70 40); } /* Perceptual uniformity */
.oklch { color: oklch(0.6 0.25 30); } /* Best for gradients */

/* Wide gamut colors */
.p3 { color: color(display-p3 1 0 0); } /* Vivid red */
.rec2020 { color: color(rec2020 1 0 0); }

/* Transparent keyword */
.transparent { background: transparent; }

/* currentColor - inherits text color */
.border { border: 2px solid currentColor; }
Note: Use OKLCH for color manipulation (tints, shades, palettes) - it's perceptually uniform. Use HSL for broad compatibility. Use RGB/Hex for simple colors.

3. CSS Custom Properties (Variables) and calc()

Feature Syntax Description Example
Declaration --name: value; Define custom property with double dash --primary-color: blue;
Usage var(--name) Reference custom property value color: var(--primary-color);
Fallback var(--name, fallback) Provide fallback if variable undefined color: var(--color, red);
Scope :root, element Variables inherit down DOM tree :root { --size: 16px; }
calc() calc(expression) Mathematical calculations with mixed units calc(100% - 20px)
Operators + - * / Addition, subtraction, multiplication, division calc(2 * 10px + 5%)

Example: CSS Custom Properties (Variables)

/* Global variables in :root */
:root {
    --primary-color: #007acc;
    --secondary-color: #ff6600;
    --spacing-unit: 8px;
    --font-size-base: 16px;
    --border-radius: 4px;
}

/* Using variables */
.button {
    background: var(--primary-color);
    padding: calc(var(--spacing-unit) * 2);
    border-radius: var(--border-radius);
    font-size: var(--font-size-base);
}

/* Scoped variables */
.dark-theme {
    --primary-color: #4fc3f7;
    --bg-color: #1e1e1e;
}

/* Fallback values */
.card {
    padding: var(--card-padding, 20px); /* 20px if not defined */
}

/* Nested var() */
.element {
    --size: 100px;
    --half: calc(var(--size) / 2);
    width: var(--half); /* 50px */
}

Example: calc() function usage

/* Mixed unit calculations */
.container {
    width: calc(100% - 40px);
    height: calc(100vh - 60px);
    padding: calc(1rem + 10px);
}

/* With variables */
.responsive {
    --header-height: 60px;
    --footer-height: 40px;
    height: calc(100vh - var(--header-height) - var(--footer-height));
}

/* Complex expressions */
.grid {
    width: calc((100% - 3 * 10px) / 4); /* 4 columns with 10px gaps */
}

/* Negative values */
.offset {
    margin-left: calc(-1 * var(--spacing));
}

/* Nested calc (auto-flattened) */
.element {
    width: calc(calc(100% / 3) - 20px);
}

4. CSS Functions (min, max, clamp, round, mod, rem) NEW

Function Syntax Description Use Case
min() min(val1, val2, ...) Returns smallest value from list Maximum width constraints
max() max(val1, val2, ...) Returns largest value from list Minimum width constraints
clamp() clamp(min, ideal, max) Value between min and max bounds Responsive typography, fluid sizing
round() NEW round(strategy, val, step) Round to nearest, up, down, or to-zero Grid alignment, snapping
mod() NEW mod(dividend, divisor) Modulo operation (remainder) Cyclic patterns, alternating styles
rem() NEW rem(dividend, divisor) Remainder operation (like % in calc) Pattern calculations
abs() NEW abs(value) Absolute value Distance calculations
sign() NEW sign(value) Returns -1, 0, or 1 Directional logic

Example: min(), max(), clamp() for responsive design

/* min() - maximum constraint */
.container {
    width: min(100%, 1200px); /* Never wider than 1200px */
    padding: min(5vw, 40px);  /* Responsive but capped */
}

/* max() - minimum constraint */
.button {
    font-size: max(16px, 1vw); /* Never smaller than 16px */
    padding: max(10px, 1em);
}

/* clamp() - fluid between bounds */
.heading {
    font-size: clamp(1.5rem, 5vw, 3rem);
    /* Min: 1.5rem, Ideal: 5vw, Max: 3rem */
}

.responsive-spacing {
    margin: clamp(1rem, 3vw, 3rem);
    padding: clamp(0.5rem, 2vw, 2rem);
}

/* Combining functions */
.card {
    width: clamp(200px, 50%, 500px);
    padding: max(1rem, 2vw);
    gap: min(20px, 3%);
}

Example: New math functions (round, mod, rem)

/* round() function */
.grid-item {
    /* Round to nearest 10px */
    width: round(nearest, 23.7px, 10px); /* = 20px */
    
    /* Round up */
    height: round(up, 43px, 10px); /* = 50px */
    
    /* Round down */
    margin: round(down, 47px, 10px); /* = 40px */
}

/* mod() for cyclic patterns */
.item {
    /* Alternate colors every 3 items */
    --index: 5;
    --cycle: mod(var(--index), 3); /* = 2 (5 % 3) */
}

/* rem() for remainder */
.pattern {
    --offset: rem(127px, 50px); /* = 27px */
}

/* abs() for absolute values */
.transform {
    --distance: abs(-50px); /* = 50px */
}

/* Practical example: snap to grid */
.snap-grid {
    width: round(nearest, var(--dynamic-width), 20px);
    height: round(nearest, var(--dynamic-height), 20px);
}

5. Container Query Units (cqw, cqh, cqi, cqb) NEW

Unit Description Relative To Use Case
cqw 1% of container's width Query container width Component responsive typography
cqh 1% of container's height Query container height Vertical responsive components
cqi 1% of container's inline size Container inline dimension Logical responsive design
cqb 1% of container's block size Container block dimension Logical responsive design
cqmin Smaller of cqi or cqb Container smaller dimension Square components
cqmax Larger of cqi or cqb Container larger dimension Cover layouts

Example: Container query units setup

/* Define container */
.container {
    container-type: inline-size;
    /* or: size (both dimensions) */
    /* or: normal (default, no containment) */
    container-name: card; /* Optional name */
}

/* Use container query units inside */
.card-title {
    font-size: clamp(1rem, 5cqw, 2rem);
    /* Responsive to container width, not viewport */
}

.card-content {
    padding: 2cqw;  /* 2% of container width */
    gap: 1cqh;      /* 1% of container height */
}

/* Named container queries */
@container card (min-width: 400px) {
    .card-title {
        font-size: 3cqw; /* 3% of container named "card" */
    }
}

/* Component entirely responsive to its container */
.product-card {
    container-type: inline-size;
}

.product-title {
    font-size: clamp(0.875rem, 4cqi + 0.5rem, 1.5rem);
}

.product-image {
    width: 100%;
    height: 50cqh; /* 50% of container height */
}
Note: Container query units enable component-scoped responsive design. Use cqw/cqi for typography and spacing relative to container, not viewport. Requires container-type on parent.

6. CSS4 Math Functions and Trigonometry NEW

Function Syntax Description Returns
sin() sin(angle) Sine of angle Number between -1 and 1
cos() cos(angle) Cosine of angle Number between -1 and 1
tan() tan(angle) Tangent of angle Number (unbounded)
asin() asin(number) Arc sine (inverse sine) Angle in radians
acos() acos(number) Arc cosine (inverse cosine) Angle in radians
atan() atan(number) Arc tangent (inverse tangent) Angle in radians
atan2() atan2(y, x) Arc tangent of y/x in correct quadrant Angle in radians
sqrt() sqrt(number) Square root Non-negative number
pow() pow(base, exponent) Exponentiation (base^exponent) Number
hypot() hypot(x, y, ...) Hypotenuse: √(x² + y² + ...) Non-negative number
log() log(value, base?) Logarithm (default base e) Number
exp() exp(exponent) e raised to power (e^x) Number

Example: Trigonometric functions for circular layouts

/* Circular positioning */
.item {
    --angle: 45deg;
    --radius: 100px;
    
    /* Calculate x, y positions on circle */
    --x: calc(cos(var(--angle)) * var(--radius));
    --y: calc(sin(var(--angle)) * var(--radius));
    
    transform: translate(var(--x), var(--y));
}

/* Diagonal line length using Pythagorean theorem */
.diagonal {
    --width: 100px;
    --height: 100px;
    --length: hypot(var(--width), var(--height)); /* √(100² + 100²) */
}

/* Wave animation offset */
@keyframes wave {
    to {
        transform: translateY(calc(sin(var(--time)) * 20px));
    }
}

/* Circular progress indicator */
.progress-circle {
    --progress: 0.75; /* 75% */
    --angle: calc(var(--progress) * 360deg);
    background: conic-gradient(
        blue calc(var(--angle)),
        lightgray 0
    );
}

/* Scaling with easing curves */
.smooth-scale {
    --t: 0.5; /* time factor 0-1 */
    scale: calc(1 + sin(var(--t) * 3.14159));
}

Example: Power and logarithmic functions

/* Exponential scaling */
.exponential {
    --base: 2;
    --level: 3;
    font-size: calc(pow(var(--base), var(--level)) * 1px); /* 2³ = 8px */
}

/* Square root for maintaining aspect ratio */
.aspect-box {
    --area: 10000; /* 100 x 100 */
    width: calc(sqrt(var(--area)) * 1px); /* 100px */
    height: calc(sqrt(var(--area)) * 1px);
}

/* Logarithmic scaling (audio visualizers) */
.audio-bar {
    --frequency: 1000;
    --scale: log(var(--frequency), 10); /* log base 10 */
    height: calc(var(--scale) * 20px);
}

/* Natural exponential growth */
.growth {
    --rate: 0.5;
    scale: exp(var(--rate)); /* e^0.5 ≈ 1.648 */
}
Warning: Trigonometric functions use radians or degree units. Use deg, rad, grad, or turn units. Performance impact on complex calculations - use sparingly.

7. CSS Typed OM and Houdini Properties BETA

Feature Syntax Description Use Case
@property @property --name { } Register custom property with type, syntax, inheritance Animated custom properties, type safety
syntax syntax: "<color>"; Define value type: color, length, number, etc. Type checking, animation interpolation
inherits inherits: true | false; Whether property inherits down tree Control inheritance behavior
initial-value initial-value: value; Default value for property Fallback, required for animation
Syntax Type Value Description Example
<length> Length value Distances with units (px, rem, etc.) 10px, 2rem
<number> Numeric value Unitless numbers 1.5, 42
<percentage> Percentage Values with % unit 50%, 100%
<length-percentage> Length or % Either length or percentage 10px, 50%
<color> Color value Any valid color #fff, rgb(...)
<image> Image value URLs, gradients url(...), linear-gradient(...)
<angle> Angle value Angles with units (deg, rad, etc.) 45deg, 1rad
<time> Time value Duration with units (s, ms) 2s, 500ms
* Universal Any value (no type checking) Any valid CSS value
| Or operator Multiple allowed types "<length> | <percentage>"

Example: @property registration for animations

/* Register custom property with type */
@property --gradient-angle {
    syntax: "<angle>";
    inherits: false;
    initial-value: 0deg;
}

/* Now can animate it smoothly */
.gradient-box {
    background: linear-gradient(
        var(--gradient-angle),
        blue,
        purple
    );
    transition: --gradient-angle 1s;
}

.gradient-box:hover {
    --gradient-angle: 360deg; /* Smooth rotation */
}

/* Numeric property for smooth transitions */
@property --progress {
    syntax: "<number>";
    inherits: false;
    initial-value: 0;
}

.progress-bar {
    --progress: 0.75;
    width: calc(var(--progress) * 100%);
    transition: --progress 500ms ease-out;
}

Example: Complex @property definitions

/* Color property for theme switching */
@property --theme-color {
    syntax: "<color>";
    inherits: true;
    initial-value: #007acc;
}

/* Multiple types allowed */
@property --spacing {
    syntax: "<length> | <percentage>";
    inherits: true;
    initial-value: 1rem;
}

/* Universal syntax (any value) */
@property --custom-value {
    syntax: "*";
    inherits: false;
    initial-value: auto;
}

/* Length with specific constraints */
@property --max-width {
    syntax: "<length>";
    inherits: false;
    initial-value: 1200px;
}

/* Usage in animations */
@keyframes colorChange {
    from { --theme-color: blue; }
    to { --theme-color: red; }
}

.animated {
    animation: colorChange 2s infinite;
    background: var(--theme-color);
}
Note: @property enables smooth animation of custom properties by defining their type. Without it, custom properties animate discretely (no interpolation). Required for animating colors, numbers, angles, etc.

Value & Unit Best Practices

  • Use rem for typography and spacing (accessibility)
  • Use clamp() for fluid responsive typography
  • Use OKLCH/LCH for color manipulation and gradients
  • Use container query units for component-scoped responsive design
  • Register custom properties with @property for smooth animations
  • Use calc() with CSS variables for maintainable calculations
  • Use dvh/dvw instead of vh/vw on mobile for browser UI issues