为何480px×859px浏览器窗口中h1与p标签内容溢出?
问题描述
在480px×859px的浏览器窗口中,h1标题与p段落内容出现溢出,其他设备显示正常。推测是给h1和p设置了固定宽度导致的——之前想用text-align匹配设计稿的文本对齐和每行字数效果没成功,设固定宽度后实现了设计要求,但没法适配所有设备。

现有代码
CSS代码
* { box-sizing: border-box; padding: 0; margin: 0; } html { font-size: 15px; } /*Card Styles*/ body { background-color: hsl(233, 47%, 7%); font-family: 'Inter', sans-serif; font-weight: 400; } main { width: 80%; max-width: 900px; margin: 1.5em; background-color: hsl(244, 38%, 16%); display: grid; grid-template-columns: 1fr 1fr; margin: 8.5em auto; border-radius: 6px; } .first { background-image: url('images/image-header-desktop.jpg'); background-color: hsl(277, 64%, 61%); background-blend-mode: multiply; order: 1; } .second { padding: 3em; } h1 { color: hsl(0, 0%, 100%); width: 350px; } h1>span { color: hsl(277, 64%, 61%); } p { color: hsla(0, 0%, 100%, 0.75); margin-top: 1em; width: 400px; font-family: 'Lexend Deca', sans-serif; font-weight: 400; line-height: 1.4; } dl { margin-top: 2em; display: flex; justify-content: space-between; } dl dt { color: hsl(0, 0%, 100%); font-weight: 700; font-size: 1.25rem; } dl dd { font-variant-caps: all-petite-caps; color: hsla(0, 0%, 100%, 0.6); } /*media query styles */ @media(max-width:480px) { body { min-height: 100vh; padding: 2em 1em; } main { width: 100%; grid-template-rows: 250px 1fr; /*height property cannot be used to adjust height of grid items only grid template rows*/ grid-template-columns: auto; margin: 3em auto; overflow: hidden; } .first { background-image: url('images/image-header-mobile.jpg'); background-color: hsl(277, 64%, 61%); background-blend-mode: multiply; background-size: cover; order: -1; } .second { padding: 1.5em; display: grid; grid-template-rows: 1fr 1fr 2fr; } h1 { width: 300px; text-align: center; margin: 0 auto; } p { width: 245px; text-align: center; line-height: 1.6; margin: 0 auto; } dl { flex-direction: column; gap: 1.5em; justify-content: center; align-items: center; } dl div { width: 71px; /*giving all flex items equal width to make them all align*/ } }
HTML代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- displays site properly based on user's device --> <link rel="icon" type="image/png" sizes="32x32" href="./images/favicon-32x32.png"> <link rel="stylesheet" href="styles.css"> <!--fonts styles--> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&family=Lexend+Deca&display=swap" rel="stylesheet"> <title>Frontend Mentor | Stats preview card component</title> </head> <body> <main class="container"> <div class="first"> <!--container for background image--> </div> <div class="second"> <h1>Get <span>insights</span> that help your business grow.</h1> <p>Discover the benefits of data analytics and make better decisions regarding revenue, customer experience, and overall efficiency.</p> <div class="stats"> <dl> <div> <dt>10k+</dt> <dd>companies</dd> </div> <div> <dt>314</dt> <dd>templates</dd> </div> <div> <dt>12M+</dt> <dd>queries</dd> </div> </dl> </div> </div> </main> </body> </html>
解决方案
你的推测完全正确,固定宽度是溢出的核心原因——在480px宽的窗口中,.second容器的实际可用宽度会被内边距压缩,硬设置的固定宽度会超出容器范围导致溢出。可以用相对宽度+最大宽度的组合,既满足设计稿的每行字数要求,又能自适应不同设备:
1. 修改桌面端h1和p的样式
h1 { color: hsl(0, 0%, 100%); width: 100%; max-width: 350px; } p { color: hsla(0, 0%, 100%, 0.75); margin-top: 1em; width: 100%; max-width: 400px; font-family: 'Lexend Deca', sans-serif; font-weight: 400; line-height: 1.4; }
2. 修改移动端媒体查询内的h1和p样式
h1 { width: 100%; max-width: 300px; text-align: center; margin: 0 auto; } p { width: 100%; max-width: 245px; text-align: center; line-height: 1.6; margin: 0 auto; }
3. 优化.second的grid布局(可选)
去掉固定的行高设置,用间距控制元素位置,避免内容溢出容器:
.second { padding: 1.5em; display: grid; gap: 1.5em; }
这样设置后,元素会在容器宽度允许的情况下自适应缩放,当容器宽度小于最大限制时自动占满容器,不会出现溢出;当容器宽度足够时,保持设计要求的最大宽度,匹配每行字数效果。
内容的提问来源于stack exchange,提问作者Adebimpe Abdulhamid Eniola
相关产品推荐
相关产品推荐

