【发布时间】:2021-07-08 16:16:02
【问题描述】:
由于某种原因,在移动设备上滚动页面时,我的带有背景图像的 div 会被放大。我用谷歌搜索,似乎其他人也有这个问题,但我没有找到令人满意的解决方案,如果有的话。但是,我发现了这个:https://www.w3schools.com/howto/tryhow_css_parallax_demo.htm(来自这里:https://www.w3schools.com/howto/howto_css_parallax.asp),它工作正常。那么 w3schools 有什么不同呢?我检查了他们的代码,但没有发现任何影响。
(仅供参考,即使没有“背景附件:固定;”的视差效果,甚至没有高度的 vh 单位,我仍然遇到问题。
这是我的代码:
HTML:
<div class="landingContainer">
<h1>Welcome to Restaurant</h1>
<h2>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</h2>
</div>
CSS:
.landingContainer {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-image: linear-gradient(rgba(0,0,0,0.4),rgba(0,0,0,0.4)),
url('/src/components/Home/homeImages/pexels-emre-can-acer-2079438.jpg');
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
width: 100%;
min-height: var(--vh100);
padding: 1rem;
color: white;
text-align: center;
overflow-wrap: anywhere;
}
.landingContainer h1 {
font-size: clamp(2.2rem, 2.0058rem + 1.2427vw, 3rem);
/* https://css-tricks.com/linearly-scale-font-size-with-css-clamp-based-on-the-viewport/#for-those-who-dont-mind-that-edge-case */
font-weight: 600;
margin: 1em 0 .5em;
color: white;
}
.landingContainer h2 {
font-size: clamp(1rem, 0.9892rem + 0.0693vw, 1.1rem);
font-weight: 300;
width: 85%;
max-width: 45ch;
line-height: 1.6;
}
谢谢!
更新:看这里的视频:https://imgur.com/a/ZFWgdqr
【问题讨论】:
-
var(--vh100)从何而来,它的价值是什么? -
我想我理解这个问题。当地址栏 (dis) 出现时,显示大小会发生变化。当显示尺寸改变时,背景封面会重新计算缩放。可能没有一个简单的解决方案,因为您可能希望它在不同尺寸的设备上按需要工作。可能是在加载时修复背景大小的脚本。或者使用设备宽度作为指标。
标签: html css responsive-design