Table of Contents
1. The fr unit in the grid layout
二、grid 中的过渡动画
三、自适应高度动画的两个实例
四、注意事项和一些局限性
五、最后总结一下
Home Web Front-end CSS Tutorial Let's talk about how to make auto height support transition animation in CSS

Let's talk about how to make auto height support transition animation in CSS

Feb 06, 2023 pm 08:10 PM
css front end

How can CSS make auto height perfectly support transition animation? The following article will talk about how to make auto height support transition animation in CSS. I hope it will be helpful to you!

Let's talk about how to make auto height support transition animation in CSS

As we all know, the transition transition animation will not be triggered when the height is set to the auto keyword. The following is the pseudo code

div{
  height: 0;
  transition: 1s
}
.wrap:hover div{
  height: auto
}
Copy after login

The effect is as follows

Kapture 2023-01-31 at 19.19.59.gif

If you want to have a transition animation when expanding, for example like this

Kapture 2023-01-31 at 19.21.02.gif

Usually It uses JS to dynamically obtain the height of the element (it's a bit troublesome, you need to render it to know the height). In fact, CSS also has a solution that cleverly uses max-height to adapt to dynamic height. The following is the pseudo code [Recommended learning: css video tutorial]

div{
  max-height: 0;
  transition: 1s
}
.wrap :hover div{
  max-height: 800px /*大概的值,需要超过元素高度*/
}
Copy after login

If you are interested You can refer to this article: CSS tricks: dynamic height transition animation, but there is a limitation. The greater the height difference, the worse the transition effect. Assume that the real height of the element is only 100px, if max-height is 800px, then only the first 1/8 will have animation, like this

Kapture 2023-01-31 at 19.36.12.gif

So, is there a better way to solve this problem?

Of course there are some. This time I will introduce a new way to achieve dynamic height transition. Let’s take a look.

1. The fr unit in the grid layout

grid There is a new fr unit in the layout, which is used to define the elasticity factor of the grid track size. gridThe layout is relatively complicated and it is impossible to explain clearly in a few words. Those who are interested can refer to grid related tutorials, such as

Here is a brief introduction to the fr unit For example, there is such a layout

<div class="grid">
  <span class="item">1fr</span>
  <span class="item">1fr</span>
  <span class="item">1fr</span>
</div>
Copy after login

The key style is as follows

.grid{
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}
Copy after login

You can get such an effect

Lets talk about how to make auto height support transition animation in CSS

Here repeat(3, 1fr) is actually the abbreviation of 1fr 1fr 1fr, which means 3 equals the remaining space. You can also set it in the vertical direction

.grid{
  grid-template-rows: repeat(3, 1fr);
}
Copy after login

The effect is as follows

Lets talk about how to make auto height support transition animation in CSS

You can also change the respective distribution ratio

.grid{
  grid-template-rows: 1fr 2fr 1fr;
}
Copy after login
Copy after login

The effect is as follows

Lets talk about how to make auto height support transition animation in CSS

Now let’s look at a special case, you can also set the distribution ratio to0fr

.grid{
  grid-template-rows: 0fr 2fr 1fr;
}
Copy after login

The effect is as follows

Lets talk about how to make auto height support transition animation in CSS

Isn’t it a bit strange, how come 0fr has the same proportion as 1fr?

In fact, this is determined by the minimum size rule of grid. The minimum height at this time is min-content, which is determined by the internal text. If there is no text, 0fr naturally does not take up space. The following is the effect after removing the text

Lets talk about how to make auto height support transition animation in CSS

If you want to keep the text and not take up space What to do with space? You can directly set the minimum size manually

span{
  min-height: 0
}
Copy after login

This way 0fr will not take up space

Lets talk about how to make auto height support transition animation in CSS

You can also use Beyond Hide to completely hide the child Content

.grid{
  overflow: hidden;
}
span{
  min-height: 0
}
Copy after login

The effect is as follows

Lets talk about how to make auto height support transition animation in CSS

应该还是比较容易理解吧,那么和动画有啥关系呢?接着往下看

二、grid 中的过渡动画

有同学可能纳闷了,为啥要折腾这个0fr呢?下面就来揭晓

如果重新设置1fr,子内容又会重新出现

.grid{
  grid-template-rows: 1fr 2fr 1fr;
}
Copy after login
Copy after login

Lets talk about how to make auto height support transition animation in CSS

下面重点来了,grid中的fr单位也是支持过渡动画的(0fr=>1fr )

.grid{
  grid-template-rows: 1fr 2fr 1fr;  
  transition: .3s
}
Copy after login

效果如下

Kapture 2023-01-30 at 20.14.59.gif

由于高度是由内部文本撑开的,也就是高度不确定,而0fr1fr的过渡变化,相当于实现了 高度不固定的过渡动画

进一步精简一下,可以实现这样的效果

Kapture 2023-01-31 at 10.40.33.gif

这就是高度不固定动画的雏形了,换个文本多一点也完美支持

Kapture 2023-01-31 at 19.48.37.gif

完整 demo可以查看以下任意链接

三、自适应高度动画的两个实例

现在根据上面的原理来实现两个实例。

首先来看文章最开头的示例,HTML 结构是这样的

<div >
  <button >鼠标放上来试试</button>
  <div >
    <div><p>欢迎关注前端侦探,这里有一些有趣的、你可能不知道的HTML、CSS、JS小技巧技巧,比如这篇文章,如何让 auto height 支持过渡动画?一起看看吧</p></div>
  </div>
</div>
Copy after login

简单修饰一下,应该比较容易,可以得到这样的效果

Lets talk about how to make auto height support transition animation in CSS

然后通过上面的技巧将下拉内容隐藏起来,关键样式如下

.grid{
  display: grid;
  grid-template-rows: 0fr;
  transition: .3s;
  overflow: hidden;
}
.grid>div{
  min-height: 0;
}
Copy after login

然后通过hover触发显示,也就是改变grid-template-rows

.wrap:hover .grid{
  grid-template-rows: 1fr;
}
Copy after login

这样就实现了不定高度的过渡动画

Kapture 2023-01-31 at 20.02.28.gif

完整 demo可以查看以下任意链接

如果仅仅是悬浮窗口,由于是绝对定位,不会影响其他布局,其实是可以用transform scale 进行缩放的,再来看另外一个更加实用的例子,常见的菜单展开收起效果,就像这样

Kapture 2023-01-30 at 16.35.21.gif

可以看到,在展开的同时,下方的元素也被挤压下去了,这样效果更加自然,也是transform实现不了的,这里的切换是通过:checked实现的,关键代码如下

<input hidden type="checkbox" id="s1" checked />
<label for="s1">工作台</label>
<div class="sub">
  <ul>
    <li>项目列表</li>
    <li>数据配置器</li>
  </ul>
</div>
Copy after login
ul{
  min-height: 0;
}
.sub {
  display: grid;
  grid-template-rows: 0fr;
  transition: 0.3s;
  overflow: hidden;
}
:checked ~ .sub {
  grid-template-rows: 1fr;
}
Copy after login

完整 demo可以查看以下任意链接

四、注意事项和一些局限性

下面是一些注意事项。

这里的动画源于grid-template-rows的变化,也就是0fr1fr

**注意,注意,注意,**这里的0fr必须是0fr,不能是0或者0px,必须是fr单位

Lets talk about how to make auto height support transition animation in CSS

下面是改为40px的效果(动画丢失)

Kapture 2023-02-02 at 18.44.50.gif

再者,0fr也不支持calc计算,直接被认为不合法

Lets talk about how to make auto height support transition animation in CSS

这意味着,例如你希望默认有一个固定高度(非0),然后展开到自适应高度,这种方法是无法实现过渡动画的,略遗憾?

五、最后总结一下

最后再来回顾一下实现关键过程

.grid{
  display: grid;
  grid-template-rows: 0fr;
  transition: .3s;
  overflow: hidden;
}
.grid>div{
  min-height: 0;
}
.wrap:hover .grid{
  grid-template-rows: 1fr;
}
Copy after login

主要是利用了grid弹性布局可以实现过渡动画的特点,下面总结一些实现要点

  • 高度在设置成auto关键词时不会触发transition过渡动画

  • grid布局中的fr单位,可以用于定义网格轨道大小的弹性系数

  • grid布局的尺寸计算规则是由最小高度决定的,默认是min-content,也就是由内部文本决定的,可以通过手动设置min-height来实现0fr

  • grid布局也支持过渡动画(0fr=>1fr ),这样就实现高度不固定的过渡动画

  • 要使过渡动画生效,必须是fr单位,其他单位不行,也不能通过calc计算

  • 这种方法只能实现初始高度为0自适应高度的过渡变化,略微遗憾

(学习视频分享:web前端

The above is the detailed content of Let's talk about how to make auto height support transition animation in CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles