CSS & layout

CSS random() remembers its roll, and the cache key decides when

CSS random() remembers its roll, and the cache key decides when

You tilt every sidenote by a random couple of degrees. Then a media query bumps the scale at wider viewports, you repeat the rotate() so it doesn't get wiped, and you brace for every note to jump to a new angle when the window crosses the breakpoint. It doesn't jump. Same tilt on both sides.

Mat Marquis explains why in a Piccalilli article published 18 November. His demo is a column of sidenotes, each scaled and rotated with random():

.sidenote {
  transform: scale(0.925) rotate(random(-2.5deg, 2.5deg));
}

@media (min-width: 55rem) {
  .sidenote {
    transform: scale(1) rotate(random(-2.5deg, 2.5deg));
  }
}

The function takes a lower bound, an upper bound and an optional step. His example: random(0px, 10px, 5px) resolves to 0, 5 or 10.

Seeds, keys and the index trap

Each call generates a seed between 0 and 1 and computes its value from that. Two calculations built on the same seed always give the same result. Whether two calls share a seed comes down to the cache key, and Marquis says the default key is scoped to the element, the property name, and the index of the call within that property's value. Same .sidenote, same transform, first random() in the value. Same key, so the same angle. That's why the rotation holds across the breakpoint, and why it also holds when he repeats it inside @keyframes.

The index is the part that bites. Marquis's margin example:

div {
  margin-block: random(1rem, 5rem);
}

@media (min-width: 55rem) {
  div {
    margin-block: random(1rem, 5rem) random(1rem, 5rem);
  }
}

At the wider size, margin-block-start keeps its value because it is still the first call. The second call has a new index, so margin-block-end gets a fresh roll. Add property-scoped and the index drops out of the key, so both values reuse one result: random(property-scoped, 1rem, 5rem). He also covers element-scoped, named dashed identifiers like --random-key, and fixed, which pins the seed so you can preview outcomes. random(fixed 0.5, 0deg, 20deg) gives 10deg.

One gotcha to remember. Put random(0px, 10px) in a custom property, read it with var() in two rules, and the rules don't share a value. Marquis's point is that the custom property holds the call, not its result, so every use rolls again. If you want sharing, use the scoping keywords.

On support, Marquis calls random() brand new and links to caniuse for the current picture. Prototype the sidenotes now. Learn the key before you need it.

Source: Piccalilli (piccalil.li)

Related
CSS & layout

Staggering scroll-driven animations takes one animation-range-start line

On CSS-Tricks, Lee Meyer replaces a formula-heavy stagger technique with a single animation-range-start declaration built on sibling-index(). The reason is that animation-delay has no effect on scroll-driven animations.

November 17, 2026
CSS & layout

CSS random() puts a little deliberate mess back into your layout

In Smashing Magazine, Brecht De Ruyte works through the native random() function from CSS Values and Units Level 5 with demos of jittered letters, bouncing type and splayed cards, and draws a clear line around where randomness does not belong. He reports that Safari shipped it first, Chrome 156 is rolling it out, and Mozilla has sent positive signals about Firefox.

October 20, 2026
CSS & layout

A polyfill for CSS random(), while the browsers catch up

Lee Meyer's walkthrough on CSS-Tricks builds a polyfill for the emergent CSS random() function so authors can start using it — and get fluent in its caching keywords — before every engine ships.

September 9, 2026