Skip to content

Commit dae79b6

Browse files
Getting Started
1 parent a8b9cdf commit dae79b6

File tree

1 file changed

+108
-0
lines changed
  • src/pages/2024-07/css-focus-crash-course

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
layout: "@layouts/BlogPost.astro"
3+
title: "We Can Finally Animate height: auto; in CSS!"
4+
date: "2024-07-01"
5+
description: "For decades, height: auto; has been a pain to animate in CSS. But with the introduction of the calc-size() function in CSS, we can finally animate height: auto; without the need for any JavaScript."
6+
tags: ["CSS"]
7+
---
8+
9+
import CalcSizeComparison from "@blogComponents/cssCalcSize/CalcSizeComparison.astro"
10+
11+
## Introduction
12+
13+
Animating `height: auto;` in CSS seems like it should be easy, but CSS is unable to animate to/from `height: auto;` since it needs a specific height value to run any animation/transition. This has been a pain point for web developers for decades, and the only way to animate `height: auto;` was to use JavaScript to calculate the height of the element and then animate it. This is obviously not ideal, which is why CSS has finally added the brand new `calc-size()` function which makes this type of animation trivial.
14+
15+
## `calc-size()`
16+
17+
The `calc-size()` function works exactly the same as the `calc()` function, but it has the additional capability of calculating based on sizes that are automatically calculated by the browser. These values are:
18+
19+
1. `auto`
20+
2. `min-content`
21+
3. `max-content`
22+
4. `fit-content`
23+
5. `stretch`
24+
6. `contain`
25+
26+
Essentially, what this function does is convert values like `auto` to specific pixel values which it can then use in calculations with other values. This is handy on its own, but where it is most useful is with animating elements that are `auto` sized.
27+
28+
```css {8}
29+
.element {
30+
height: 0;
31+
overflow: hidden;
32+
transition: height 0.3s;
33+
}
34+
35+
.element.open {
36+
height: calc-size(auto);
37+
}
38+
```
39+
40+
By wrapping our `auto` value in the `calc-size()` function, we can now animate the height of the element from `0` to `auto` without any JavaScript. Here is an example of what this looks like in practice:
41+
42+
<CalcSizeComparison />
43+
44+
The only thing you need to be away of is that you cannot animate between two automatically calculated values, such as `auto` and `min-content`.
45+
46+
Another interesting thing about `calc-size()` is you can actually use it on the non-automatic value in the animation and it will still animate correctly. As long as you have `calc-size` on one of the values in the animation, it will work.
47+
48+
```css {3}
49+
.element {
50+
/* This still works */
51+
height: calc-size(0px);
52+
overflow: hidden;
53+
transition: height 0.3s;
54+
}
55+
56+
.element.open {
57+
height: auto;
58+
}
59+
```
60+
61+
### Doing Actual Calculations
62+
63+
By far the most common use for this will be with animations/transitions as shown above, but since this function works just like `calc` it can actually be used to do certain calculations that used to be impossible.
64+
65+
```css
66+
.element {
67+
width: calc-size(min-content, size + 50px);
68+
}
69+
```
70+
71+
The above CSS will set the width of the element to the minimum content size plus `50px`. The syntax for this is a bit confusing so let me explain.
72+
73+
`calc-size` takes two arguments, the first is the size that you want to calculate, and the second is the calculation you want to perform. In this case, we are calculating the `min-content` size of the element and then adding `50px` to that value. The keyword `size` is always used to represent the current size of the first property passed to `calc-size`. This means in our example `size` would be equal to the `min-content` size of the element.
74+
75+
You can even nest multiple `calc-size` functions to perform more complex calculations.
76+
77+
```css
78+
.element {
79+
width: calc-size(calc-size(min-content, size + 50px), size * 2);
80+
}
81+
```
82+
83+
This will calculate the `min-content` size of the element, add `50px` to that value, and then multiply the result by `2`.
84+
85+
## Browser Support
86+
87+
This is where we get to the bad news. As of writing this article, `calc-size()` is only supported in Chrome Canary when the `#enable-experimental-web-platform-features` flag is enabled. It is so new there isn't even a caniuse.com page for me to link to yet.
88+
89+
Luckily, this CSS feature is not something that will break your site if it isn't supported. It will just mean that the animation won't work, so you can use it today and it will act as a progressive enhancement for users on browsers that support it.
90+
91+
```css {8-9}
92+
.element {
93+
height: 0;
94+
overflow: hidden;
95+
transition: height 0.3s;
96+
}
97+
98+
.element.open {
99+
height: auto;
100+
height: calc-size(auto);
101+
}
102+
```
103+
104+
With the above CSS the animation will work in browsers that support `calc-size()` while in older browsers it will just show the element without any animation.
105+
106+
## Conclusion
107+
108+
`calc-size()` is a fantastic new addition to CSS that will make animating `auto` based sizes incredibly easy. It also opens up a lot of possibilities for doing calculations that were previously impossible in CSS. I can't wait for this feature to be supported in all browsers!

0 commit comments

Comments
 (0)