Favorites

random() CSS Function

random()
CSS FunctionThis function is supported in some modern browsers, but not all.
Updated 21st November 2026

The random() function returns a random value between a minimum and a maximum, such as random(0deg, 360deg), optionally in steps.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

Description

The random() CSS function returns a random value between a minimum and a maximum. It can produce numbers, lengths, angles, times, percentages or any other numeric type, as long as the minimum and maximum are the same type. This lets you add natural variation to a design, such as scattering particles, varying the size or rotation of decorative shapes, or offsetting animation timings, without JavaScript.

The minimum and maximum are required; an optional third value is a step, so random(0px, 100px, 10px) only returns multiples of 10px. If the maximum is smaller than the minimum, the minimum is returned. An optional first argument, the random key, controls which random() calls share the same underlying random number. By default (auto) every call on every element is independent. element-scoped makes all calls with the same key on one element share a value, which is useful for keeping width and height equal; property-scoped gives every element the same value for that property; a custom --name shares one value everywhere it is used; and fixed followed by a number between 0 and 1 gives a repeatable result.

The random value is chosen once, when styles are computed, and does not change on every frame. It is a recent addition to CSS, so always provide a normal fallback value first.

/* Each star gets its own random position and size */
.star {
  position: absolute;
  top: random(0%, 100%);
  left: random(0%, 100%);
  width: random(--star element-scoped, 2px, 6px);
  height: random(--star element-scoped, 2px, 6px);   /* same as width */
}

/* Random rotation in 15deg steps */
.sticker {
  rotate: random(-45deg, 45deg, 15deg);
}

/* Staggered animation start */
.bubble {
  animation-delay: random(0s, 2s);
}

Syntax

random() = random( <random-key>?, <calc-sum>, <calc-sum>, <calc-sum>? )

<random-key> = auto | <random-cache-key> | fixed <number [0,1]>
<random-cache-key> = <dashed-ident> || element-scoped || [ property-scoped | property-index-scoped ]

Values

  • <random-key>Optional. Controls which random() calls share a random number: auto (default, every call independent), element-scoped (shared within one element), property-scoped (shared by all elements for the same property), property-index-scoped, a custom <dashed-ident> such as --size (shared wherever it is used, optionally combined with a scope keyword), or fixed followed by a number from 0 to 1 for a fixed, repeatable value.
  • minimumA <number>, <dimension> or <percentage>, or a calculation: the smallest value that can be returned.
  • maximumA value of the same type: the largest value that can be returned. If it is less than the minimum, the minimum is returned.
  • stepOptional. A value of the same type. The result is the minimum plus a whole number of steps, never going above the maximum.

Example

<div class="wrap">
<h1>CSS random() examples</h1>
<div class="card"><div class="sky">
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
</div>
<div class="label">top: random(0%, 95%); left: random(0%, 97%); width/height: random(--star element-scoped, 2px, 9px)</div></div>
<p class="note">Reload the page to get a new random sky. Browsers without random() show a single star in the middle.</p>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* random() examples */
.sky {
position: relative;
height: 220px;
border-radius: 12px;
overflow: hidden;
background: linear-gradient(#0f172a, #1e3a8a);
}

.star {
position: absolute;
border-radius: 50%;
background: #fde68a;
/* fallbacks for browsers without random() */
top: 50%;
left: 50%;
width: 4px;
height: 4px;
/* random values */
top: random(0%, 95%);
left: random(0%, 97%);
width: random(--star element-scoped, 2px, 9px);
height: random(--star element-scoped, 2px, 9px);
opacity: random(0.4, 1);
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS random() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported in some modern browsers, but not all.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.