Firefox 161, released on Monday, supports the CSS random() function. Mozilla's notes for developers describe it as generating "a random value within a specified range", optionally stepped, with an optional key to share one random value across several uses.
It is a lovely feature for confetti, scattered cards and organic-looking layouts. It also introduces something CSS never had before: a stylesheet that renders differently every time the page loads. That matters the day somebody sends you a screenshot of a layout bug.
What the spec says about when the dice are rolled
The specification, CSS Values and Units Level 5, is explicit that a random value is tied to a "random cache name", so that every time the function is evaluated with the same name "it will return the same random base value". Part of that name is "a document identifier identifying the Document the styles are from".
Two consequences follow, and both matter for debugging:
- Within one page view, the value is stable for the same element: re-renders, resizes and style recalculations do not reroll it.
- Every new document gets new values. Reload the page and the layout is drawn again from fresh random numbers.
So the reporter's page had one set of values, and when you open the same URL you get another. If the bug only happens when a card lands at 290px and overlaps the sidebar, it is real, it is in their screenshot, and reloading until you see it is now a game of chance.
The key changes how often it differs
The optional <random-key> decides what shares a value. With the default, auto, the spec says the value "varies across every random() instance in a multi-component value, across different properties, and across different elements". A named key such as --card makes every use of that name share one value; element-scoped and property-scoped share along one of those axes.
Read your own stylesheet with that in mind. A random() with no key on twenty elements is twenty independent rolls, so the number of distinct layouts a user can see is very large, and the one in the bug report may be rare.
The escape hatch the spec already provides
The same grammar includes fixed <number [0,1]>. In the spec's words, with fixed the function "bypasses the random cache name entirely, and just uses the <number> as its random base value directly".
That gives you a deterministic mode for free, and it is worth wiring in before the first report arrives: a test-only stylesheet that overrides each rule with a fixed key, for example random(fixed 0.5, -4deg, 4deg) instead of random(-4deg, 4deg). Run the visual checks at fixed 0 and fixed 1 as well, because the extremes of the range are exactly where overlap bugs live.
Capture the value, not just the picture
A screenshot shows that something overlapped. It does not tell you which random value produced it. What does is the size the element actually ended up with, which getComputedStyle reports for layout properties like width:
const cards = [...document.querySelectorAll('.card')]
.map(el => ({ id: el.id, width: getComputedStyle(el).width }))
Attach that to the bug report alongside the screenshot, and you can reproduce the layout by setting the same sizes, instead of reloading until chance agrees with you.
The general version
Most "cannot reproduce" reports are about state the developer does not have: a cookie, an extension, a slower network. random() adds a new kind, state that nobody has, because it was generated for that one page view and discarded. The only fix is to record it when the report is made, or to make it deterministic before anybody needs to.
Browser support beyond Firefox 161 is not covered here; check it before relying on any of this in production.
Top comments (1)
Running the visual checks at
fixed 0andfixed 1is the tip I'd push hardest, because it changes who reviews what. The range is the design decision now, and it lives in a stylesheet nobody opens.random(-4deg, 4deg)gets signed off from one screenshot that happened to land near zero.I'd put both extremes in as stories (Storybook or whatever you use) so whoever approves the design actually looks at -4 and 4 side by side. Same with the key. No key on 20 cards means 20 separate rolls, and nobody reading the mockup would guess that.