CSS Grid列等高实现:左侧图片与右侧内容高度一致问题
问题:响应式弹窗左侧图片无法匹配右侧内容高度
我需要实现左侧列图片与右侧列内容高度一致,打造一个高度由右侧内容决定的响应式弹窗,左侧图片需匹配这个高度,请问我遗漏了什么配置?
当前代码
<div class="grid grid-cols-1 items-center bg-purewhite rounded-xl shadow authentication-popup lg:grid-cols-auth-popup lg:max-w-[55rem] lg:w-max svelte-q5aaad opened" style="grid-template-rows: auto !important;"> <div class="w-full h-full relative shrink-0 lg:rounded-l-xl lg:w-fit"> <img class="w-full h-[200px] object-authentication object-cover rounded-t-xl lg:w-[380px] lg:h-full lg:rounded-l-xl lg:rounded-tr-none" style="/*! height: 100% !important; */" src="https://plus.unsplash.com/premium_photo-1677942035529-db39d2a25915?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=928&q=80"> </div> <div class="flex flex-col grow p-6"> <div class="flex items-center gap-1.5 justify-between"> <p class="text-primary font-bold text-3xl lg:whitespace-nowrap">Please Sign In</p> <i class="close-pm-delete-confirm-popup fa-solid fa-xmark fa-xl text-secondary cursor-pointer"></i> </div> </div> </div>
当前效果

解决方案
问题出在Grid容器的对齐设置和图片的固定高度限制上,调整点如下:
- 移除Grid容器的
items-center类(或替换为items-stretch),Grid默认列会拉伸匹配容器高度,items-center会强制垂直居中,导致左侧列高度不跟随右侧内容拉伸。 - 删除Grid容器上的
style="grid-template-rows: auto !important;",这个强制设置会限制行高自适应,让Grid无法根据右侧内容动态调整整体高度。 - 修改图片的类,去掉固定高度
h-[200px],换成h-full,确保图片能撑满父容器的高度,同时保留object-cover保证图片比例正确、不拉伸变形。
调整后的核心代码片段:
<div class="grid grid-cols-1 bg-purewhite rounded-xl shadow authentication-popup lg:grid-cols-auth-popup lg:max-w-[55rem] lg:w-max svelte-q5aaad opened"> <div class="w-full h-full relative shrink-0 lg:rounded-l-xl lg:w-fit"> <img class="w-full h-full object-authentication object-cover rounded-t-xl lg:w-[380px] lg:rounded-l-xl lg:rounded-tr-none" src="https://plus.unsplash.com/premium_photo-1677942035529-db39d2a25915?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=928&q=80"> </div> <!-- 右侧内容容器保持不变 --> </div>
内容的提问来源于stack exchange,提问作者rediz
相关产品推荐
相关产品推荐

