响应式网页设计:屏幕宽度较小时如何实现文本居中对齐
解决响应式文本在小屏幕下的居中问题
你的问题出在:当前仅在屏幕宽度≤1201px时,给.index-first-text设置了align-items:center(让容器内的元素块居中),但文本内容默认是左对齐的,当屏幕宽度继续缩小到615px以下时,文本内容的左对齐会让视觉上看起来没有居中。
修改方案
只需给小屏幕下的文本元素添加text-align:center,让文本内容本身居中,同时确保容器的居中属性持续生效:
- 在现有的
@media screen and (max-width: 1201px)媒体查询中,补充文本对齐样式:
@media screen and (max-width: 1201px) { .navbar { flex-direction: column; align-items: center; padding: 10px; } .navbar h1 { margin-top: 10px; font-size: 30px; } .menu { flex-wrap: wrap; justify-content: center; margin-top: 30px; margin-left: 10px; } .menu li { padding: 10px 20px; } .index-first-text { align-items: center; margin: 50px; } /* 新增:让文本内容居中 */ .index-first-text p, .index-first-text h4 { text-align: center; } }
- (可选)如果要适配615px以下的极小屏幕,可以额外添加媒体查询,调整边距:
@media screen and (max-width: 615px) { .index-first-text { margin: 50px 20px; } }
修改后的完整代码
<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>MedConnect User | Home</title> <style media="screen"> @import url('https://fonts.googleapis.com/css2?family=Lora&display=swap'); body { background-color: #f8d4a4; font-family: Lora; } * { margin: 0; padding: 0; box-sizing: border-box; } a { text-decoration: none; } li { list-style: none; } .navbar { display: flex; align-items: center; justify-content: space-around; padding: 20px; margin-top: 50px; } .nav-links h3 { color: #704a1b; } .head { font-size: 32px; color: #614124; margin-top: -20px; margin-left: 50px; cursor: pointer; } .menu { display: flex; gap: 1em; font-size: 18px; margin-left: 280px; } .menu li { padding: 5px 12px; cursor: pointer; } .profile-btn { border-radius: 5px; box-shadow: 0 1px 1px black; padding: 10px 10px; margin-top: -10px; background-color: #60a159; } .profile-btn:active { box-shadow: none; } @media screen and (max-width: 1201px) { .navbar { flex-direction: column; align-items: center; padding: 10px; } .navbar h1 { margin-top: 10px; font-size: 30px; } .menu { flex-wrap: wrap; justify-content: center; margin-top: 30px; margin-left: 10px; } .menu li { padding: 10px 20px; } .index-first-text { align-items: center; margin: 50px; } .index-first-text p, .index-first-text h4 { text-align: center; } } .index-first p { color: #614124; font-size: 40px; } .index-first h4 { color: #704a1b; font-size: 20px; } .index-first-text { margin: 130px 120px; display: flex; flex-direction: column; gap: 2em; } .consult-today-btn { border-radius: 5px; box-shadow: 0 1px 1px black; padding: 25px 25px; background-color: #60a159; width: 176px; cursor: pointer; margin-top: 20px; } .consult-today-btn:active { box-shadow: none; } @media screen and (max-width: 790px) { .index-first-text p { font-size: 30px; } .index-first-text h4 { font-size: 15px; } } @media screen and (max-width: 615px) { .index-first-text { margin: 50px 20px; } } </style> </head> <body> <nav class="navbar"> <h1 class="head">MedConnect</h1> <ul class="nav-links"> <div class="menu"> <li><h3>Consult</h3></li> <li><h3>Resources</h3></li> <li><h3>About</h3></li> <li><h3>Contact</h3></li> <li><h3 class="profile-btn" style="color: white;">Your Profile</h3></li> </div> </ul> </nav> <div class="index-first"> <div class="index-first-text"> <p><b>Get Remote Medical Assistance here.</b></p> <h4>Find, Consult, Connect and Personalise your Needs Today.</h4> <h3 class="consult-today-btn" style="color: white;">Consult Today</h3> </div> </div> </body> </html>
内容的提问来源于stack exchange,提问作者user17999474
相关产品推荐
相关产品推荐

