Sub topic 11
Passing dynamic values with CSS custom properties
This chapter assumes you can write React function components, know basic CSS selectors, and have already imported a .module.css file into a component. Class names in a CSS Module are fixed at build time. Some values only exist at runtime, such as a progress percentage or a color from user data. This chapter shows how to pass those values into the stylesheet.
The problem with runtime values
A CSS Module cannot read props. You could build a class for every possible value, but that does not scale: a progress bar would need 101 classes. You could also put the value in an inline style prop, but then the component owns the width rule and the stylesheet loses control of the layout.
CSS custom properties solve this. The stylesheet declares how a value is used. The component supplies the value. A custom property is a CSS variable whose name starts with --, read with var().
Reading the variable in the stylesheet
.bar {
width: var(--progress);
height: 8px;
background: #16a34a;
}The .bar class still gets a scoped, generated name. Only the value of --progress comes from outside.
Setting the variable from the component
import type { CSSProperties } from 'react';
import styles from './Progress.module.css';
export function Progress({ value }: { value: number }) {
return (
<div
className={styles.bar}
style={{ '--progress': `${value}%` } as CSSProperties}
/>
);
}React writes --progress onto the element's inline style. Custom properties inherit, so the element and its descendants can read the value. When value changes, React updates the inline style and the browser repaints. No new class is created.
The as CSSProperties cast is needed only in TypeScript. React's CSSProperties type does not list custom properties, so an object literal with --progress fails type checking without it. In plain JSX you can omit the cast.
When to use this approach
Use custom properties for values that vary continuously or come from data:
- Widths, heights, and positions computed from props.
- Colors chosen by a user or returned by an API.
- Delays and durations for animations.
Use classes for a small, fixed set of states, such as primary and secondary variants. Classes are easier to read and to override.
Fallback values and common errors
var() accepts a fallback as its second argument. This keeps the element usable when the component forgets to set the variable.
.bar {
width: var(--progress, 0%);
}Watch for these mistakes:
- Missing units.
--progress: 40makeswidth: 40invalid. Include%orpxin the value, or usecalc(var(--n) * 1px)in the CSS. - Not escaping the name. Custom property names are not renamed by CSS Modules. Only class names and similar identifiers are scoped, so
--progressis global to the element tree. Pick specific names if components nest. - Passing untrusted strings. Values are written into a style attribute. Validate or clamp numbers first, for example
Math.min(100, Math.max(0, value)).
Exercise
A Badge component receives a color prop such as #dc2626. How do you apply it as the background without creating a class per color? Name the two places you change.
Show answer
In the CSS Module, add background: var(--badge-color, #6b7280); to the .badge rule. In the component, pass style={{ '--badge-color': color } as CSSProperties} (the cast is needed only in TypeScript) along with className={styles.badge}.