random() CSS Function
The random() function returns a random value between a minimum and a maximum, such as random(0deg, 360deg), optionally in steps.
Description
The random() CSS function returns a random value between a minimum and a maximum. It can produce numbers, lengths, angles, times, percentages or any other numeric type, as long as the minimum and maximum are the same type. This lets you add natural variation to a design, such as scattering particles, varying the size or rotation of decorative shapes, or offsetting animation timings, without JavaScript.
The minimum and maximum are required; an optional third value is a step, so random(0px, 100px, 10px) only returns multiples of 10px. If the maximum is smaller than the minimum, the minimum is returned. An optional first argument, the random key, controls which random() calls share the same underlying random number. By default (auto) every call on every element is independent. element-scoped makes all calls with the same key on one element share a value, which is useful for keeping width and height equal; property-scoped gives every element the same value for that property; a custom --name shares one value everywhere it is used; and fixed followed by a number between 0 and 1 gives a repeatable result.
The random value is chosen once, when styles are computed, and does not change on every frame. It is a recent addition to CSS, so always provide a normal fallback value first.
/* Each star gets its own random position and size */
.star {
position: absolute;
top: random(0%, 100%);
left: random(0%, 100%);
width: random(--star element-scoped, 2px, 6px);
height: random(--star element-scoped, 2px, 6px); /* same as width */
}
/* Random rotation in 15deg steps */
.sticker {
rotate: random(-45deg, 45deg, 15deg);
}
/* Staggered animation start */
.bubble {
animation-delay: random(0s, 2s);
}
Syntax
random() = random( <random-key>?, <calc-sum>, <calc-sum>, <calc-sum>? )
<random-key> = auto | <random-cache-key> | fixed <number [0,1]>
<random-cache-key> = <dashed-ident> || element-scoped || [ property-scoped | property-index-scoped ]
Values
- <random-key>Optional. Controls which random() calls share a random number: auto (default, every call independent), element-scoped (shared within one element), property-scoped (shared by all elements for the same property), property-index-scoped, a custom <dashed-ident> such as --size (shared wherever it is used, optionally combined with a scope keyword), or fixed followed by a number from 0 to 1 for a fixed, repeatable value.
- minimumA <number>, <dimension> or <percentage>, or a calculation: the smallest value that can be returned.
- maximumA value of the same type: the largest value that can be returned. If it is less than the minimum, the minimum is returned.
- stepOptional. A value of the same type. The result is the minimum plus a whole number of steps, never going above the maximum.
Example
Browser Support
The following information will show you the current browser support for the CSS random() function. Hover over a browser icon to see the version that first introduced support for this CSS function.
Desktop





Tablets & Mobile





