Conversation
|
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:
External URLs (3)URL:
URL:
(comment last updated: 2026-03-10 11:38:14) |
Josh-Cena
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
estelle
left a comment
There was a problem hiding this comment.
The rest of the feedback.
I created a few codepens to help me understand. They're at
https://codepen.io/estelle/pen/NPrQxzG
https://codepen.io/estelle/pen/MYeNKXp
https://codepen.io/estelle/pen/OPXKWao
https://codepen.io/estelle/pen/PwGYaor
|
Please note that in some rules, |
estelle
left a comment
There was a problem hiding this comment.
more feedback. Also see https://github.com/jdatapple/content/pulls
| - {{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. |
There was a problem hiding this comment.
| - : 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: |
There was a problem hiding this comment.
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:
| 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. |
There was a problem hiding this comment.
| 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. |
There was a problem hiding this comment.
| 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>`. |
There was a problem hiding this comment.
| 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. |
There was a problem hiding this comment.
| 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. |
There was a problem hiding this comment.
| 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. |
There was a problem hiding this comment.
| 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`. |
There was a problem hiding this comment.
| 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 | ||
|
|
There was a problem hiding this comment.
| - {{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
| - {{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. |
There was a problem hiding this comment.
crossorigin/referrerpolicy are supported by safari 26.2
There was a problem hiding this comment.
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)
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
2fbdf2f to
926ecaf
Compare
This comment was marked as outdated.
This comment was marked as outdated.
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
This comment was marked as outdated.
This comment was marked as outdated.
|
Mike rebased, and I pressed "update branch", so your local copy and the remote branch are no longer synced. Before working locally:
|
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