[CSS] Use CSS Grid to Animate Elements with Dynamic Height
Currently we are animating to a fixed height of 100px on hover since browsers can't animate from 0 to auto
:
<p class="h-0 overflow-hidden text-white/70 transition-all group-hover:h-[100px]">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minima quia ipsa
eius.
</p>
Luckily there's a handy trick we can use to get around this limitation!
wrap the paragraph with a div
containing a class of grid
, and set the grid row to 0fr
. Then we can then animate the grid row to 1fr
on hover and the transition-all
class from the paragraph to the new grid
div:
<div class="grid grid-rows-[0fr] transition-all group-hover:grid-rows-[1fr]">
<p class="mt-2 overflow-hidden text-white/70">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minima quia ipsa
eius.
</p>
</div>
Using the fractional unit fr
for the sizing is the trick that makes this work. It's a flexible unit of measurement for CSS Grid that distributes the available space evenly among the rows and columns.
In our case, setting the grid row to 0fr
will hide the paragraph since it's a zero height row. Then, when we hover over the card, we change the grid row to 1fr
, which will reveal the paragraph and have it take up all available space.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2021-06-04 [AWS - DA] Cognito
2021-06-04 [AWS -DA] SAM & CDK
2021-06-04 [AWS - DA] Lambda - PART 2
2021-06-04 [AWS - DA] VPC
2020-06-04 [Python] Putting Code on PyPi
2016-06-04 [Redux] Navigating with React Router <Link>
2016-06-04 [Redux] Adding React Router to the Project