@@ -1461,7 +1461,7 @@ Toggling Between Values: the ''toggle()'' notation</h3>
14611461 not a particular serialization [[!CSS21]] ,
14621462 so comparison between computed values should always be unambiguous and have the expected result.
14631463 For example,
1464- a Level 2 <l spec=css22 > 'background-position' </l> computed value
1464+ a Level 2 <l spec=css2 > 'background-position' </l> computed value
14651465 is just two offsets, each represented as an absolute length or a percentage,
14661466 so the declarations ''background-position: top center'' and ''background-position: 50% 0%''
14671467 produce identical computed values.
@@ -1993,101 +1993,23 @@ Generating a Random Numeric Value: the ''random()'' function</h3>
19931993 <pre class=prod>
19941994 <random()> = random( <<random-value-sharing>> ? , <<calc-sum>> , <<calc-sum>> , <<calc-sum>> ? )
19951995
1996- <dfn><<random-value-sharing>></dfn> = <dfn value for="random()|<random-value-sharing>">auto</dfn> | [ <<dashed-ident>> || <dfn value for="random()|<random-value-sharing>">match-element</dfn> ] | <<number [0,1]>>
1996+ <dfn><<random-value-sharing>></dfn> = [ [ auto | <<dashed-ident>> ] || match-element ]
1997+ | <<number [0,1]>>
19971998 </pre>
19981999
19992000 Its arguments are:
20002001
20012002 <dl>
2002- : <css> << random-value-sharing>> </css >
2003+ : << random-value-sharing>>
20032004 :: The optional <<random-value-sharing>>
2004- controls whether a given ''random()'' function
2005- resolves similarly or differently to other [=random functions=] on the page.
2006- See [[#random-caching]] for details.
2007-
2008- If <<random-value-sharing>> is omitted or specified as ''random()/auto'' ,
2009- ''random()'' functions on different properties,
2010- and multiple ''random()'' functions within a single property,
2011- will resolve to different random values,
2012- and will have different values on every element
2013- styled with that property.
2014-
2015- Providing <<random-value-sharing>> makes it "less random":
2016-
2017- * two ''random()'' functions on an element with the same <<dashed-ident>>
2018- will use the same random value.
2019- (They'll still be different between elements, tho.)
2020- * two elements using the same style containing ''random()'' with ''match-element''
2021- will use the same random value on both of them.
2022- (They'll still be different between properties, tho.)
2023- * with both <<dashed-ident>> and ''match-element'' together,
2024- multiple ''random()'' functions will use the same random value
2025- across different properties
2026- and on all elements using the same style.
2005+ controls which [=random functions=] in the document
2006+ will share a [=random base value=]
2007+ and which will get distinct values.
2008+ If <<random-value-sharing>> is omitted,
2009+ it behaves as ''random()/auto'' .
20272010
2028- <div class=example>
2029- * <b> Maximum random</b> :
2030- both properties get a different value,
2031- and it's different per element too,
2032- so you get lots of random rectangles.
2033-
2034- <pre highlight=css>
2035- .random-rect {
2036- width: random(100px, 200px);
2037- height: random(100px, 200px);
2038- }
2039- </pre>
2040-
2041- * <b> Shared by name within an element</b> :
2042- both properties get the same value,
2043- but it's still different per element,
2044- so you get lots of random squares.
2045-
2046- <pre highlight=css>
2047- .random-square {
2048- width: random(--foo, 100px, 200px);
2049- height: random(--foo, 100px, 200px);
2050- }
2051- </pre>
2052-
2053- * <b> Shared between elements within a property</b> :
2054- both properties get different values,
2055- but they're shared by every element,
2056- so you get lots of identical rectangles of a single random size.
2057-
2058- <pre highlight=css>
2059- .shared-random-rect {
2060- width: random(match-element, 100px, 200px);
2061- height: random(match-element, 100px, 200px);
2062- }
2063- </pre>
2064-
2065- * <b> Shared globally</b> :
2066- both properties get the same value,
2067- and every element shares that value,
2068- so you get lots of identical squares of a single random size.
2069-
2070- <pre highlight=css>
2071- .shared-random-squares {
2072- width: random(--foo match-element, 100px, 200px);
2073- height: random(--foo match-element, 100px, 200px);
2074- }
2075- </pre>
2076- </div>
2077-
2078- Instead of a caching key,
2079- a <<number>> between 0 and 1 can be specified;
2080- this provides the [=random base value=] to be used
2081- rather than generating one for you.
2082-
2083- This <em> can</em> be used by authors to aid in testing their page
2084- (using a stable value rather than one that changes each refresh),
2085- but it mostly exists to help [=user agents=]
2086- ensure that ''random()'' can be fully resolved at [=computed value=] time in all cases,
2087- so the function inherits reasonably.
2088- If the function's min/max/step values can't resolve in time,
2089- the ''random()'' function determines its [=random base value=]
2090- and computes to a form using this instead.
2011+ See below for details on the <<random-value-sharing>> options,
2012+ and see [[#random-caching]] for a precise description of their behavior.
20912013
20922014 : <css> <<calc-sum>> , <<calc-sum>> </css>
20932015 :: The two required [=calculations=]
@@ -2150,6 +2072,186 @@ Generating a Random Numeric Value: the ''random()'' function</h3>
21502072 while ''100px'' and ''200px'' will only occur 1/4 of the time each.
21512073 </dl>
21522074
2075+ The <<random-value-sharing>> value controls sharing along two axises:
2076+
2077+ * between [=random functions=] used for different things
2078+ in the style for a single element,
2079+ via the ''random()/auto'' or ''random()/<dashed-ident>'' values
2080+ * between [=random functions=] used for the same thing
2081+ in the styles across several elements,
2082+ via the ''random()/match-element'' value (or its absence)
2083+
2084+ <dl dfn-type=value dfn-for="random()|<random-value-sharing> ">
2085+ : <dfn>auto</dfn>
2086+ :: Each [=random function=] in an element's styles
2087+ will use a distinct [=random base value=] .
2088+ (But see <a href="#auto-naming-details">the note below</a> for a small wrinkle about this.)
2089+
2090+ If neither ''random()/auto'' nor ''random()/<dashed-ident>'' are specified,
2091+ it behaves as ''random()/auto'' .
2092+
2093+ : <dfn><<dashed-ident>></dfn>
2094+ :: Each [=random function=] in an element's styles
2095+ with the same <<dashed-ident>>
2096+ will share the same [=random base value=] ;
2097+ ones with different <<dashed-ident>> s
2098+ will use distinct [=random base values=] .
2099+
2100+ : <dfn>match-element</dfn>
2101+ :: If ''random()/match-element'' is omitted,
2102+ [=random functions=] on different elements
2103+ will use distinct [=random base values=] .
2104+
2105+ If specified,
2106+ [=random functions=] on different elements
2107+ will share the same [=random base value=]
2108+ if they have the same <<dashed-ident>> (if specified)
2109+ or if they're used in the same style in the same way (if not).
2110+ (Again, see <a href="#auto-naming-details">the note below</a> for a small wrinkle about this.)
2111+
2112+ : <dfn><<number>></dfn>
2113+ :: If <<number>> is specified,
2114+ it's used as the [=random function's=] [=random base value=] ,
2115+ rather than allowing the UA to generate a [=random base value=]
2116+ and controlling how it's shared.
2117+
2118+ Note: While this <em> might</em> be useful for authors
2119+ to get predictable values while testing a page,
2120+ the reason it exists is to fix some corner cases in [=inheritance=]
2121+ that would otherwise cause elements to <em> not</em> share [=random base values=]
2122+ when authors would expect them to.
2123+ It is observable in some rare [=computed values=] ,
2124+ but otherwise will never show up in an element's styles.
2125+ </dl>
2126+
2127+ <div class=example>
2128+ * <b> Maximum random</b> :
2129+ both properties get a different value,
2130+ and it's different per element too,
2131+ so you get lots of random rectangles.
2132+
2133+ <pre highlight=css>
2134+ .random-rect {
2135+ width: random(100px, 200px);
2136+ height: random(100px, 200px);
2137+ }
2138+ </pre>
2139+
2140+ * <b> Shared by name within an element</b> :
2141+ both properties get the same value,
2142+ but it's still different per element,
2143+ so you get lots of random squares.
2144+
2145+ <pre highlight=css>
2146+ .random-square {
2147+ width: random(--foo, 100px, 200px);
2148+ height: random(--foo, 100px, 200px);
2149+ }
2150+ </pre>
2151+
2152+ * <b> Shared between elements within a property</b> :
2153+ both properties get different values,
2154+ but they're shared by every element,
2155+ so you get lots of identical rectangles of a single random size.
2156+
2157+ <pre highlight=css>
2158+ .shared-random-rect {
2159+ width: random(match-element, 100px, 200px);
2160+ height: random(match-element, 100px, 200px);
2161+ }
2162+ </pre>
2163+
2164+ * <b> Shared globally</b> :
2165+ both properties get the same value,
2166+ and every element shares that value,
2167+ so you get lots of identical squares of a single random size.
2168+
2169+ <pre highlight=css>
2170+ .shared-random-squares {
2171+ width: random(--foo match-element, 100px, 200px);
2172+ height: random(--foo match-element, 100px, 200px);
2173+ }
2174+ </pre>
2175+ </div>
2176+
2177+ <details class=note id=auto-naming-details>
2178+ <summary> Details about how ''random()/auto'' works</summary>
2179+
2180+ Technically, the ''random()/auto'' value generates a name
2181+ similar to an author specifying a <<dashed-ident>> ,
2182+ but it's auto-generated using the property the [=random function=] shows up in,
2183+ and its index among multiple [=random functions=] in that property.
2184+ For example, in ''margin: random(0px, 10px) random(0px, 10px)'' ,
2185+ the first function gets a name of "margin 0"
2186+ while the second gets a name of "margin 1".
2187+
2188+ This ensures that every auto-generated name on a single element is unique,
2189+ and thus will get their own [=random base value=] .
2190+ It also ensures that declarations like
2191+ ''.foo { width: random(match-element, 100px, 200px); }''
2192+ will cause all the ''.foo'' elements to share a [=random base value=] for their width
2193+ (they all have the same auto-generated name, "width 0").
2194+
2195+ However, it also means that a few other cases can create sharing that might be unexpected.
2196+ For example, in the following:
2197+
2198+ <pre highlight=css>
2199+ .foo {
2200+ width: random(match-element, 100px, 200px);
2201+ }
2202+ .bar {
2203+ width: random(match-element, 200px, 300px);
2204+ }
2205+ </pre>
2206+
2207+ Both ''.foo'' and ''.bar'' elements have ''random()'' functions
2208+ whose auto-generated names are "width 0",
2209+ so they'll share the same [=random base value=]
2210+ and generate related random values,
2211+ even if they were never intended to be linked together like that.
2212+
2213+ Similarly, in the following:
2214+
2215+ <pre highlight=css>
2216+ .foo {
2217+ animation: flicker linear random(1s, 2s);
2218+ }
2219+ .foo:hover {
2220+ animation: glow linear random(4s, 6s);
2221+ }
2222+ </pre>
2223+
2224+ The two ''random()'' functions have the same auto-generated name "animation 0",
2225+ so they'll share [=random base values=] on a single element
2226+ and generate related random values,
2227+ even tho the author likely considers them to be "different uses".
2228+
2229+ This unexpected sharing is, unfortunately, unavoidable.
2230+ CSS generally doesn't care <em> how</em> you organize styles for your elements;
2231+ in particular, whether you style several elements
2232+ with a single style rule like ''.foo, .bar {...}'' ,
2233+ multiple style rules like ''.foo {...} .bar {...}'' ,
2234+ or by setting the <{html-global/style}> attribute on each element individually,
2235+ you'll get the same results.
2236+ This needs to continue to be true even when you're using random values,
2237+ so we can't distinguish random values by how they're <em> set</em> ,
2238+ only how they're <em> used</em> .
2239+
2240+ There's no syntactic distinction in a stylesheet
2241+ between styling "related" elements and "independent" elements,
2242+ or distinguishing "independent" states,
2243+ so we must treat all elements as potentially related for this purpose.
2244+ If this is undesirable,
2245+ you can always supply a sufficiently unique <<dashed-ident>> yourself,
2246+ like ''random(--sidebar-width match-element, ...)'' on one set of elements
2247+ and ''random(--card-width match-element, ...)'' on another,
2248+ or ''random(--flicker, ...)'' and ''random(--glow, ...)'' on the different rules.
2249+
2250+ This is the one case where supplying a <<dashed-ident>>
2251+ can <em> reduce</em> the sharing of [=random base values=]
2252+ versus omitting it.
2253+ </details>
2254+
21532255 All of the [=calculation=] arguments can resolve to any value,
21542256 but must have a [=consistent type=]
21552257 or else the function is invalid;
0 commit comments