如何实现卡片Hover展开时悬浮于其他卡片上方而非向下挤压
卡片网格hover展开覆盖下方元素的解决方案
要解决卡片hover展开时推挤下方卡片的问题,只需通过调整定位属性让展开内容脱离文档流,并提升hover卡片的层级,具体修改如下:
核心修改
- 给
.card添加position: relative,为子元素提供定位参照;hover时设置z-index: 10,确保卡片处于上层 - 将
.description设置为position: absolute,固定在卡片底部,使其展开时不占用网格布局空间
修改后的完整CSS代码
.grid { display: grid; width: 60%; margin: 0 auto; grid-template-columns: 1fr 1fr 1fr; gap: 15px; } .card { transition: all 0.3s linear; position: relative; } .card:hover { transform: scale(1.04); z-index: 10; } .card:hover .description { max-height: 100px; } .image { aspect-ratio: 1.6; background-color: #516d9b; display: flex; justify-content: center; align-items: center; } .title { display: flex; justify-content: center; align-items: center; height: 100px; background-color: #3f8539; } .description { transition: all 0.3s linear; display: flex; justify-content: center; align-items: center; max-height: 0; overflow: hidden; height: 100px; background-color: #c99753; padding-left: 25px; position: absolute; bottom: 0; left: 0; right: 0; }
效果说明
修改后,hover时展开的描述区域会固定在卡片底部,不会增加卡片整体高度,因此不会推挤下方网格元素;同时z-index属性确保当前卡片覆盖在其他卡片上方,实现悬浮覆盖的预期效果。
内容的提问来源于stack exchange,提问作者Alvaro Bataller
相关产品推荐
相关产品推荐

