5.7 · Logic in CSS
Real randomness: random()
Random values in CSS, without a script writing inline styles.
random(0%, 95%) returns a random value in a range, and
by sets a step. Each element and each property gets its
own value by default, so 24 dots get 24 positions.
A caching key controls sharing:
random(--size, 1rem, 3rem) gives the same value wherever
that name is used. Values are fixed for the page load. Reload the demo
to roll again.
dots.forEach((dot) => {
dot.style.left = Math.random() * 95 + '%'
dot.style.setProperty(
'--hue', Math.random() * 360,
)
})
.dot {
inset-inline-start: random(0%, 95%);
inset-block-start: random(0%, 90%);
background: oklch(75% 0.15 random(0, 360));
scale: random(0.5, 1.5);
}