解决页眉深绿色通知条内文本与图片垂直对齐异常问题
顶部通知条图文错位问题修复方案
问题根源
- 给链接添加的
vertical-align: super属性,会将文字强制设置为上标位置,直接破坏正常排版的对齐基准 - 作为行内元素的
<img>默认按文字基线对齐,没有统一对齐规则时,和相邻文字必然出现高低错位 - 包裹文字的
<p>标签保留了浏览器默认的上下边距,且设置的margin-bottom:auto在父元素无固定高度的场景下属于无效样式,无法实现真正的垂直居中 - 右侧关闭按钮使用
float定位,没有和左侧内容建立统一的垂直对齐参照,容易挤压内容位置导致排版错位
修正代码
替换原有全局CSS
@media screen and (max-width: 425px){ #message{font-size:10px;} .shippingimage{display:none;} } .ShowHide { display: flex; align-items: center; justify-content: center; min-height: 40px; padding: 6px 40px 6px 16px; overflow: hidden; background-color: #2a4735; color: white; position: relative; } .ShowHide p { margin: 0; } .shippingimage { vertical-align: middle; margin-right: 6px; } #right-showing { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); width: 30px; text-align: center; } .ShowHide a { color: white; text-decoration: none; } .ShowHide a:hover { text-decoration:underline; }
同步修正HTML/JS内的冗余样式
- 删掉p标签上原来写的
style="margin-bottom:auto;text-align:center;"行内样式 - 删掉通知消息数组里所有
<a>标签上自带的vertical-align: super行内样式
效果说明
- 依靠flex布局的
align-items: center属性,整个深绿色条带内的所有内容会自动实现垂直居中 - 图片通过
vertical-align: middle和相邻文字做中线对齐,不会出现高低错位 - 右侧关闭按钮通过绝对定位+垂直偏移的方式固定在条带右侧,不会挤压中间的通知内容宽度
- 移除了原来导致文字上偏的上标对齐规则,所有文字和图片的排版基准统一
内容的提问来源于stack exchange,提问作者MorganPubblicita
相关产品推荐
相关产品推荐

