求助:如何用CSS实现移动端文本下划线宽度统一?
统一联系信息下划线宽度的解决办法
需求:让「Press」「Phone」下方的下划线宽度和「Questions」的保持一致,所有文本下划线宽度统一,之前调整宽度没效果。

问题原因
当前每个联系信息块(.questions-info/.press-info/.phone-info)的宽度由内部文本决定,p标签的border-bottom会随文本长度变化,导致下划线宽度不一致。
解决方案
给每个信息块设置统一的固定宽度,并让内部文本居中,这样p标签的下划线就会和容器宽度保持一致。
修改后的代码
CSS 代码
.contact-banner { border-top-right-radius: 40px; border-top-left-radius: 40px; margin-top: 10%; } .contacts-info { display: flex; justify-content: space-around; margin-top: 2%; align-items: center; } /* 给每个信息块设置统一宽度,文本居中 */ .questions-info, .press-info, .phone-info { width: 220px; text-align: center; } h1 { display: flex; justify-content: center; font-family: var(--elewa-group-website-dmsans-regular); font-size: 50px; font-weight: 550; } p { margin: 5% 0; padding-bottom: 2%; border-bottom: 1px solid rgb(180, 175, 175); } h2 { font-family: var(--elewa-group-website-dmsans-regular); font-size: 13px; opacity: .4; } p { font-family: var(--elewa-group-website-dmsans-regular); font-size: 27px; } @media (max-width: 767px) { .banner-main { margin-top: 5%; } .contacts-info { flex-direction: column; align-items: center; } .questions-info, .press-info, .phone-info { margin: 5% 0; width: 100%; /* 移动端宽度铺满 */ } .press-info, .phone-info { padding-top: 2%; } h1 { font-size: 35px; } h2 { font-size: 18px; text-align: center; } p { font-size: 20px; margin: 5% 0; padding-bottom: 2%; border-bottom: 1px solid rgb(180, 175, 175); box-sizing: border-box; } } @media (max-width: 480px) { .banner-main { margin-top: 4%; } h1 { font-size: 30px; } h2 { font-size: 14px; } p { font-size: 25px; margin: 5% 0; padding-bottom: 2%; border-bottom: 1px solid rgb(180, 175, 175); box-sizing: border-box; } }
HTML 代码
<div class="contact-banner"> <h1>Contact</h1> <div class="contacts-info"> <div class="questions-info"> <h2 class="subheader">Questions</h2> <p>Info@elewagroup.com</p> </div> <div class="press-info"> <h2 class="subheader">Press</h2> <p>Press@elewa.com</p> </div> <div class="phone-info"> <h2 class="subheader">Phone</h2> <p>+254 78 92 27 755</p> </div> </div> </div>
关键修改说明
- 新增
.questions-info, .press-info, .phone-info的样式,设置固定宽度220px(可根据需求调整),并让文本居中,确保每个块的宽度一致 - 移动端适配时,把这些块的宽度设为
100%,保证在小屏幕上的显示效果
内容的提问来源于stack exchange,提问作者someone
相关产品推荐
相关产品推荐

