Just lately, I printed a story in regards to the new random features which have landed in CSS and the way they work. On this article, we’ll discover the challenges of randomness in CSS, how the idea has developed over time, and why this native function is an enormous deal.
One of many first issues I wished to do after I began creating web sites was create distinctive experiences that modified from individual to individual. Simply little issues: a random background right here, random colours there… Even small micro-interactions, like confetti or falling snow, wanted some stage of randomness to really feel pure.
And I used to be not alone! I quickly found that many internet builders (“site owners,” on the time) wished to do issues like that: including wow elements and a way of uniqueness to their websites. However we had an issue: CSS.
CSS is a declarative and deterministic language. Two traits that conflict with the concept of pure variation:
- Declarative signifies that it focuses on the what, not the how. In distinction to crucial languages, builders utilizing CSS inform the browser what the anticipated result’s, however not the best way to obtain it.
- Deterministic signifies that for a given enter we are going to get the identical output. All the time the identical. When you specify {that a} shade shall be pink, that shade shall be pink, not blue or yellow.
That is by design, and it’s one of many issues that makes CSS predictable and dependable. When you perceive how the structure engine works, you may inform which kinds shall be utilized at any given time. Which is nice… however not so nice if you wish to generate random content material.
And so started a difficult (and generally tortuous) journey for designers and builders to attain pure variation from a deterministic system.
The Lengthy and Winding Street to Random Kinds
The trail to random kinds in CSS is paved with a number of makes an attempt and shortcomings. However at each step alongside the best way, builders discovered new options that improved on the earlier ones. Even when solely a bit.
Be aware: This timeline displays logical progress greater than a strict historic or chronological order.
CSS Pseudo-Randomness and Patterns
We are able to simulate randomness in CSS by creating patterns. However this isn’t really random. The outcomes will at all times be the identical, and in the end folks will discover the sample.
One solution to create this simulation is through the use of :nth-child() selectors or by enjoying with animations. The primary methodology is simple however yields subpar outcomes; the second might trick and impress some folks.
Warning: Auto-playing media

Evidently, these strategies are hacks that don’t present randomization at any stage. A human might not be capable of exactly predict which worth comes subsequent — not less than not with out some effort — however a machine actually can.
Pre-Processors to the Rescue
We turned to the subsequent smartest thing: tooling. Specifically, CSS preprocessors akin to Sass, SCSS, Much less, and the like. These instruments embody math modules that present random features we are able to use at compilation time.
The important thing phrase within the earlier paragraph is “at compilation time.” Sure, we’re producing random values for our CSS properties. However as soon as these values are produced throughout compilation, they’re frozen ceaselessly (or till the subsequent compilation, to be extra exact). Similar to a mosquito caught in amber.
The values shall be random when the CSS is generated, however each time guests go to or refresh the web page, they’ll get the identical ones. To provide new values, we would wish to recompile the stylesheets.
This was a child step towards styling randomization, however there was nonetheless an extended solution to go.
Server-Facet Randomness
We moved to the subsequent smartest thing: utilizing different languages to generate random values and passing them to CSS by way of HTML. Server-side languages like PHP, Java, ASP, and others have been good for this activity whereas producing the HTML (and even the CSS itself).
This method works effectively: we get new random values each time the web page is generated, which normally means each time it’s visited or refreshed. We even have full management over the randomization, since we are able to implement our personal features.
It has shortcomings, too. If new content material is added dynamically to the web page, it will get caught with the “frozen” values generated throughout the preliminary web page load. Higher than patterns, higher than preprocessors… however nonetheless not good.
This limitation turned an excellent larger drawback with the rise and widespread adoption of single-page functions and client-side JavaScript architectures.
And JavaScript… Lastly!
With the proliferation of internet functions, it made sense to maneuver randomness to JavaScript. The language is already closely used, and including just a few random features to the combo doesn’t seem to be an enormous stretch.
And JavaScript lastly solved it! For the primary time, kinds might really behave with pure variation: random on creation, on refresh, and even on mutation.
It may be achieved in some ways, too: utilizing frameworks, CSS-in-JS libraries, or plain vanilla JavaScript. The strategies to include styling by way of this language are huge and effectively supported. There are some efficiency and complexity issues, however JavaScript will get the job achieved.
We lastly had true randomization in internet kinds… simply not in CSS itself.

A Internet Drawback, and a Internet Resolution
That final half is vital. Now we have randomization on the internet (JavaScript will get the job achieved) however one thing feels off. One thing doesn’t fairly really feel proper. At its core, that discomfort comes from two issues:
- We’re making use of an crucial resolution to a declarative drawback.
- We’re shifting structure selections from CSS to JavaScript.
An Crucial Resolution to a Declarative Drawback
We talked about earlier that CSS is a declarative language that focuses on the what, whereas JavaScript is an crucial language that focuses on the how.
By shifting randomization to JavaScript, we are attempting to reply a what query with a how reply. It really works, but it surely’s not preferrred.
Utilizing JavaScript, we lastly achieved type randomness in any respect ranges: when the web page is created, when it’s refreshed, and when components are added or modified (mutation). However in doing so, we’re breaking the mannequin.
CSS handles structure, and JavaScript handles logic. We solved a CSS limitation by shifting structure selections into JavaScript, making a mismatch that produces that delicate “this isn’t fairly proper” feeling — even when every part technically works.
The CSS Resolution
The answer to this mannequin mismatch is easy: transfer randomization to CSS. Remedy a structure drawback straight within the structure layer as an alternative of delegating it to a unique software or language. And this occurred with the introduction of two new random features as a part of the CSS Values and Models Module Stage 5:
random(): generates a random worth between a minimal and a most.random-item(): selects a random worth from a given listing.

This method additionally aligns with the Rule of Least Energy, which suggests selecting the least highly effective language appropriate for a given objective. In observe, this implies fixing an issue utilizing the least highly effective language able to expressing and fixing it.
Often, that language shall be higher suited to the duty. Its options shall be tailored to the extent at which they’re utilized, making them easier, extra environment friendly, and higher performing. Whereas a extra highly effective language can actually do the job, it usually introduces an pointless layer of complexity and abstraction.
On the net platform, now we have HTML for construction (least highly effective), CSS for styling and structure (extra highly effective), and JavaScript (considerably extra highly effective). By implementing randomization in CSS, we transfer the answer to the suitable layer whereas additionally following the Rule of Least Energy.
And that’s one of many causes the brand new random CSS options are such an enormous deal… and why they signify one thing a lot larger than simply one other function.
The Large Deal
CSS has at all times been deterministic by design, and native randomness breaks with that custom. It isn’t simply one other function, it represents a shift in how we take into consideration CSS as a language and in regards to the internet platform itself.
For the primary time, CSS can mannequin pure methods with variation straight: no hacks, no instruments, no outsourcing structure selections to different languages. Randomization takes an honored place within the styling layer, the place it at all times belonged.
This unlocks inventive prospects: generative layouts, natural patterns, playful micro-interactions, and design methods that really feel alive and distinctive. But it surely additionally restores architectural readability: every layer of the net as soon as once more does the job it was designed for.
With this modification, CSS strikes from being purely a styling language towards changing into a generative structure system. It’s now not only a passive actor in internet improvement; it turns into an energetic participant within the rendering course of, defining an area of doable outcomes that the browser resolves right into a concrete web page.
And that’s the true huge deal. Native randomness isn’t nearly making issues look completely different; it’s about making the platform extra coherent and expressive.
It’s additionally a reminder that CSS continues to be evolving, and that generally the options folks overlook can reshape how we take into consideration a language, and what we think about is feasible on the internet.









