Skip to content

CSS random() reference doc#43161

Open
jdatapple wants to merge 3 commits intomdn:mainfrom
jdatapple:draft/css-random
Open

CSS random() reference doc#43161
jdatapple wants to merge 3 commits intomdn:mainfrom
jdatapple:draft/css-random

Conversation

@jdatapple
Copy link
Contributor

@jdatapple jdatapple commented Feb 19, 2026

Description

Adds reference documentation for the CSS random() function.

Motivation

This new CSS function has shipped in Safari 26.2.

Additional details

See Safari 26.2 Release Notes

@jdatapple jdatapple requested a review from a team as a code owner February 19, 2026 00:54
@jdatapple jdatapple requested review from hamishwillee and removed request for a team February 19, 2026 00:54
@github-actions github-actions bot added Content:CSS Cascading Style Sheets docs size/m [PR only] 51-500 LoC changed labels Feb 19, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 19, 2026

Preview URLs (3 pages)

Flaws (7)

Note! 2 documents with no flaws that don't need to be listed. 🎉

Found an unexpected or unresolvable flaw? Please report it here.

URL: /en-US/docs/Web/CSS/Reference/Values/random
Title: random()
Flaw count: 7

  • broken_links:
    • Link /en-US/docs/Web/CSS/dashed-ident is a redirect
    • Link /en-US/docs/Web/CSS/number is a redirect
    • Link /en-US/docs/Web/CSS/number is a redirect
    • Link /en-US/docs/Web/CSS/dimension is a redirect
    • Link /en-US/docs/Web/CSS/percentage is a redirect
  • macros:
    • Macro cssxref produces link /en-US/docs/Web/CSS/hsl which doesn't resolve
    • Macro domxref produces link /en-US/docs/Web/API/Math/random which doesn't resolve
External URLs (3)

URL: /en-US/docs/Glossary/RNG
Title: Random Number Generator


URL: /en-US/docs/Web/CSS/Reference/Values/random
Title: random()

(comment last updated: 2026-03-10 11:38:14)

Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by: I don't see it very clearly mentioned if the random() function ever gets reevaluated, and if so, when. IIUC, this is the first non-idempotent function in CSS, right?

@estelle estelle self-requested a review February 19, 2026 18:03
Copy link
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the first half. i'll finish tomorrow. I made a lot of suggestions in hopes that it is helpful guidance for this and future PRs, so there is more feedback than i likely would normally offer.

@hamishwillee hamishwillee removed their request for review February 19, 2026 22:52
Copy link
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdatapple jdatapple requested a review from estelle February 25, 2026 18:45
@yisibl
Copy link

yisibl commented Feb 28, 2026

Please note that in some rules, random() is disabled, and I would like the documentation to reflect this.
w3c/csswg-drafts#10982

@jdatapple jdatapple changed the title CSS random() draft CSS random() reference doc Mar 3, 2026
Copy link
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdatapple jdatapple requested a review from estelle March 4, 2026 17:37
Copy link
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a few tweaks and we'll be good to go.

(Note: i committed some of the lintbot's suggestions so i could more easily read the page)

- {{cssxref("dashed-ident")}}
- : A custom key for sharing (e.g., `--my-random-key`) the same random base value across properties of an element.
- `element-shared`
- : A random base value is shared across all elements for a property using `random()` with this key term, while other properties on the same element using `random()` get a unique random base value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- : A random base value is shared across all elements for a property using `random()` with this key term, while other properties on the same element using `random()` get a unique random base value.
- : A random base value is shared across all elements for the same property using this key term. This base value is independent from the `random()` functions included in the values of other properties on that same element, unless the random functions also include the same custom key.


- `<random-value-sharing>` {{optional_inline}}
- : Controls which random functions in the document will share a random base value and which will get distinct values.
This can be either:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can also be a combo, so "either" isn't right, unles we add <dashed-ident> element-shared as a option in the DL, so maybe:

Suggested change
This can be either:
This can be one of the following values, or both a custom key and the keyterm `element-shared`, separated by a space:

Returns a random `<number>`, `<dimension>`, or `<percentage>` between the minimum and maximum range values, inclusive, in the same type as the `<calc-sum>` parameters.

## Description
The `random(SEED, MIN, MAX, STEP)` function specifies a minimum value and a maximum value, and optional step increments starting at the minimum value. The function generates a random result within the range specified. The seed, an [optional `<random-value-sharing>`](#random-value-sharing) parameter, enables sharing or varying random base values across different properties and elements.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The `random(SEED, MIN, MAX, STEP)` function specifies a minimum value and a maximum value, and optional step increments starting at the minimum value. The function generates a random result within the range specified. The seed, an [optional `<random-value-sharing>`](#random-value-sharing) parameter, enables sharing or varying random base values across different properties and elements.
The `random(SEED, MIN, MAX, STEP)` function specifies the minimum and maximum values and optional step increments, starting at the minimum value. The function generates a random result within the range specified. The seed, an [optional `<random-value-sharing>`](#random-value-sharing) parameter, enables sharing or varying random base values across different properties and elements.

## Description
The `random(SEED, MIN, MAX, STEP)` function specifies a minimum value and a maximum value, and optional step increments starting at the minimum value. The function generates a random result within the range specified. The seed, an [optional `<random-value-sharing>`](#random-value-sharing) parameter, enables sharing or varying random base values across different properties and elements.

The minimum, maximum and step values specified must be of the same {{cssxref("number")}}, {{cssxref("dimension")}}, or {{cssxref("percentage")}} data type, for the function to be valid. While the units in the two to three `<calc-sum>` parameters don't need to be the same, they do need to be of the same data type, such as {{cssxref("length")}}, {{cssxref("angle")}}, {{cssxref("time")}}, or {{cssxref("frequency")}} to be valid.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The minimum, maximum and step values specified must be of the same {{cssxref("number")}}, {{cssxref("dimension")}}, or {{cssxref("percentage")}} data type, for the function to be valid. While the units in the two to three `<calc-sum>` parameters don't need to be the same, they do need to be of the same data type, such as {{cssxref("length")}}, {{cssxref("angle")}}, {{cssxref("time")}}, or {{cssxref("frequency")}} to be valid.
The minimum, maximum and step values specified must be of the same data type for the function to be valid. While the units in the two to three `<calc-sum>` parameters don't need to be the same, they do need to be of the same data type, such as {{cssxref("number")}}, {{cssxref("percentage")}}, {{cssxref("length")}}, {{cssxref("angle")}}, {{cssxref("time")}}, or {{cssxref("frequency")}}, to be valid.


The random base value works like a [seed for randomness](/en-US/docs/Glossary/RNG). It's a starting number that is used to generate the final random result. When two `random()` functions share the same base value, their results vary together in a predictable pattern. When they have different base values, their results are completely independent of each other.

The optional first`<random-value-sharing>` parameter controls how the random base value is shared. Sharing enables reusing the same randomly generated value, a necessity for some design effects. The value can be set to use `auto`, an `element-shared` keyword, a custom {{cssxref("dashed-ident")}} , or `fixed <number>`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The optional first`<random-value-sharing>` parameter controls how the random base value is shared. Sharing enables reusing the same randomly generated value, a necessity for some design effects. The value can be set to use `auto`, an `element-shared` keyword, a custom {{cssxref("dashed-ident")}} , or `fixed <number>`.
The optional first`<random-value-sharing>` parameter controls how the random base value is shared. Sharing enables reusing the same randomly generated value, a necessity for some design effects. The value can be set to use `auto`, an `element-shared` keyword, a custom {{cssxref("dashed-ident")}} , or `fixed <number>`. Including a custom {{cssxref("dashed-ident")}} with the `element-shared` keyword, space-separated, is also valid.


## Accessibility

Because `random()` can generate an unknown value within a range, you don't have full control over what you get and that can lead to inaccessible results. For example, if you use `random()` to generate color for text, you could end up with a value that has low contrast. So it's important to be mindful of the context in which `random()` is being used so that your results are still accessible.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Because `random()` can generate an unknown value within a range, you don't have full control over what you get and that can lead to inaccessible results. For example, if you use `random()` to generate color for text, you could end up with a value that has low contrast. So it's important to be mindful of the context in which `random()` is being used so that your results are still accessible.
Because `random()` can generate an unknown value within a range, you don't have full control over what you get. This can lead to inaccessible results. For example, if you use `random()` to generate text color, you could end up with a value that has low contrast with its background. It is important to be mindful of the context in which `random()` is used, ensuring your results are always accessible.


### Basic Usage

In this example, we'll generate random colors for circular badge, to demonstrate the basic usage of the `random()` function.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In this example, we'll generate random colors for circular badge, to demonstrate the basic usage of the `random()` function.
In this example, we'll generate random colors for some circular badges to demonstrate the basic usage of the `random()` function.


#### HTML

We include five badges, one using the `desaturated` class, two with the `unique` class.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
We include five badges, one using the `desaturated` class, two with the `unique` class.
We include five badges, one using the `desaturated` class and two using the `unique` class.


#### CSS

We render the five badges as circles. We use the `random()` function within an {{cssxref("hsl()")}} color function to define the {{cssxref("angle")}} of the {{cssxref("hue")}}. We set `element-shared` to share the random base value between the default `badge` and the `desaturated` one, so it has the same {{cssxref("hue")}}, but with reduced saturation. We then override set the `unique` badges to have a truly random `hue` by letting the base value sharing parameter default to `auto`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
We render the five badges as circles. We use the `random()` function within an {{cssxref("hsl()")}} color function to define the {{cssxref("angle")}} of the {{cssxref("hue")}}. We set `element-shared` to share the random base value between the default `badge` and the `desaturated` one, so it has the same {{cssxref("hue")}}, but with reduced saturation. We then override set the `unique` badges to have a truly random `hue` by letting the base value sharing parameter default to `auto`.
We render the five badges as circles. We use the `random()` function within an {{cssxref("hsl()")}} color function to define the {{cssxref("angle")}} of the {{cssxref("hue")}}. We set `element-shared` to share the random base value between the default `badge` and the `desaturated` one, so it is a less saturated version of the same {{cssxref("hue")}}. We then override the `unique` badges to have a truly random `hue` by letting the base value sharing parameter default to `auto`.

{{Compat}}

## See also

Copy link
Member

@estelle estelle Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- {{cssxref("calc()")}}
- [CSS units and values](/en-US/docs/Web/CSS/Guides/Values_and_units) module
- {{domxref("Math.random()")}}

order in css section is CSS reference first, then CSS module, then non-CSS section MDN references, then external links

@github-actions github-actions bot added the Content:Glossary Glossary entries label Mar 5, 2026
- {{cssxref("url_function", "url()")}}

The CSS values and units module also introduces the `calc-mix()`, `crossorigin()`, `first-valid()`, `integrity()`, `random()`, `random-item()`, `referrerpolicy()`, `src()`, `type()`, and `toggle()` functions. Currently, no browsers support these features.
The CSS values and units module also introduces the `calc-mix()`, `crossorigin()`, `first-valid()`, `integrity()`, `random-item()`, `referrerpolicy()`, `src()`, `type()`, and `toggle()` functions. Currently, no browsers support these features.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crossorigin/referrerpolicy are supported by safari 26.2

Copy link
Member

@estelle estelle Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure of the syntax, so tested two ways in https://codepen.io/estelle/pen/MYjjRPJ. Both lines with referrer- policy() are listed as unsupported.

If supported, we need to get this into BCD and then document it.

(it's listed on https://webkit.org/blog/17640/webkit-features-for-safari-26-2, but without examples, and there is no mention in the CSS section under bug fixes and more)

@jdatapple jdatapple requested review from a team and mdn-bot as code owners March 6, 2026 22:05
@pepelsbey

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the merge conflicts 🚧 [PR only] label Mar 7, 2026
@github-actions github-actions bot added merge conflicts 🚧 [PR only] size/m [PR only] 51-500 LoC changed and removed merge conflicts 🚧 [PR only] Content:HTML Hypertext Markup Language docs Content:WebExt WebExtensions docs Content:WebAPI Web API docs Content:JS JavaScript docs Content:Learn Learning area docs Content:Other Any docs not covered by another "Content:" label Content:HTTP HTTP docs Content:Accessibility Accessibility docs Content:wasm WebAssembly docs Content:WebDriver WebDriver docs Content:Games Games docs Content:Security Security docs Content:Firefox Content in the Mozilla/Firefox subtree Content:Meta Content in the meta docs system [PR only] Infrastructure and configuration for the project size/xl [PR only] >1000 LoC changed labels Mar 7, 2026
@sideshowbarker

This comment was marked as outdated.

Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
@sideshowbarker

This comment was marked as outdated.

@estelle
Copy link
Member

estelle commented Mar 10, 2026

Mike rebased, and I pressed "update branch", so your local copy and the remote branch are no longer synced. Before working locally:

  1. commit any suggestions you want. The fasted way to do that is to go to https://github.com/mdn/content/pull/43161/files, and under any suggestion you want to add select "add to batch". Once done, at the top of the page select the "commit suggestions (n)" button. then the "commit changes" in the UI popup. You can also make changes in the github GUI and then commit them directly to the remote branch in this way.

  2. Update the remote branch by hitting the "Update branch" button at the bottom of CSS random() reference doc #43161. This will ensure that all files on the branch are up to date with main.

  3. Update your branch locally to match the remote branch you just updated. At the command line, enter git pull origin draft/css-random

  4. Edit away locally!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:CSS Cascading Style Sheets docs Content:Glossary Glossary entries size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants