作者头像边框未匹配正确图片高度问题排查
作者头像尺寸异常问题排查
我想给作者信息栏里的头像设置72x72的尺寸并添加边框,预期效果是这样的:
但实际渲染后,头像变成了72x24的窄条,效果如下:
浏览器检查器显示的头像尺寸也不符合设置:
我的代码如下:
CSS代码
.block { position: relative; background: #ffffff; border-radius: 4px; padding: 32px; display: grid; grid-row-gap: 24px; grid-column-gap: 24px; } #author { background: #ffffff; grid-template-rows: 1fr; grid-template-columns: 72px 1fr; margin-bottom: 24px; box-shadow: 0px 0px 96px rgba(247, 237, 226, 0.24); } .authorPhoto { width: 72px; height: 72px; vertical-align: top; } .profilePicture { width: 72px; height: 72px; }
HTML代码
<style></style> <div class="block" id="author"> <div class="authorPhoto"> <img src="#" class="profilePicture"/> </div> <div> <h2>#</h2> <p>#</p> </div> </div> --- # 问题原因及修复方案 ## 问题出在哪 1. **Grid行高计算优先级更高**:你给`#author`用了Grid布局,设置`grid-template-rows:1fr`,但Grid容器的行高会优先根据内容自动计算,你给`.authorPhoto`设的`height:72px`会被覆盖。 2. **空图片占位导致高度压缩**:`img`的`src="#"`是无效地址,图片加载失败后会变成默认的空占位元素,高度只有24px左右,这直接把`.authorPhoto`的高度拉低成了和空图片一样的尺寸。 3. **缺少边框样式**:你没给头像添加边框的CSS属性,就算尺寸对了也不会显示边框。 ## 怎么修复 1. **替换有效图片地址**:把`img`的`src="#"`换成真实的头像URL,让图片正常加载,这样图片会撑满你设置的72x72尺寸。 2. **强制Grid行高为固定值**:修改`#author`的`grid-template-rows`,把`1fr`改成`72px`,确保Grid第一行的高度固定,不受内容影响: ```css #author { background: #ffffff; grid-template-rows: 72px; /* 替换原来的1fr */ grid-template-columns: 72px 1fr; margin-bottom: 24px; box-shadow: 0px 0px 96px rgba(247, 237, 226, 0.24); }
- 添加边框样式:如果需要给头像加边框,给
.profilePicture添加上边框属性,比如:
.profilePicture { width: 72px; height: 72px; border: 2px solid #e0e0e0; /* 自定义边框样式 */ border-radius: 4px; /* 可选:圆角效果 */ }
内容的提问来源于stack exchange,提问作者Nick Groeneveld
相关产品推荐
相关产品推荐

