Skip to content

Commit e7cd60c

Browse files
Add CSS typed arithmetic examples (#339)
* Add CSS typed arithmetic examples * Add entry to README file * code simplification / improvement * A few more small improvements
1 parent 9fde42e commit e7cd60c

File tree

8 files changed

+215
-0
lines changed

8 files changed

+215
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Code examples that accompany various MDN DOM and Web API documentation pages.
3131

3232
- The "css-progress" directory contains an example demonstrating the [CSS `progress()` function](https://developer.mozilla.org/en-US/docs/Web/CSS/progress). See the [example live](https://mdn.github.io/dom-examples/css-progress).
3333

34+
- The "css-typed-arithmetic" directory contains an example demonstrating [CSS typed arithmetic](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Values_and_Units/Using_CSS_typed_arithmetic) usage. See the [index page](https://mdn.github.io/dom-examples/css-typed-arithmetic) to access the live examples.
35+
3436
- The "device-posture-api" directory contains an example demonstrating how to use the [Device Posture API](https://developer.mozilla.org/docs/Web/API/Device_Posture_API). [Run the example live](https://mdn.github.io/dom-examples/device-posture-api/).
3537

3638
- The "drag-and-drop" directory is for examples and demos of the [HTML Drag and Drop](https://developer.mozilla.org/docs/Web/API/HTML_Drag_and_Drop_API) standard.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Animated story circle</title>
6+
<link href="style.css" rel="stylesheet" />
7+
</head>
8+
<body>
9+
<div class="support-info">
10+
The rotation of each colored story section is calculated based on its
11+
<code>sibling-index()</code> multiplied by a ratio of the width of the
12+
<code>&lt;body&gt;</code> element and <code>1200px</code>. At the time of
13+
writing, this works in Chromium 140+ and Safari 26+.
14+
</div>
15+
<div class="story-circle">
16+
<p><strong>Hello there!</strong></p>
17+
<p>This is</p>
18+
<p>quite an</p>
19+
<p>interesting way</p>
20+
<p>to tell a</p>
21+
<p>story</p>
22+
<p>all around in</p>
23+
<p>a circle.</p>
24+
<p>What fun!</p>
25+
</div>
26+
</body>
27+
</html>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
:root {
2+
height: 100%;
3+
font-family: sans-serif;
4+
}
5+
6+
body {
7+
height: inherit;
8+
margin: 0 auto;
9+
min-width: 400px;
10+
max-width: 1600px;
11+
display: flex;
12+
align-items: center;
13+
justify-content: center;
14+
container-type: inline-size;
15+
}
16+
17+
.story-circle {
18+
width: 1px;
19+
height: 1px;
20+
--width-percentage: calc((100cqw / 1200px) - 0.33333);
21+
}
22+
23+
p {
24+
padding: 10px;
25+
width: 150px;
26+
text-align: right;
27+
background: linear-gradient(to right, red, orange 50%);
28+
border-radius: 5px;
29+
30+
position: absolute;
31+
transform-origin: center left;
32+
33+
--angle: calc(
34+
((sibling-index() - 1) * (360 / sibling-count())) * var(--width-percentage)
35+
);
36+
rotate: calc(var(--angle) * 1deg);
37+
}
38+
39+
/* Support information */
40+
41+
.support-info {
42+
padding: 5px;
43+
background-color: yellow;
44+
border: 1px solid black;
45+
position: absolute;
46+
top: 5px;
47+
left: 5px;
48+
right: 5px;
49+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>CSS typed arithmetic demo</title>
6+
<link href="style.css" rel="stylesheet" />
7+
</head>
8+
<body>
9+
<div class="support-info">
10+
The paragraph <code>background-color</code> and
11+
<code>font-size</code> should change depending on the viewport width, as
12+
expressed in pixels in the <code>--viewport-in-pixels</code> custom
13+
property. At the time of writing, this works in Chromium 140+ and Safari
14+
26+.
15+
</div>
16+
<p>Hello there!</p>
17+
</body>
18+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
:root {
2+
font-family: sans-serif;
3+
--viewport-in-pixels: calc(100vw / 1px);
4+
}
5+
6+
p {
7+
border: 5px solid black;
8+
text-align: center;
9+
font-size: calc(1em * (var(--viewport-in-pixels) / 200));
10+
background-color: lch(
11+
75% 50% calc((100 + (var(--viewport-in-pixels) / 10)) * 1deg)
12+
);
13+
}
14+
15+
/* Support information */
16+
17+
.support-info {
18+
padding: 5px;
19+
background-color: yellow;
20+
border: 1px solid black;
21+
}

css-typed-arithmetic/index.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width" />
7+
8+
<title>CSS typed arithmetic examples</title>
9+
</head>
10+
11+
<body>
12+
<h1>CSS typed arithmetic examples</h1>
13+
<ul>
14+
<li>
15+
<a href="responsive-background-opacity/index.html"
16+
>Responsive background opacity</a
17+
>
18+
</li>
19+
<li>
20+
<a href="different-type-variations/index.html"
21+
>Different type variations</a
22+
>
23+
</li>
24+
<li>
25+
<a href="animated-story-circle/index.html">Animated story circle</a>
26+
</li>
27+
</ul>
28+
<h2>More information</h2>
29+
<ul>
30+
<li>
31+
<a
32+
href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Values_and_Units/Using_typed_arithmetic"
33+
>Using CSS typed arithmetic</a
34+
>
35+
</li>
36+
<li>
37+
<a
38+
href="https://github.com/mdn/dom-examples/tree/main/css-typed-arithmetic"
39+
>The code used on these pages</a
40+
>
41+
</li>
42+
</ul>
43+
</body>
44+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Responsive background opacity demo</title>
6+
<link href="style.css" rel="stylesheet" />
7+
</head>
8+
<body>
9+
<div class="wrapper">
10+
<h1>Prose of the century</h1>
11+
<p>
12+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus
13+
aliquam dolor, eu lacinia lorem placerat vulputate. Duis felis orci,
14+
pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc,
15+
at ultricies tellus laoreet sit amet.
16+
</p>
17+
<div class="support-info">
18+
The opacity of the background image decreases as the viewport width
19+
decreases. At the time of writing, this works in Chromium 140+.
20+
</div>
21+
</div>
22+
</body>
23+
</html>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
:root {
2+
font-family: sans-serif;
3+
--width-percentage: calc((100vw / 2000px));
4+
}
5+
6+
.wrapper {
7+
width: 480px;
8+
padding: 20px;
9+
margin: 0 auto;
10+
}
11+
12+
body {
13+
background: linear-gradient(
14+
rgb(255 255 255 / calc(1 - var(--width-percentage))),
15+
rgb(255 255 255 / calc(1 - var(--width-percentage)))
16+
),
17+
url("https://mdn.github.io/shared-assets/images/examples/colorful-heart.png")
18+
no-repeat top 50px right 50px;
19+
}
20+
21+
p {
22+
line-height: 1.5;
23+
}
24+
25+
/* Support information */
26+
27+
.support-info {
28+
padding: 5px;
29+
background-color: yellow;
30+
border: 1px solid black;
31+
}

0 commit comments

Comments
 (0)