Sometimes we encounter this situation where the container is stretched by elements and its height cannot be known, but a smooth transition is required. Like this:
I believe that CSS beginners have tried this:
|
|
Then I got a cold shoulder. The height does change when the mouse is touched, but the transition does not take effect. This is because transition
can only handle transitions between values, and changes between auto - 0 cannot be transitioned.
Now this transition can be achieved through the
calc-size
attribute. For details, please see this post, but the compatibility is not very good at present.
So what can we do? At this time, we need to bring out our display: grid
. The grid layout can control the width-to-height ratio of its internal container through the fr
unit. That’s a coincidence, folks. We just need to change the height ratio between 1 and 0, right?
Below is the code for the head-touching expansion example above:
|
|
The effect is completed in a simple way. The knowledge about grid layout cannot be explained in a few words. Students who are interested can learn it.
Reference to this article
How to make CSS auto height perfectly support transition animation?