HTML Body宽度在小屏设备上锁定问题求助
页面宽度在小屏设备无法适配的问题解决
问题
个人作品集网站某页面在屏幕宽度低于940px时,页面宽度被锁定,无法随屏幕尺寸缩小,其他页面响应式表现正常。
相关代码
CSS
html { scroll-behavior: smooth; } body { display: flex; flex-direction: column; align-items: center; background-color: rgba(210, 208, 208, 0.633); overflow-x: hidden; height: auto; width: auto; } .conteudo { display: flex; flex-direction: column; align-items: center; width: 45svb; height: auto; margin: 30px 30px; } .botao_voltar { display: flex; flex-direction: inherit; position: fixed; z-index: 30; align-items: flex-start; height: 60px; justify-content: center; width: 45vmax; padding: 20px 20px; margin-top: 30px; background: rgba(172, 170, 170, 0.452); border-radius: 80px; box-shadow: 0 4px 30px rgba(0, 0, 0, 0.37); backdrop-filter: blur(17.5px); } .botao_voltar button { border: 0; border-radius: 35px; background-color: #FF5F1F; color: white; font-family: 'Poppins', sans-serif; width: 110px; height: 60px; font-weight: 700; font-size: 30px; padding: 10px 10px; transition: all .7s ease; } .botao_voltar button:hover { transform: translate3D(0,-1px,0) scale(1.20); box-shadow: 8px 28px 50px rgba(39,44,49,.07), 1px 6px 12px rgba(39,44,49,.04); transition: all .7s ease; cursor: pointer; } .corpo { display: flex; position: relative; flex-direction: column; width: 40vmax; padding: 50px 50px; margin-top: 120px; background: #FF5F1F; border-radius: 80px; box-shadow: 0 4px 30px rgba(0, 0, 0, 0.37); backdrop-filter: blur(17.5px); } .corpo h1 { text-align: start; font-family: monospace; font-size: 60px; font-weight: 700; color: rgb(252, 248, 248); text-wrap: balance; } .corpo img { margin-top: 50px; width: inherit; border-radius: 60px; object-fit: cover; box-shadow: 0 4px 30px rgba(0, 0, 0, 0.37); backdrop-filter: blur(17.5px); } .corpo p { text-align: justify; font-family: 'Corporate'; font-size: 25px; line-height: 3.5rem; word-spacing: 0.5px; color: rgb(252, 248, 248); text-wrap: balance; } .copyright { display: flex; align-items: flex-end; font-size: 20px; font-family: "Corporate"; color: black; }
HTML
<div class="botao_voltar"> <a href="/Projetos/index.html"><button>Voltar</button></a> </div> <div class="conteudo"> <div class="corpo"> <h1>Reconhecimento Facial Usando CNN</h1> <img src="https://picsum.photos/300"> <p> Criei este projeto simples meramente por questões didáticas já que estava estudando CNN (Convulational Neural Networks). Basicamente eu montei um modelo do zero, não utilizei nenhuma arquitetura pronta, como RES-NET ou ALEX-NET, ao invés montei eu mesmo, de forma bem amadora mas para simplesmente entender a lógica por trás do CNN. </p> </div> </div> <p class="copyright"> © 2024 Luiz Fernando Brasão</p>
问题原因
vmax单位误用:botao_voltar和corpo的宽度使用vmax,该单位取视口宽/高中的较大值。手机竖屏时视口高度大于宽度,导致元素宽度基于高度计算,远超屏幕宽度,撑开页面。svb单位兼容性问题:conteudo使用的svb是较新的CSS单位,部分浏览器对其支持不完善,计算逻辑易在小屏上导致宽度异常。- 固定像素尺寸溢出:标题、按钮的固定像素字体大小,在小屏上易超出容器,间接导致页面宽度被锁定。
解决方案
1. 替换问题单位,改用适配性更好的相对单位
将vmax换成vw(基于视口宽度),svb换成百分比,并添加最大宽度限制:
.conteudo { display: flex; flex-direction: column; align-items: center; width: 90%; max-width: 1200px; height: auto; margin: 30px auto; } .botao_voltar { display: flex; flex-direction: inherit; position: fixed; z-index: 30; align-items: flex-start; height: 60px; justify-content: center; width: 90vw; max-width: 800px; padding: 20px 20px; margin-top: 30px; left: 50%; transform: translateX(-50%); background: rgba(172, 170, 170, 0.452); border-radius: 80px; box-shadow: 0 4px 30px rgba(0, 0, 0, 0.37); backdrop-filter: blur(17.5px); } .corpo { display: flex; position: relative; flex-direction: column; width: 100%; max-width: 800px; padding: 30px 25px; margin-top: 120px; background: #FF5F1F; border-radius: 80px; box-shadow: 0 4px 30px rgba(0, 0, 0, 0.37); backdrop-filter: blur(17.5px); }
2. 实现响应式字体大小
用clamp()函数让字体随屏幕宽度自动缩放,避免溢出:
.corpo h1 { text-align: start; font-family: monospace; font-size: clamp(2rem, 5vw, 4rem); font-weight: 700; color: rgb(252, 248, 248); text-wrap: balance; } .corpo p { text-align: justify; font-family: 'Corporate'; font-size: clamp(1rem, 2.5vw, 1.5rem); line-height: clamp(1.8rem, 4vw, 3.5rem); word-spacing: 0.5px; color: rgb(252, 248, 248); text-wrap: balance; } .botao_voltar button { border: 0; border-radius: 35px; background-color: #FF5F1F; color: white; font-family: 'Poppins', sans-serif; width: clamp(80px, 15vw, 110px); height: 60px; font-weight: 700; font-size: clamp(1.2rem, 3vw, 2rem); padding: 10px 10px; transition: all .7s ease; }
3. 优化全局样式
确保body不会被溢出内容撑开,版权文本适配小屏:
body { display: flex; flex-direction: column; align-items: center; background-color: rgba(210, 208, 208, 0.633); overflow-x: hidden; height: auto; width: 100%; padding: 0 10px; box-sizing: border-box; } .copyright { display: flex; align-items: flex-end; font-size: clamp(0.8rem, 2vw, 1.2rem); font-family: "Corporate"; color: black; margin: 20px 0; }
内容的提问来源于stack exchange,提问作者N1ne
相关产品推荐
相关产品推荐

