<random-key> CSS type

The <random-key> CSS data type represents a seed value that controls the sharing of random base values, or seeds, within a document.

Associated functions

This data type is accepted as a parameter in the following CSS functions:

Syntax

css
/* Keyword values */
auto
element-scoped
property-scoped
property-index-scoped

/* fixed values */
--my-random-key
fixed 0.123

/* Scoped keywords with <dashed-ident> */
--random-key element-scoped
--some-random-key property-scoped
--another-random-key property-index-scoped

Values

This data type is specified as one of the following values:

auto

Specifies an independent random value should be generated. This is the default value, and is equivalent to specifying element-scoped property-index-scoped.

element-scoped

Specifies an element-specific identifier to be added to the random cache name.

property-scoped

Specifies a property-specific identifier to be added to the random cache name.

property-index-scoped

Specifies the property name and index of the containing function to be added to the random cache name.

<dashed-ident>

Specifies a custom name for the random cache key shared across all elements and properties using the same identifier. When combined with a *-scoped keyword, the sharing behavior is determined by the keyword.

fixed <number>

Specifies the seed's value, as a number between 0 and 1, inclusive of 0, exclusive of 1.

Description

The <random-key> data type provides a seed value that controls which random functions in a document share a seed value and which get distinct values.

Random base values work like seeds for randomness. They are starting numbers that are used to generate random results. When multiple functions share the same seed, their results vary together in a predictable pattern. When they have different base values, their results are completely independent of each other.

The <random-key> data type is used as a parameter in random functions to control how random base values are shared. It can be auto, a scope keyword (element-scoped, property-scoped, or property-index-scoped), a custom <dashed-ident>, fixed <number>, or a <dashed-ident> paired with a scope keyword.

The auto keyword

The auto value is the default, which causes random cache names and results to vary across every random() instance in a multi-component value, across different properties, and across different elements. This is equivalent to specifying element-scoped property-index-scoped.

When <random-key> is set to auto, or omitted (defaulting to auto), the user agent auto-generates a seed name, or generated value sharing identifier, following consistent rules based on property name and order. This can cause random base value sharing. Multiple random functions can end up with the same seed name and, therefore, the same random base value.

css
.a,
.b,
.c {
  width: random(auto, 10px, 200px);
  height: random(auto, 10px, 200px);
  border-color: random-item(auto, red, pink, coral, maroon, magenta);
  color: random-item(auto, red, pink, coral, maroon, magenta);
}

In this example, the elements .a, .b, and .c each get independent values for each property, and the properties are independent of each other.

The element-scoped keyword

The element-scoped keyword gives each element its own base value, per property. This parameter adds an element-specific identifier to the random cache name, so if applied over multiple properties (e.g., width and height), both properties use the same value, but each element (e.g., <div>) gets different random values.

css
.a,
.b,
.c {
  width: random(element-scoped, 10px, 200px);
  height: random(element-scoped, 10px, 200px);
  border-color: random-item(element-scoped, red, pink, coral, maroon, magenta);
  color: random-item(element-scoped, red, pink, coral, maroon, magenta);
}

In this example, each of the elements.a, .b, and .c gets the same value for width and height, but they are different for each element. The border will also be the same color as the text in each case, but each box's colors are different.

The property-scoped keyword

The property-scoped keyword adds the property name to the random cache name, so if applied over multiple properties (e.g., width and height), each property gets a different random value, but each element (e.g., <div>) uses the same random values.

css
.a,
.b,
.c {
  border: solid 10px;
  width: random(property-scoped, 10px, 200px);
  height: random(property-scoped, 10px, 200px);
  border-color: random-item(property-scoped, red, pink, coral, maroon, magenta);
  color: random-item(property-scoped, red, pink, coral, maroon, magenta);
}

In this example, the property-scoped keyword makes .a, .b, and .c identical rectangles, since every element gets the same width, height, border-color, and color values, but the dimensions and color values are independent of each other.

The property-index-scoped keyword

The property-index-scoped keyword adds the property name and the index of the random() function among all random functions used in the same property value to the random cache name, so multiple instances in the same declaration each get different random values. The property-index-scoped keyword works like property-scoped, but provides different seeds for each position within a shorthand property.

The property-index-scoped keyword is useful in shorthands, where each position needs its own shared value:

css
.a,
.b,
.c {
  border-width: random(property-index-scoped, 5px, 40px)
    random(property-index-scoped, 5px, 40px)
    random(property-index-scoped, 5px, 40px)
    random(property-index-scoped, 5px, 40px);
  border-style: random-item(
      property-index-scoped,
      dotted,
      dashed,
      solid,
      double
    )
    random-item(property-index-scoped, dotted, dashed, solid, double)
    random-item(property-index-scoped, dotted, dashed, solid, double)
    random-item(property-index-scoped, dotted, dashed, solid, double);
  border-color: random-item(
      property-index-scoped,
      red,
      pink,
      coral,
      maroon,
      magenta
    )
    random-item(property-index-scoped, red, pink, coral, maroon, magenta)
    random-item(property-index-scoped, red, pink, coral, maroon, magenta)
    random-item(property-index-scoped, red, pink, coral, maroon, magenta);
}

All three elements get the same top border, the same right border, and so on, but each of the four borders is independent of the others.

Custom identifiers

The seed can also be a <dashed-ident>. This specifies a custom name for the random cache key (e.g., --my-random-key). When used alone, the same random base value is shared across all elements and properties that use the same identifier. When combined with a *-scoped keyword, the sharing behavior is determined by the keyword.

Used alone, the <dashed-ident> shares its base value globally: any usage in a random function call anywhere in the document with the same identifier will get the same result. For example, if you use identical random() functions — using the same --custom-name with the same minimum and maximum values — on different properties, all the functions will return the same values. Similarly, if you use the same <dashed-ident> in a random-item() function with the same list of options, all functions will return the same item.

css
.a,
.b,
.c {
  border-style: solid;
  width: random(--custom-name, 50px, 200px);
  height: random(--custom-name, 50px, 200px);
  border-width: calc(random(--custom-name, 50px, 200px) / 10);
  padding: calc(random(--custom-name, 50px, 200px) / 10);
  border-color: random-item(--custom-name, red, pink, coral, maroon, magenta);
  color: random-item(--custom-name, red, pink, coral, maroon, magenta);
}

This makes .a, .b, and .c identical squares, since every width and height resolves to the same value, with the padding and border being 10% of that value, and the color will be the same as the border-color.

Formal syntax

<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>

Specifications

Specification
CSS Color Module Level 4
# typedef-color-random-key

See also