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.
Top comments (0)