Skip to content

Commit 16efed0

Browse files
yashrajbhartiyashrajbharticybtekkchrisdavidmills
authored
Feat/property example (mdn#40607)
* Add: linear gradient example to @Property * Add: linear gradient example to @Property * Update files/en-us/web/css/@property/index.md Co-authored-by: Chris Mills <chrisdavidmills@gmail.com> * Update files/en-us/web/css/@property/index.md Co-authored-by: Chris Mills <chrisdavidmills@gmail.com> * Update files/en-us/web/css/@property/index.md Co-authored-by: Chris Mills <chrisdavidmills@gmail.com> * Update index.md * fix: percentage link * linear gradient link --------- Co-authored-by: yashrajbharticybtekk <y.bharti@cybtekk.com> Co-authored-by: Chris Mills <chrisdavidmills@gmail.com>
1 parent 3708005 commit 16efed0

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

files/en-us/web/css/@property/index.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ We use the two custom properties to style the items:
118118
}
119119
```
120120

121-
{{ EmbedLiveSample('examples', '100%', '250px') }}
121+
{{ EmbedLiveSample('Using `@property` to register and use a custom property', '100%', '250px') }}
122122

123123
The two custom properties, `--item-size: 20%` and `--item-color: orange;` are set on the `container` parent, overriding the `40%` and `aqua` default values set when these custom properties were defined. The size is set to be inheritable; the color is not.
124124

@@ -128,6 +128,43 @@ For item two, CSS global keywords are set for both custom properties which are v
128128

129129
For item three, the `--item-size` value gets set to `1000px`. While `1000px` is a {{cssxref("length")}} value, the `@property` declaration requires the value be a `<percentage>`, so the declaration is not valid and is ignored, meaning the inheritable `20%` set on the parent is used. The `xyz` value is also invalid. As `registerProperty()` set `--item-color` to not be inherited, the default initial value of `aqua` is used and not the parent's `orange` value.
130130

131+
### Animating a custom property value
132+
133+
In this example, we define a custom property called `--progress` using `@property`: this accepts [`<percentage>`](/en-US/docs/Web/CSS/percentage) values and has an initial value of `25%`. We use `--progress` to define the position value of the color stops in a {{cssxref("linear-gradient()")}}, specifying where a green color stops, and black starts. We then animate the value of `--progress` to `100%` over 2.5 seconds, giving the effect of animating a progress bar.
134+
135+
```html
136+
<div class="bar"></div>
137+
```
138+
139+
```css
140+
@property --progress {
141+
syntax: "<percentage>";
142+
inherits: false;
143+
initial-value: 25%;
144+
}
145+
146+
.bar {
147+
display: inline-block;
148+
--progress: 25%;
149+
width: 100%;
150+
height: 5px;
151+
background: linear-gradient(
152+
to right,
153+
#00d230 var(--progress),
154+
#000000 var(--progress)
155+
);
156+
animation: progressAnimation 2.5s ease infinite;
157+
}
158+
159+
@keyframes progressAnimation {
160+
to {
161+
--progress: 100%;
162+
}
163+
}
164+
```
165+
166+
{{ EmbedLiveSample('Animating a custom property value', '100%', '60px') }}
167+
131168
## Specifications
132169

133170
{{Specifications}}

0 commit comments

Comments
 (0)