Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(css): add module page for CSS Properties and Values API #30503

Merged
merged 24 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
671fd98
docs(css): add module page for CSS Properties and Values API
bsmth Nov 24, 2023
af2ed26
docs(css): remove unnecessary string identifier
bsmth Nov 24, 2023
28f63ba
docs(css): fix flaws in module pages
bsmth Nov 24, 2023
29012d3
docs(css): check-in changes, remove unnecessary styles
bsmth Nov 24, 2023
1f5848f
docs(css): changes following reviewer feedback, conform to existing d…
bsmth Nov 27, 2023
8dca1c6
Merge branch 'main' into css-module-custom-prop
bsmth Nov 27, 2023
322febd
Apply suggestions from code review
bsmth Nov 27, 2023
b7e0dec
Merge branch 'main' into css-module-custom-prop
bsmth Nov 27, 2023
7d11339
docs(css): changes following reviewer feedback, make hidden examples …
bsmth Nov 27, 2023
9671942
docs(css): changes following reviewer feedback
bsmth Nov 27, 2023
ab5879f
docs(css): changes following reviewer feedback
bsmth Nov 27, 2023
d7473a5
Update files/en-us/web/css/css_cascading_variables/index.md
bsmth Nov 27, 2023
1e4b35b
docs(css): changes following reviewer feedback
bsmth Nov 27, 2023
0ceef8d
docs(css): changes following reviewer feedback
bsmth Nov 27, 2023
b0a01c6
Apply suggestions from code review
bsmth Dec 15, 2023
5a368c4
Fix typos
bsmth Dec 15, 2023
e7b3f62
docs(css): Move 'syntax strings' up into syntax DL
bsmth Dec 15, 2023
1ae4cd7
docs(css): Improvements following reviewer feedback
bsmth Dec 15, 2023
3c4ae0b
Update files/en-us/web/css/css_properties_and_values_api/index.md
bsmth Dec 15, 2023
9fb459f
Apply suggestions from code review
bsmth Dec 18, 2023
e7c2f87
docs(css): Correct internal xrefs to use locale / root relative
bsmth Dec 18, 2023
34c88c8
docs(css): Improvements following reviewer feedback
bsmth Dec 18, 2023
938afd3
Merge branch 'main' into css-module-custom-prop
bsmth Dec 18, 2023
fdd0310
Merge branch 'main' into css-module-custom-prop
bsmth Dec 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions files/en-us/web/api/css/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ title: CSS
slug: Web/API/CSS
page-type: web-api-interface
browser-compat: api.CSS
spec-urls:
- https://drafts.csswg.org/cssom/#namespacedef-css
- https://www.w3.org/TR/css-properties-values-api-1/
---

{{APIRef("CSSOM")}}
Expand Down
3 changes: 2 additions & 1 deletion files/en-us/web/css/--_star_/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Custom properties are scoped to the element(s) they are declared on, and partici

## See also

- [Using CSS variables](/en-US/docs/Web/CSS/Using_CSS_custom_properties)
- The {{cssxref("var", "var()")}} function
- {{cssxref("@property")}} at-rule
- [Using CSS custom properties (variables)](/en-US/docs/Web/CSS/Using_CSS_custom_properties) guide
- [CSS custom properties for cascading variables](/en-US/docs/Web/CSS/CSS_cascading_variables) module
17 changes: 13 additions & 4 deletions files/en-us/web/css/@property/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@ The `@property` rule represents a custom property registration directly in a sty
### Descriptors

- {{cssxref("@property/syntax","syntax")}}
- : Describes the allowable syntax for the property.

- : Describes the allowable syntax for the property. May be a `<length>`, `<number>`, `<percentage>`, `<length-percentage>`, `<color>`, `<image>`, `<url>`, `<integer>`, `<angle>`, `<time>`, `<resolution>`, `<transform-function>`, or `<custom-ident>`, or a list of data type and keyword values.

The `+` (space-separated) and `#` (comma-separated) multipliers indicate that a list of values is expected, for example `<color>#` means a comma-separated list of `<color>` values is the expected syntax.

Vertical lines (`|`) can create "or" conditions for the expected syntax, for example `<length> | auto` accepts a `<length>`, or `auto`, and `<color># | <integer>#` expects a comma-separated list of `<color>` values or a comma-separated list of `<integer>` values.
bsmth marked this conversation as resolved.
Show resolved Hide resolved
bsmth marked this conversation as resolved.
Show resolved Hide resolved

- {{cssxref("@property/inherits","inherits")}}
- : Controls whether the custom property registration specified by `@property` inherits by default.
- {{cssxref("@property/initial-value","initial-value")}}
- : Sets the initial value for the property.

A valid `@property` rule represents a custom property registration, with the property name being the serialization of the in the rule's prelude.

`@property` rules require a {{cssxref("@property/syntax","syntax")}} and {{cssxref("@property/inherits","inherits")}} descriptor; if either are missing, the entire rule is invalid and must be ignored. The {{cssxref("@property/initial-value","initial-value")}} descriptor is optional only if the syntax is the <a href="https://drafts.css-houdini.org/css-properties-values-api/#universal-syntax-definition">universal syntax definition</a>, otherwise the descriptor is required; if it's missing, the entire rule is invalid and must be ignored.
The `@property` rule must include both the {{cssxref("@property/syntax","syntax")}} and {{cssxref("@property/inherits","inherits")}} descriptors; if either are missing, the entire `@property` rule is invalid and ignored.
The {{cssxref("@property/initial-value","initial-value")}} descriptor is also required, unless the syntax is the [`*` universal syntax definition](https://drafts.css-houdini.org/css-properties-values-api/#universal-syntax-definition) (e.g., `initial-value: *`).
If the `initial-value` descriptor is required and omitted, the entire `@property` rule is invalid and ignored.

Unknown descriptors are invalid and ignored, but do not invalidate the `@property` rule.

Expand Down Expand Up @@ -126,7 +132,10 @@ For item three, the `--item-size` value gets set to `1000px`. While `1000px` is

## See also

- {{cssxref("var")}}
- [CSS Properties and Values API](/en-US/docs/Web/API/CSS_Properties_and_Values_API)
- [CSS Painting API](/en-US/docs/Web/API/CSS_Painting_API)
- [CSS Typed Object Model](/en-US/docs/Web/API/CSS_Typed_OM_API)
- [CSS Houdini](/en-US/docs/Web/Guide/Houdini)
- [Using CSS custom properties (variables)](/en-US/docs/Web/CSS/Using_CSS_custom_properties) guide
- [CSS custom properties for cascading variables](/en-US/docs/Web/CSS/CSS_cascading_variables) module
111 changes: 109 additions & 2 deletions files/en-us/web/css/css_cascading_variables/index.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,127 @@
---
title: CSS cascading variables
title: CSS custom properties for cascading variables
slug: Web/CSS/CSS_cascading_variables
page-type: css-module
spec-urls: https://drafts.csswg.org/css-variables/
---

{{CSSRef}}

The **CSS cascading variables** module adds support for cascading variables in all CSS properties and also lets you create custom properties to define these variables.
The **CSS custom properties for cascading variables** module adds support for cascading variables in CSS properties and lets you create custom properties to define these variables and the mechanisms to use custom properties as the values for other CSS properties.
bsmth marked this conversation as resolved.
Show resolved Hide resolved
bsmth marked this conversation as resolved.
Show resolved Hide resolved

When working with CSS, you often end up reusing common project-specific values such as widths that work well with your layout, or a set of colors for your color scheme.
One way of managing repetition in stylesheets is to define a value once and use it many times in other places.
Custom properties, also known as custom variables, let you reuse values and simplify complex or repetitive rules which are easier to read and maintain.
bsmth marked this conversation as resolved.
Show resolved Hide resolved
For example, `--dark-grey-text` and `--dark-footer` are easier to understand than hexadecimal colors such as `#00ff00`, and the context of how you use them is more obvious, too.
bsmth marked this conversation as resolved.
Show resolved Hide resolved

## Custom properties in action

To see how custom properties can be used, move the input slider left to right.

```html hidden
<div class="container">
<div id="color-1">--hue</div>
<div id="color-2">--hue + 10</div>
<div id="color-3">--hue + 20</div>
<div id="color-4">--hue + 30</div>
<div id="color-5">--hue + 40</div>
<div id="color-6">--hue + 50</div>
<div id="color-7">--hue + 60</div>
<div id="color-8">--hue + 70</div>
</div>
<input type="range" min="0" max="360" value="0" step="0.1" id="hue" />
```

```js hidden
const hue = document.querySelector("#hue");

const updateHue = () => {
document.documentElement.style.setProperty("--hue", hue.value);
};

hue.addEventListener("input", updateHue);
```

```css hidden
.container {
display: grid;
font-family: sans-serif;
color: white;
gap: 0.5rem;
grid-template-columns: repeat(4, 1fr);
margin-bottom: 1rem;
}
.container div {
border-radius: 0.5rem;
padding: 1rem;
}

input {
width: 100%;
margin: 0;
}

:root {
--hue: 0;
}

#color-1 {
background-color: hsl(var(--hue) 50% 50%);
}
#color-2 {
background-color: hsl(calc(var(--hue) + 10) 50% 50%);
}
#color-3 {
background-color: hsl(calc(var(--hue) + 20) 50% 50%);
}
#color-4 {
background-color: hsl(calc(var(--hue) + 30) 50% 50%);
}
#color-5 {
background-color: hsl(calc(var(--hue) + 40) 50% 50%);
}
#color-6 {
background-color: hsl(calc(var(--hue) + 50) 50% 50%);
}
#color-7 {
background-color: hsl(calc(var(--hue) + 60) 50% 50%);
}
#color-8 {
background-color: hsl(calc(var(--hue) + 70) 50% 50%);
}
```

{{EmbedLiveSample("",600,160)}}
bsmth marked this conversation as resolved.
Show resolved Hide resolved

Each color swatch increments the {{cssxref("hue")}} value by 10 degrees, setting the {{cssxref("background-color")}} using the {{cssxref("color_value/hsl", "hsl()")}} {{cssxref("&lt;color&gt;")}} function (`hsl(var(--hue) 50% 50%);`). As the slider's value changes from 0 up to 360, the value of the `--hue` [custom property](/en-US/docs/Web/CSS/--*) is updated, updating the background color of each box inside the grid.
Copy link
Member

Choose a reason for hiding this comment

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

this suggestion may be overkill. And, while pedantic, the equation i suggested is off by 10. optional suggestion, and gives us a reason to link to calc()

Suggested change
Each color swatch increments the {{cssxref("hue")}} value by 10 degrees, setting the {{cssxref("background-color")}} using the {{cssxref("color_value/hsl", "hsl()")}} {{cssxref("&lt;color&gt;")}} function (`hsl(var(--hue) 50% 50%);`). As the slider's value changes from 0 up to 360, the value of the `--hue` [custom property](/en-US/docs/Web/CSS/--*) is updated, updating the background color of each box inside the grid.
Each color swatch increments the {{cssxref("hue")}} value by 10 degrees, setting the {{cssxref("background-color")}} using the {{cssxref("color_value/hsl", "hsl()")}} {{cssxref("&lt;color&gt;")}} function (`hsl( calc( var( --hue ) + ( itemNumber * 10 ) ) 50% 50%);`). As the slider's value changes from 0 up to 360, the value of the `--hue` [custom property](/en-US/docs/Web/CSS/--*) is updated, updating the background color of each box inside the grid.

Copy link
Member Author

Choose a reason for hiding this comment

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

Taking a look how it renders now 👀

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's a little hard to mentally parse:

image

What do you think about this:

In these color swatches, the {{cssxref("background-color")}} is set using the {{cssxref("color_value/hsl", "hsl()")}} {{cssxref("&lt;color&gt;")}} function as `hsl(var(--hue) 50% 50%)`.
Each color swatch increments the {{cssxref("hue")}} value by 10 degrees like `calc(var(--hue) + 10)`, `calc(var(--hue) + 20)` etc.
As the slider's value changes from 0 up to 360, the value of the `--hue` [custom property](/en-US/docs/Web/CSS/--*) is updated using {{cssxref("calc")}}, and the background color of each box inside the grid is updated, also.

i.e.:

In these color swatches, the {{cssxref("background-color")}} is set using the {{cssxref("color_value/hsl", "hsl()")}} {{cssxref("<color>")}} function as hsl(var(--hue) 50% 50%).
Each color swatch increments the {{cssxref("hue")}} value by 10 degrees like calc(var(--hue) + 10), calc(var(--hue) + 20) etc.
As the slider's value changes from 0 up to 360, the value of the --hue custom property is updated using {{cssxref("calc")}}, and the background color of each box inside the grid is updated, also.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to commit this edit above, but feedback welcome

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in 34c88c8


## Reference

### Properties

- {{cssxref("--*")}}

### Functions

- {{cssxref("var")}}

## Guides

- [Using CSS custom properties (variables)](/en-US/docs/Web/CSS/Using_CSS_custom_properties)
- : Explains how to use custom properties in CSS and JavaScript, with hints on handling undefined and invalid values, fallbacks, and inheritance.
bsmth marked this conversation as resolved.
Show resolved Hide resolved

bsmth marked this conversation as resolved.
Show resolved Hide resolved
## Related concepts

- [CSS Properties and Values API](/en-US/docs/Web/CSS/CSS_properties_and_values_API) module
- [`@property`](/en-US/docs/Web/CSS/@property) at-rule
- [`CSS.registerProperty()`](/en-US/docs/Web/API/CSS/registerProperty_static) method

## Specifications

{{Specifications}}

## See also

- [CSS cascade and inheritance](/en-US/docs/Web/CSS/CSS_cascade) module
- [CSS `env()`](Web/CSS/env)
- [getPropertyValue()](/en-US/docs/Web/API/CSSStyleDeclaration/getPropertyValue)
bsmth marked this conversation as resolved.
Show resolved Hide resolved
106 changes: 106 additions & 0 deletions files/en-us/web/css/css_properties_and_values_api/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
title: CSS properties and values API
slug: Web/CSS/CSS_properties_and_values_API
page-type: css-module
spec-urls: https://www.w3.org/TR/css-properties-values-api-1/
---

{{CSSRef}}

The **CSS properties and values API** module defines a method for registering new CSS properties, defining the property's data type, inheritance behavior, and, optionally, an initial value. This API expands on [CSS custom properties for cascading variables](Web/CSS/CSS_cascading_variables) module, which allows authors to define custom properties in CSS using [two dash syntax (`--`)](/en-US/docs/Web/CSS/--*). The CSS properties and values API is part of the [CSS Houdini](/en-US/docs/Web/CSS/CSS_houdini) umbrella of APIs.
bsmth marked this conversation as resolved.
Show resolved Hide resolved

Custom properties let you reuse values across a project to simplify complex or repetitive stylesheets. Basic custom properties are defined in the [CSS custom properties for cascading variables](Web/CSS/CSS_cascading_variables) module. The CSS properties and values API expands on that module, enabling adding metadata to custom properties using CSS with the [`@property`](/en-US/docs/Web/CSS/@property) at-rule or, alternatively, using JavaScript's {{domxref('CSS/registerProperty_static', 'CSS.registerProperty')}} method.

Whether registered with CSS or JavaScript, setting metadata on custom properties provides for an expected data type that the browser can use depending on the context, defines an initial value, and lets you control inheritance.

CSS properties and values API custom property registration is more robust than the more basic CSS cascading variable custom property declaration, especially when it comes to transitioning and animating values as browsers can interpolate between custom values of this type, whereas properties that use [two dash syntax (`--`)](/en-US/docs/Web/CSS/--*) behave more like a string substitution.

## Properties and values API in action

To see how custom properties and values can be used via API, hover over the box below.

```js hidden
CSS.registerProperty({
bsmth marked this conversation as resolved.
Show resolved Hide resolved
name: "--stop-color",
syntax: "<color>",
inherits: false,
initialValue: "cornflowerblue",
});
```

```css hidden
.box {
padding: 1rem;
width: 90%;
height: 4rem;
font-family: sans-serif;
font-size: large;
color: white;
border-radius: 0.5rem;
}

.box {
background: linear-gradient(to right, var(--stop-color), lavenderblush);
transition: --stop-color 2s;
}

.box:hover {
--stop-color: aquamarine;
}
```

```html hidden
<div class="box"><p>Linear gradient with transition</p></div>
bsmth marked this conversation as resolved.
Show resolved Hide resolved
```

{{EmbedLiveSample("",600,120)}}

The box has a [backround](/en-US/docs/Web/CSS/background) consisting of a [linear gradient](/en-US/docs/Web/CSS/gradient/linear-gradient) from `--stop-color` (the custom property) to [`lavenderblush`](/en-US/docs/Web/CSS/named-color).
bsmth marked this conversation as resolved.
Show resolved Hide resolved
The value of `--stop-color` is set to `cornflowerblue` at first, but when you hover over the box, `--stop-color` [transitions](/en-US/docs/Web/CSS/transition) to `aquamarine` over two seconds (`linear-gradient(to right, aquamarine, lavenderblush)`).
bsmth marked this conversation as resolved.
Show resolved Hide resolved

## Reference

### At-rules
bsmth marked this conversation as resolved.
Show resolved Hide resolved

- {{cssxref("@property")}}
bsmth marked this conversation as resolved.
Show resolved Hide resolved
- [syntax](/en-US/docs/Web/CSS/@property#descriptors) descriptor
- [`+` and `#`](/en-US/docs/Web/CSS/@property#descriptors) multipliers
- [`|`](/en-US/docs/Web/CSS/@property#descriptors) combinator
- [inherits](/en-US/docs/Web/CSS/@property#descriptors) descriptor
- [initial-value](/en-US/docs/Web/CSS/@property#descriptors) descriptor

### Interfaces and APIs

- {{domxref('CSSPropertyRule')}}
- {{domxref('CSS/registerProperty_static', 'CSS.registerProperty()')}}

## Guides

- [Using the CSS properties and values API](/en-US/docs/Web/API/CSS_Properties_and_Values_API/guide)
- : Explains how to register custom properties in CSS and JavaScript, with hints on handling undefined and invalid values, fallbacks, and inheritance.

## Related concepts

- {{cssxref("var")}}
- [CSSRule](/en-US/docs/Web/API/CSSRule)
- [CSSStyleValue](/en-US/docs/Web/API/CSSStyleValue)
- [CSS scoping](/en-US/docs/Web/CSS/CSS_scoping)
- [Using shadow DOM](/en-US/docs/Web/API/Web_components/Using_shadow_DOM)
- [CSS Typed Object Model API](/en-US/docs/Web/API/CSS_Typed_OM_API)
- [CSS Painting API](/en-US/docs/Web/API/CSS_Painting_API)
- [Worklet](/en-US/docs/Web/API/Worklet)
bsmth marked this conversation as resolved.
Show resolved Hide resolved

## Specifications
bsmth marked this conversation as resolved.
Show resolved Hide resolved

{{Specifications}}

## See also

- [CSS cascade and inheritance](/en-US/docs/Web/CSS/CSS_cascade)
bsmth marked this conversation as resolved.
Show resolved Hide resolved
- [CSS scoping](/en-US/docs/Web/CSS/CSS_scoping) module
- [Using shadow DOM](/en-US/docs/Web/API/Web_components/Using_shadow_DOM)
- [CSS Painting API](/en-US/docs/Web/API/CSS_Painting_API) module
- [Worklet](/en-US/docs/Web/API/Worklet) interface
- [CSS Houdini](/en-US/docs/Web/Guide/Houdini)
bsmth marked this conversation as resolved.
Show resolved Hide resolved
- [CSS `env()`](Web/CSS/env)
- [CSS Typed Object Model](/en-US/docs/Web/API/CSS_Typed_OM_API)
4 changes: 2 additions & 2 deletions files/en-us/web/css/env/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ main {
## See also

- {{CSSxRef("var", "var(…)")}}
- [CSS Custom Properties for Cascading Variables](/en-US/docs/Web/CSS/CSS_cascading_variables)
- [Custom Properties (--\*)](/en-US/docs/Web/CSS/--*)
- [CSS custom properties for cascading variables](/en-US/docs/Web/CSS/CSS_cascading_variables) module
- [Custom properties (--\*): CSS variables](/en-US/docs/Web/CSS/--*)
bsmth marked this conversation as resolved.
Show resolved Hide resolved
- [Using CSS custom properties (variables)](/en-US/docs/Web/CSS/Using_CSS_custom_properties)
- [Customize the window controls overlay of your PWA's title bar](https://web.dev/articles/window-controls-overlay)
- [Display content in the title bar](https://docs.microsoft.com/microsoft-edge/progressive-web-apps-chromium/how-to/window-controls-overlay)
Expand Down
3 changes: 2 additions & 1 deletion files/en-us/web/css/using_css_custom_properties/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,5 +535,6 @@ element.style.setProperty("--my-var", jsVar + 4);

- [Custom property syntax](/en-US/docs/Web/CSS/--*)
- {{cssxref("@property")}} at-rule
- [CSS Properties and Values API](/en-US/docs/Web/API/CSS_Properties_and_Values_API)
- [`var()`](/en-US/docs/Web/CSS/var)
- [CSS Properties and Values API](/en-US/docs/Web/API/CSS_Properties_and_Values_API)
- [CSS custom properties for cascading variables](/en-US/docs/Web/CSS/CSS_cascading_variables) module
3 changes: 2 additions & 1 deletion files/en-us/web/css/var/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,6 @@ Since `--main-bg-color` isn't set, the body's `background-color` will fall back
## See also

- {{cssxref("env","env(…)")}} – read‑only environment variables controlled by the user‑agent.
- [Using CSS variables](/en-US/docs/Web/CSS/Using_CSS_custom_properties)
- [Using CSS custom properties (variables)](/en-US/docs/Web/CSS/Using_CSS_custom_properties)
- {{cssxref("@property")}} at-rule
- [CSS custom properties for cascading variables](/en-US/docs/Web/CSS/CSS_cascading_variables) module