LinkedIn series demo

CSS random()

Every scattered layout I have ever built took JavaScript, or a pile of hand-written nth-child rules. CSS is getting a random() function that does it in the stylesheet. This page puts sixteen dots on a stage with it.

1. What your browser reports

Support for random():

Checking...

Reading it live with CSS.supports.

2. The demo: a scatter with no positions written

Scattered by: checking...

No dot on that stage has a position written for it individually. Where random() is supported, the stylesheet rolls each dot's position, size, corner radius, rotation, and hue, per element, fresh on every page load. Reload the page and the field changes. Where it is not supported yet, a small script does the same rolls so the stage still shows what the CSS will do, and the label above tells you honestly which one you are looking at.

3. The pattern

One rule does the whole stage. Each dot that matches gets its own values:

.dot {
  left: random(2%, 90%);
  top: random(4%, 82%);
  width: random(10px, 28px);
  height: random(10px, 28px);
  border-radius: random(25%, 50%);
  rotate: random(-35deg, 35deg);
  background: hsl(random(248, 292) 85% 68%);
}

Two things worth knowing. The arguments must agree on type: percentages with percentages, pixels with pixels. And the optional first argument is a sharing key. Leave it off and every call rolls independently. Give several calls the same key, like random(--seed, 0, 360), and they share one roll, which is how you keep a width and a height in step, or a hue consistent across elements.

4. Honest notes

Why write it today, then? Because the fallback is tiny. An @supports block for the browsers that have it, a hand-scattered default or a few lines of script for the rest, and the day Chrome ships it, the stylesheet quietly takes over.