Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
Winter, 04 Dec 2026 - @39.13
Forum Activity: Four Stars Random | Recent Posts | Guild Recents
News: :wizard: You can make anything on the web! :wizard: Guild Events: Holiday Horror Watch-a-thon
Super News: ++ MelonJam 2026 ~ 1st Dec to 1st Jan! ++
+  MelonLand Forum
|-+  Da Web Revival
| |-+  ✁ ∙ Web Crafting
| | |-+  ♺ ∙ Snippets & Materials
| | | |-+  CSS's new random() function - what can I do with it?


« previous next »
Pages: [1] Print Embed
Author Topic: CSS's new random() function - what can I do with it?  (Read 511 times)
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
The Worm is creeping around Melonland profiles!Caught by RubbereonThe Worm is creeping around Melonland profiles!Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« on: Autumn, 24 Nov 2026 @434 » Toggle Font Embed

CSS recently gained a random() function, which opens up all kinds of opportunities. It's not supported in all browsers yet, but once Firefox 161 comes out (soon!), the latest versions of all the major desktop browsers will support it, which means that so long as you code with care you can probably start using it!

I created a page on the Everyone Site to play with the new functionality and see what it can do. Open that in a supported browser and you'll see some of the following, implemented entirely in HTML + CSS:

Random link hover colors


Code
a:hover {
  color: hsl(random(0, 360), 100%, 40%);
}

Each link gets a different random color on-hover. I'm generating the colors using the HSL function, with a random hue (shade) and a fixed saturation (colorfulness) and lightness (whiteness).

Random link colors with random link hover colors


Code
a {
  color: hsl(random(0, 360), 100%, 40%);

  &:hover {
    color: hsl(random(element-scoped, 0, 360), 100%, 40%);
  }
}

Here I've given each link a random color in the same way as before, and then given each link an on-hover random color. Note that I had to add element-scoped to the second one so that it doesn't generate the same color as the first!

This is necessary because the random() function caches the value it creates and by-default if you call it with the same parameters, you'll get the same result! You can bypass that by setting the scope of the cache in one of several ways: either

(a) specifically naming the scope (I'll demonstrate that later),
(b) specifying "element-scoped" (uses the element identifier, i.e. "&:hover" in my case, as the cache name), "property-scoped" (uses the property, i.e. "color:" in my case, as the cache name), or "property-index-scoped" (uses the property name AND the number of times that property has shown up in the selector as the cache name, e.g. "color: 1"), or
(c) both of the above

I imagine that in most cases "property-scoped" will be sufficient for almost every use case where you want to stop the RNG coming up with the same number several times in succession!


Contrasting random color pairs


Code
li {
  background-color: hsl(
    random(--li-random-colour element-scoped, 0, 360),
    75%, 40%
  );

  color: hsl(
    calc(
      random(--li-random-colour        element-scoped, 0, 360) +
      random(--li-random-colour-offset element-scoped, 170, 190)
    ),
    100%, 50%);
}

Here I wanted to generate a background-color for my <li>, and then a contrasting color for it. Here, being able to generate the same random number twice is super helpful!

First I use the same technique as before to generate a random color and call it the background. But I name the cache "--li-random-colour" (that's just a name I made up, but it has to begin with "--"; also - yes, I use British English in my variable names but I've used US English in this forum post!). I still use "element-scoped" so that it doesn't come up with the same background-color for every <li>.

Next, I generate the same color again, but this time I add another random number to the hue, "inverting" it around the colour wheel. 180 degrees would be a complete inversion, but I generate a random number between 170 and 190 which results in a slightly-off, near-perfect inversion. I also bump the lightness to improve contrast slightly (and not inconvenience e.g. folks with e.g. deuteranopia too badly, I hope!).

This results in two random colors that are nontheless moderately well-contrasted to one another. There are doubtless other (better!) ways to use this trick.

Random sort!


Code
ol {
  display: flex;
  flex-direction: column;
}

li {
  order: random(0, 9);
}

In this example, I took a list (<ol> containing <li>s) and shuffled it using CSS). First I needed to make it display: flex; to allow me to use order:. Then I set order: to a random number from 0 - 9. Easy! Note that in the event of a "tie" (two items roll the same number), they one that appears first in the HTML code will appear on top, so there'll be a slight bias. You can reduce the bias by increasing the range on the random number generator, or increase it by reducing the range.

Note that my list contains links, and that's probably a Bad Idea for accessibility. If you use a keyboard to navigate, the tab order will still be the order in which they appear in the HTML code, resulting in a lot of "jumping around". Probably best to use this one for non-interactive elements only!

(Another idea would be to position things by setting their e.g. top: and left: to specific values. You can constrain the RNG to "steps" by setting a step interval as the last parameter to random()... or you can do the same with calc, via multiplication.)

Random animation offsetting


Code
#staggered-animation-output span {
  animation: word-appear 0.7s ease-out calc(random(0, 4)  * 1s) both;
}

My final example is the one I imagine I'm most-likely to make most-use out of: delaying the start of an animation by a random amount in order to make it feel more-organic. My example animates words appearing on a page, but I've delayed each one by between 0 and 4 seconds before its animation runs.

The maths defines the "steps". E.g. calc(random(0, 4)  * 1s) means that everything starts on second 0, 1, 2, 3, or 4 exactly. But I could alternatively do e.g. calc(random(0, 40)  * 0.1s) (multiplying the largest number by 10, dividing the duration by 10): the total animation time would be the same on average, but words would appear in smaller groups (because there are more numbers to choose from).

Note that because of the caching we already talked about, the delay will be the same each time the animation runs. If you ran it twice, each "word" would appear in the same order and after the same duration (until you refreshed the page: if you want them to be the same through page refreshes you need to seed the random function!). There's no way around this in CSS: if you want a different random number each time the animation runs, you'll need a different technology (e.g. JS) or a different source of entropy (e.g. user actions resulting in CSS variable changes and those variables being used as seeds, perhaps).

I hope you've enjoyed hearing about my experiments with CSS's new random() function, and I'm very excited to hear what you come up with!

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif https://origami.danq.dev/88x31.gif https://skelly.monster/dem-bones-88x31.gif

Artifact Swap: Pink ham-ham rangerPolyamorousI met Dan Q on Melonland!shoeJoined 2025!cinnamoroll3 coins
Noah_S
Sr. Member ⚓︎
****
View Profile WWWArt


Noah. Earth. I make website. I.. I.. Noah. Earth.
⛺︎ My Room
iMood: Noah_S
RSS: RSS

Guild Memberships:
Artifacts:
Caught by RubbereonfunguyVisited on Melon's 10th Anniversary!I got robbed by Dan Q on Melonland!I met Dan Q on Melonland!A coin!
« Reply #1 on: Autumn, 24 Nov 2026 @685 » Toggle Font Embed

You using British English there was a big help when I was studying your code yesterday.

I made a fishing game relying on CSS random. It relies on that same naming it technique, I used the calc() function to generate random movements up and down for my fishes. Overall I found the random() function easy to use. I spent more time figuring out how to get the javascript to work so that when you click on a fish it disappears and you get a point. I will publish it on my website once the firefox update drops, I'll make a page explaining it and include a link to it because the timer and fishes start when it loads. It doesn't make sense to embed in an iframe like I usually do.

Fish size and speed are also varied with the random function. Varied size is done by giving them an aspect ratio, putting the background image of the fish to cover and randomizing the width. The speed is randomized in animation.

You play as the fox. His wish is to catch a fish as big as he is.

You can see the full code here. I may still adjust it, but I don't think I'll make major changes.

Code
<!DOCTYPE html>
<html>
<head>
<style> 
body {
background-image: url("https://art-noah-s.eu/images/20x30grass.png");
overflow: hidden;
cursor: url("https://art-noah-s.eu/images/fishing/foxpaw-cursor.png"), auto;
}

a {
color: lightblue;
}

main {
background-image: url("https://art-noah-s.eu/images/8x8wave.png");
background-repeat: xy;
position: relative;
margin-top: 15vh;
height: 50vh;
overflow: hidden;
}

footer {
background-image: url("https://art-noah-s.eu/images/20x30grass.png");
background-repeat: xy;
margin-bottom: 0px;
height: 50vh;
overflow: hidden;
}

#done {
display: none;
position: absolute;
padding: 5px;
z-index: 200;
margin-left: -8px;
width: 100%;
height: 100%;
overflow: auto;
background-color: aquamarine;
}

#boxcontent {
background-color: powderblue;
color: navy;
margin: auto;
top: calc(50% - 75px);
left: calc(50% - 150px);
padding: 5px;
border: 1px solid violet;
width: 300px;
height: 300px;
}

.donebutton {
background-color: midnightblue;
color: azure;
border: none;
padding: 3px;
}

.donebutton:hover {
background-color: indigo;
padding: 3px;
}

.gamefish1 {
aspect-ratio: 143/69;
width: random(element-scoped, 5vw, 10vw);
background-image: url("https://art-noah-s.eu/images/5fish2.png");
background-size: cover;
z-index: 100;
margin-left: -10vw;
position: absolute;
--t0: random(element-scoped, 0vh, 50vh);
--t1: random(element-scoped, 0vh, 50vh);
--t2: random(element-scoped, 0vh, 50vh);
--t3: random(element-scoped, 0vh, 50vh);
--t4: random(element-scoped, 0vh, 50vh);
--t5: random(element-scoped, 0vh, 50vh);
--t6: random(element-scoped, 0vh, 50vh);
animation: mymove0 random(element-scoped, 5s, 15s) linear 1;
}

.gamefish2 {
aspect-ratio: 158/64;
width: random(element-scoped, 6vw, 11vw);
background-image: url("https://art-noah-s.eu/images/5fish1.png");
background-size: cover;
z-index: 100;
margin-left: -11vw;
position: absolute;
--t10: random(element-scoped, 0vh, 50vh);
--t11: random(element-scoped, 0vh, 50vh);
--t12: random(element-scoped, 0vh, 50vh);
--t13: random(element-scoped, 0vh, 50vh);
--t14: random(element-scoped, 0vh, 50vh);
--t15: random(element-scoped, 0vh, 50vh);
--t16: random(element-scoped, 0vh, 50vh);
animation: mymove1 random(element-scoped, 5s, 15s) linear 1;
}

@keyframes mymove0 {
0%   {top: calc(var(--t1) + var(--t0) / random(element-scoped, 2, 5)); left: -11vw; opacity: display:none;}
10%  {top: calc(var(--t1) - var(--t0) / random(element-scoped, 2.3, 4.5)); left: 10%; opacity: display: inline-block;}
20%  {top: calc(var(--t1) + var(--t2) / random(element-scoped, 2.2, 4)); left: 20%; opacity: display: inline-block;}
40%  {top: calc(var(--t2) - var(--t3) / random(element-scoped, 2.4, 5)); left: 40%; opacity: display: inline-block;}
60%  {top: calc(var(--t3) + var(--t4) / random(element-scoped, 2.5, 4.5)); left: 60%; opacity: display: inline-block;}
80%  {top: calc(var(--t4) - var(--t5) / random(element-scoped, 1.2, 2.5)); left: 80%; opacity: display: inline-block;}
100% {top: calc(var(--t5) + var(--t6) / random(element-scoped, 2, 3.5)); left: calc(100% + 14vw); opacity: display: inline-block;}
}

@keyframes mymove1 {
0%   {top: calc(var(--t11) + var(--t10) / random(element-scoped, 2, 5)); left: calc(100% + 15vw); display:inline-block;}
10%  {top: calc(var(--t11) - var(--t10) / random(element-scoped, 2.3, 4.5)); left: 90%; opacity: display: inline-block;}
20%  {top: calc(var(--t11) + var(--t12) / random(element-scoped, 2.2, 4)); left: 80%; opacity: display: inline-block;}
40%  {top: calc(var(--t12) - var(--t13) / random(element-scoped, 2.4, 5)); left: 60%; opacity: display: inline-block;}
60%  {top: calc(var(--t13) + var(--t14) / random(element-scoped, 2.5, 4.5)); left: 40%; display: inline-block;}
80%  {top: calc(var(--t14) - var(--t15) / random(element-scoped, 1.2, 2.5)); left: 20%; display: inline-block;}
100% {top: calc(var(--t15) + var(--t16) / random(element-scoped, 2, 3.5)); left: -11vw; display: none;}
}

.scoreboard {
width: fit-content;
padding-left: 5px;
padding-right: 5px;
height: 10vh;
background-image: url("https://art-noah-s.eu/images/10x6wood.png");
color: lightblue;
text-align: center;
position: relative;
display: inline-block;
top: 5vh; 
left: calc(50% - 5vw); 
}

.homelink {
width: fit-content;
padding-left: 5px;
padding-right: 5px;
height: 10vh;
background-image: url("https://art-noah-s.eu/images/10x6wood.png");
color: lightblue;
text-align: center;
position: relative;
display: inline-block;
top: 5vh; 
left: calc(50% - 5vw); 
}

.fishtime {
width: fit-content;
padding-left: 5px;
padding-right: 5px;
height: 10vh;
background-image: url("https://art-noah-s.eu/images/10x6wood.png");
color: lightblue;
text-align: center;
position: relative;
display: inline-block;
top: 5vh; 
left: calc(50% - 5vw); 
}
</style>
</head>
<body>

<div id='done'><div id='boxcontent'><center>Congratulations!<br>You got <span id="score2">0</span> points!<br><br><button class="donebutton" id='newgame' onclick='location.reload()'>Play again</button><br> <span class="homelink"><a href="https://art-noah-s.eu">Go home</a></span></center></div></div>

<main id="fishriver">
</main>
<footer>
<span class="scoreboard" id="score">0</span><span class="scoreboard">points</span><span class="homelink"><a href="https://art-noah-s.eu">Go home</a></span><br><span class="fishtime" id="timer">0</span><span class="fishtime">/ 115 seconds</span>
</footer>
<script>
let x = 0;
let t = 0;
const fishes1 = document.querySelectorAll(".gamefish1");
const fishes2 = document.querySelectorAll(".gamefish2");

function fishtimer() {
let a = t++;
document.getElementById("timer").innerHTML = t;

if (t == 0 || t == 1 || t == 2 || t == 3 || t == 4 || t == 5 || t == 6 || t == 7 || t == 8 || t == 9 || t == 10 || t == 11 || t == 12 || t == 13 || t == 14 || t == 15 || t == 16 || t == 17 || t == 18 || t == 19 || t == 20 || t == 21 || t == 22 || t == 23 || t == 24 || t == 25 || t == 26 || t == 27 || t == 28 || t == 29 || t == 30 || t == 31 || t == 32 || t == 33 || t == 34 || t == 35 || t == 36 || t == 37 || t == 38 || t == 39 || t == 40 || t == 41 || t == 42 || t == 43 || t == 44 || t == 45 || t == 46 || t == 47 || t == 48 || t == 49 || t == 50) {
create1();
create2();
}

if (t == 51 || t == 52 || t == 53 || t == 54 || t == 55 || t == 56 || t == 57 || t == 58 || t == 59 || t == 60 || t == 61 || t == 62 || t == 63 || t == 64 || t == 65 || t == 66 || t == 67 || t == 68 || t == 69 || t == 70 || t == 71 || t == 72 || t == 73 || t == 74 || t == 75 || t == 76 || t == 77 || t == 78 || t == 79 || t == 80 || t == 81 || t == 82 || t == 83 || t == 84 || t == 85 || t == 86 || t == 87 || t == 88 || t == 89 || t == 90 || t == 91 || t == 92 || t == 93 || t == 94 || t == 95 || t == 96 || t == 97 || t == 98 || t == 99 || t == 100) {
create1();
create2();
create1();
create2();
}

if (t == 115) {
let box = document.getElementById("done");
box.style.display = "block";
}
}
setInterval(fishtimer, 1000);

function create1() {
const fish1 = document.createElement("span");
document.getElementById("fishriver").appendChild(fish1);
fish1.classList.add("gamefish1");
fish1.addEventListener("click", click1);
}

function click1() {
this.remove();
let y = x++;
document.getElementById("score").innerHTML = x;
document.getElementById("score2").innerHTML = x;
}

function create2() {
const fish2 = document.createElement("span");
document.getElementById("fishriver").appendChild(fish2);
fish2.classList.add("gamefish2");
fish2.addEventListener("click", click2);
}

function click2() {
this.remove();
let y = x++;
document.getElementById("score").innerHTML = x;
document.getElementById("score2").innerHTML = x;
}
</script>
</body>
</html>

Logged

perfectionist_better.gifweb_designer.gifspeed_of_time.gifbutton290.pngmy_dream_wedding.gifClippy beats AI

Artifact Swap: Clippy would never make a subscription programClippy just wanted to helpClippy would never track youClippy didn't need data centersClippy would never sell your dataLarge watercraftGrowing ballwhen life gives you crab make mailMelonland has encountered a bugMessage Buddylaptop
Pages: [1] Print Embed 
« previous next »
 

Melonking.Net © Always and ever was! SMF © 2021 | Privacy Notice | Send Feedback | Supporters ♥ Guide | Rules | Whos a Mod? | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi
@000 Melon
Land
Forum Art Hub Chat Webring Explore Random Recent Tenement Arcade Want to Login or Join ? Web Craft Guide Graphic Catalogue Wiki Help I'm Lost! Link Land Newsletters Image Stream Minecraft: Online
Zap!
Passport Sites tamaNOTchi Asatte Add your site?
Aero Archive Onio Cafe 32 Bit Cafe Status Cafe