【发布时间】:2014-01-29 21:17:31
【问题描述】:
我想做一个单页网站,所以你必须向下滚动才能查看内容。我有 3 个不同的内容 div。每个 div 应该有一个不同的背景图像,它应该根据浏览器的大小进行缩放,同时保持图像的纵横比。 希望它有点清楚我想做什么?
这是我目前拥有的 css:
.div1 {
height: 927px;
width: 100%;
background-image: url(bg3.jpg);
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
}
.div2 {
height: 927px;
width: 100%;
background-image: url(bg1.jpg);
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
}
.div3 {
height: 927px;
width: 100%;
background-image: url(bg2.jpg);
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
这是html:
<body>
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
</body>
如您所见,它不会将每个 div 背景图像缩放到浏览器的正确大小。任何人有一个很好的解决方案来做到这一点?
【问题讨论】:
-
这只对新浏览器有帮助:css-tricks.com/perfect-full-page-background-image 或者看看buildinternet.com/project/supersized(它不像听起来那么容易,你需要 javascript 来保持旧浏览器的比例!)
-
感谢您的评论,但您的链接是关于整个页面的全尺寸背景图片。我想在一页上为 3 个不同的 div 提供 3 个不同的背景图像。
标签: css image html background