2026.11.23 · 5 min · css · browsers · animation
A night sky of random stars, in pure CSS.
CSS random(min, max) gives every element its own value, so top and left: random(0%, 100%) scatters a field of stars with no JavaScript. The grammar, when a value is rolled and when it isn't, the named-key trap that piles every star onto one spot, and who supports it. As of 2026.11.
Until now, a random layout in CSS meant JavaScript writing a custom property onto every element, or a hand-picked list of nth-child positions. Now it's one declaration: top: random(0%, 100%). Give sixty <i> elements that line and a matching left, and each one lands somewhere different. The same idea works for confetti, particles, tilted cards and staggered animation delays.
Watch the 60-second version on Instagram.
One declaration, a different value per element
Chrome 156, stable on 2026.10.20, shipped random(). In the release notes' words, "By default, each random() function resolves to a new, distinct value." With no key, that means a separate value for each element, each property and each random() within a declaration. So the two measured stars in the figure have different top and left values from the same two lines of CSS. The positions in the figure are illustrative.
The grammar is random( <random-key>?, <min>, <max>, <step>? ):
- Min and max are inclusive.
- The step is a plain third number. There's no
bykeyword:random(100px, 300px, 50px)gives 100, 150 … 300px. - Min and max must resolve to the same type: a length, percentage, angle, time or number.
random(50px, 180deg)is invalid. Smashing says the bounds need "the exact same unit", but the spec allows50pxwith1em, as long as both resolve to lengths.
The whole sky
The HTML is 60 <i></i> inside <div class="sky">. CSS can't create elements, so the stars have to exist in the markup.
.sky { position: fixed; inset: 0; overflow: hidden; background: #0b1020; }
.sky i {
position: absolute;
top: random(0%, 100%);
left: random(0%, 100%);
width: random(1px, 4px, 1px);
aspect-ratio: 1; /* square without needing a shared key */
border-radius: 50%;
background: #fff;
opacity: random(0.3, 1);
animation: twinkle 2s random(0s, 3s) infinite alternate;
}
@keyframes twinkle { to { opacity: 0.2; } }
@supports not (top: random(0%, 100%)) {
.sky i { display: none; } /* or a static star image */
}It uses only what Chrome 156 stable supports: bare random() and a step. I wrote it from the spec and the docs and haven't run it, so treat it as a starting point. width: random(1px, 4px, 1px) picks one of four sizes, and aspect-ratio: 1 makes the height follow the width. overflow: hidden matters: left: 100% puts a star past the right edge. Clip it, or use calc(100% - size) as the max.
When does it re-roll?
Not every frame, and not every repaint. A value is rolled once per element and property and then stays put. The spec ties it to the element itself (its lifetime is the same as a JavaScript reference to that element), so a restyle or a :hover doesn't re-roll it. A reload creates new elements, so you get a new sky. That follows from the spec; I haven't tested it.
That also answers the question people kept asking on Hacker News: can you get the same sky every time? Not in Chrome today. The fixed option isn't supported there, and a named key, which looks like a seed, does something else.
The named-key trap
random(--x, 0%, 100%) looks like it should give each star its own --x. It doesn't. A named key is now global: every element that uses the same name gets the same value. Put --x on left and --y on top, and all twelve stars in the figure land on one spot. To get one value per element with a key, the spec has element-scoped. The star positions in the figure are illustrative.
What people get wrong
- "It changes every frame." No. It's rolled once per element and property, then stays.
- "A name like
--xmakes it per-star." It makes every star share one value. - "
--size: random(…)gives me a square." An unregistered custom property re-rolls at eachvar()use, sowidth: var(--size); height: var(--size)comes out as a rectangle. Useaspect-ratio: 1, orelement-scopedonce Chrome ships it. - "
element-scopedworks in Chrome 156." MDN's compatibility data listsauto,element-scopedandproperty-scopedas preview only, andfixedas unsupported. My inference from that, untested:element-scopedin stable Chrome makes the declaration invalid. - "Random looks random." Uniform random clumps: some stars bunch up and leave gaps. One Hacker News commenter put it bluntly: random "often looks pretty bad".
Who has it
| Browser | random() |
|---|---|
| Safari 26.2 (2025.12.12) | Yes, first to ship |
| Chrome / Edge 156 | Yes: bare random(), named keys and step |
| Firefox 161 | Listed, ships 2026.11.24 |
Polypane found some Safari demos broken, especially random() inside animations, so test the twinkle there. For everything older, the feature check is @supports not (order: random(1, 2)), or the top version in the code above.
On a client project I'd use random() for decoration like this sky today, behind an @supports fallback, and keep anything that has to look the same for every visitor on fixed values.
The short version
random(min, max)gives every element its own value:topandleft: random(0%, 100%)scatters stars with no JavaScript.- It's rolled once per element and property and doesn't change on hover or restyle; a reload gives a new sky.
- A named key like
--xis global: every element shares that one value. - Use
aspect-ratio: 1for squares, not a shared custom property. - Safari 26.2 and Chrome 156 have it now, Firefox 161 is due 2026.11.24; add an
@supportsfallback.
Sources: Chrome 156 release notes · CSS Values and Units Level 5, randomness · MDN, random() · WebKit, Safari 26.5 features · WebKit, Safari 26.2 features · MDN browser-compat-data, random · Firefox 161 for developers · Polypane, Experimenting with CSS random() · WebKit, Rolling the Dice with CSS random() · Hacker News discussion