Decorative cards often need small differences in rotation, spacing, or color. CSS random() produces a value in a range directly in the stylesheet, without a script that writes inline styles. Firefox 161 added support on November 24, 2026, and MDN now lists the function as Baseline 2026 newly available: MDN says it works in the latest versions of the major browsers since November 2026, though older browsers and devices may not support it, and MDN notes that some parts of the feature have varying levels of support. MDN random() reference · Firefox 161 developer release notes
The range is the easy part. The part that surprises people is which random() calls share a value and which do not.
The words you need
- CSS function: something like
calc()orrandom()that you write as a property value and the browser turns into a real value. - Range: the minimum and maximum
random()may return. - Step: an optional interval; results land on the minimum plus whole multiples of the step.
- Base value: MDN describes it as a seed, the starting number used to produce the final result. Two
random()calls with the same base value vary together; calls with different base values are independent. - Random key: the optional first argument that decides which calls share a base value.
@supports: a CSS rule that applies its contents only if the browser understands a given declaration.
Bound the variation
The basic form takes a minimum and maximum; an optional step restricts the possible values. A set of decorative cards can rotate by an angle between -3 and 3 degrees in one-degree steps:
.card {
rotate: random(-3deg, 3deg, 1deg);
}
Three rules from MDN keep the arguments valid:
- Minimum, maximum and step must be the same data type, such as all lengths or all angles. Units may differ, so
random(1em, 40px)is fine because both are lengths;random(1em, 30deg)is not. - With a step, the result is the minimum or the minimum plus a multiple of the step, never above the maximum.
random(-3deg, 3deg, 1deg)can only return whole degrees from -3 to 3. - If the maximum is less than the minimum, the function returns the first value.
This keeps the result within a predictable visual range. Keep the range decorative: MDN warns that uncontrolled values, such as random text colors, can hurt readability and contrast, and a generated value should not decide essential layout or state.
Choose which calls share a value
Before the range, random() accepts an optional random key. The browser uses it to build a cache name; calls with the same cache name get the same base value. MDN describes these options: MDN: <random-key> values
| Key | What shares a value |
|---|---|
auto (default) |
Nothing is shared across instances in one value, across properties, or across elements. Equivalent to element-scoped property-index-scoped. |
element-scoped |
Properties on the same element share a value (for example width and height); each element gets its own. |
property-scoped |
Each property gets its own value, but every element shares it. |
<dashed-ident>, e.g. --card-size |
Alone: every call with that name, on any element or property, shares one value. Combined with a *-scoped keyword, the keyword decides the sharing. |
fixed <number> |
No cache; the number (0 inclusive to 1 exclusive) is used as the seed value. |
A square card whose sides should vary together needs element-scoped; with the default, width and height would be picked independently:
.card {
width: random(element-scoped, 120px, 180px);
height: random(element-scoped, 120px, 180px);
}
The :hover gotcha
The default is not “a fresh value for every rule”. MDN’s example:
.foo {
width: random(100px, 200px);
}
.foo:hover {
width: random(100px, 200px);
}
Both declarations produce the same sharing identifier: same element, same property, same position in the value. So the width does not change on hover. The same applies to the first two values of a margin shorthand in a base rule and its :hover rule. MDN’s fix is to give the call a unique <dashed-ident>. MDN example of automatic sharing
.foo:hover {
width: random(--foo-hover element-scoped, 100px, 200px);
}
On its own, --foo-hover would give every hovered .foo the same width. Adding element-scoped keeps the name separate from the base rule while each element still gets its own value. That pairing is my reading of the key rules in the table above, not an example MDN gives.
More gotchas
- Custom properties do not store the value. If you write
--size: random(1px, 100px)and usevar(--size)twice, each use becomes its ownrandom()call with its own base value. MDN says this is different when the property is registered with@property: registered custom properties compute the random value and store it. - The default does not link properties. With
auto,widthandheighton the same element get independent values. Useelement-scopedor a shared name when they must match. - A shared name is global.
random(--x, ...)on its own returns the same base value on every element in the document. Addelement-scopedif each element should differ. - Contrast. Do not randomize text or background colors that must stay readable.
Guard older browsers
Because support is new, older browsers will drop a declaration they cannot parse. A fixed value with an @supports override keeps every card valid:
.card {
rotate: 0deg;
}
@supports (rotate: random(-3deg, 3deg, 1deg)) {
.card {
rotate: random(-3deg, 3deg, 1deg);
}
}
The keys and step syntax are defined in the CSS Values and Units Level 5 draft. The snippets here follow MDN and the draft; they were not checked across browsers for this article, so test them in the browsers your audience uses.
Conclusion
Use random() for bounded, decorative variation, and choose the key on purpose: element-scoped to link properties on one element, a <dashed-ident> when you need a separate value, such as in a :hover rule. Keep an @supports fallback for browsers older than the November 2026 releases.