根据答案,对于任何有兴趣的人来说,要按比例缩放 CSS 背景图像与文本换行,以下应该在大多数情况下都有效。
看小提琴
首先,为了确保背景图像的渲染不会大于移动分辨率,我添加了一个视口
@media screen and (max-width: 800px){
.viewport {
position:absolute;
right:0;
left:0;
top:0;
bottom:0;
overflow-x:hidden;
overflow-y:scroll;
}
}
<html>
<body>
<div class="viewport">
<!-- background image -->
</div>
</body>
<html>
然后我在背景元素上background-size:cover;
使用了高度为 的属性,auto
以允许它在移动设备上随文本换行一起流动。为了允许背景元素的高度相对于文本换行重新调整大小,其父元素(在本例中为body
)的高度必须为100%
.
html,body{
height:100%;
}
.banner{
background: url('path/to/banner.jpg') center center no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
@media screen and (max-width: 800px){
.viewport {
position:absolute;
right:0;
left:0;
top:0;
bottom:0;
overflow-x:hidden;
overflow-y:scroll;
}
.banner{
height:auto;
}
.banner .banner-container {
position:relative;
overflow:hidden;
}
}
<html>
<body>
<div class="viewport">
<div class="banner">
<div class="banner-container">
<h1>Lorem ipsum dolor sit amet, est intellegebat definitionem at</h1>
<h1>Lorem ipsum dolor sit amet, est intellegebat definitionem at</h1>
<h1>Lorem ipsum dolor sit amet, est intellegebat definitionem at</h1>
</div>
</div>
</div>
</body>
<html>