random-item() CSS function

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Want more browser support for this feature? Tell us why.

The random-item() CSS function picks one value at random from a list of values, and uses it in a property value.

Try it

<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
body {
  display: flex;
  gap: 1rem;
}

.box {
  width: 60px;
  height: 60px;
  background-color: random-item(
    auto,
    tomato,
    gold,
    mediumseagreen,
    royalblue,
    orchid
  );
}

@supports not (color: random-item(auto, red, blue)) {
  body::before {
    content: "Your browser doesn't support the random-item() function.";
  }
}

Syntax

css
/* Basic usage */
random-item(auto, red, green, blue)
random-item(auto, 1px, 5px, 10px)
random-item(auto, 1px solid black, 3px dashed red)

/* With a custom <random-key> */
random-item(--my-key, red, green, blue)
random-item(fixed 0.5, red, green, blue)

/* With scoping */
random-item(element-scoped, red, green, blue)
random-item(property-scoped, red, green, blue)
random-item(property-index-scoped, red, green, blue)
random-item(--my-key element-scoped, red, green, blue)

Parameters

The random-item(<random-key>, <items>) function accepts the following parameters:

<random-key>

Controls which functions in the document should share a random base value. Specified as auto, a <dashed-ident>, one of the scope keywords element-scoped, property-scoped, or property-index-scoped, optionally combined with a <dashed-ident>, or fixed <number>.

<items>

A comma-separated list of one or more values to choose from. Each item should be a valid value for the place where the function is used.

Return value

One item, randomly chosen from the list of items.

Description

The random-item() function lets you add variety to a page without JavaScript, allowing you to provide a list of valid values from which the browser selects one at random. For example, by providing a list of color values, you can apply a randomly chosen accent color to each card in a group of cards. Similarly, by providing a list of animation names, each card can have a randomly selected animation applied.

The first parameter is the <random-key>, which defines which random functions in the document share the same random base value. The <random-key> is required and must be the first parameter. This differs from random(), where the <random-key> is optional (but still the first parameter if present). This is because the items can be any value, including values that look like a <random-key>, such as a <dashed-ident>. Without a required <random-key>, the browser couldn't tell whether the first argument is a <random-key> or an item.

The second parameter is a comma-separated list of items (values) to choose from. Each item should be a valid value of the correct data type for the situation in which the function is used. Depending on the situation, a valid value may be a var() function, a space-separated list of values, or even empty. The function returns one item from the comma-separated list, chosen randomly. Each item has the same chance of being chosen.

Substitution

The random-item() function works like var(): it is substituted with the chosen item before the property value is checked. This has the following effects:

  • A declaration that uses random-item() is valid as long as the random-item() function itself is written correctly. The browser doesn't check whether the items are valid values for the property, or whether the first argument is a valid <random-key>.
  • The item is chosen when the element's computed value is determined. The chosen item, not the random-item() function, is inherited, so child elements that inherit the property get the same value as their parent.
  • If the chosen item isn't a valid value for the property, or the first argument isn't a valid <random-key>, the declaration is invalid at computed-value time. Unlike an invalid declaration, it isn't ignored in favor of other declarations: the property is set to its inherited value if it's an inherited property, or to its initial value otherwise.

Sharing random choices

The <random-key> determines which random functions share a random base value. When two random-item() functions share a random base value and have the same number of items, they always choose the item at the same position. This lets you make several random choices that match each other, as shown in the Matching random choices across properties example.

A random-item() function and a random() function with the same <random-key> also share a random base value.

Formal syntax

<random-item()> = 
random-item( <random-key> , [ <declaration-value>? ]# )

<random-key> =
auto |
<random-cache-key> |
fixed <number [0,1]>

<random-cache-key> =
<dashed-ident> ||
element-scoped ||
[ property-scoped | property-index-scoped | <random-ua-ident> ]

<random-ua-ident> =
<custom-ident>

Examples

Choosing a random color for each element

In this example, each card is given a random background color from a list of colors.

HTML

html
<ul>
  <li>Apples</li>
  <li>Bananas</li>
  <li>Cherries</li>
  <li>Dates</li>
  <li>Elderberries</li>
  <li>Figs</li>
</ul>

CSS

The auto keyword makes each <li> element choose its own color.

css
ul {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  padding: 0;
  list-style: none;
}

li {
  padding: 1rem;
  font-size: 1.25rem;
  background-color: random-item(auto, lightpink, khaki, lightgreen, lightblue);
}

Result

Each card has one of the four background colors. Reload the page to see a different combination.

Matching random choices across properties

This example shows how to use <random-key> across multiple random-item() functions to create themes with matching background and border colors, randomly applied to a series of cards.

HTML

html
<ul>
  <li>Apples</li>
  <li>Bananas</li>
  <li>Cherries</li>
  <li>Dates</li>
  <li>Elderberries</li>
  <li>Figs</li>
</ul>

CSS

Both random-item() functions use the same <random-key>, --theme element-scoped. The --theme name makes the two properties share a random base value, and element-scoped gives each element its own random base value. Because both random-item() functions have the same number of items, the background color and border color are always chosen from the same position in their lists.

css
ul {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  padding: 0;
  list-style: none;
}

li {
  padding: 1rem;
  font-size: 1.25rem;
  border: 4px solid;
  background-color: random-item(
    --theme element-scoped,
    lightpink,
    khaki,
    lightblue
  );
  border-color: random-item(
    --theme element-scoped,
    deeppink,
    goldenrod,
    royalblue
  );
}

Result

Each card has a random theme, but the border color always matches the background color: pink cards have a deep pink border, yellow cards have a goldenrod border, and blue cards have a royal blue border.

Specifications

Specification
CSS Values and Units Module Level 5
# funcdef-random-item

Browser compatibility

See also