【发布时间】:2013-12-04 07:41:40
【问题描述】:
我希望背景图片适合 div 的高度,有办法吗? background-size:cover; 只适合宽度和高度自动缩放(我需要相反的效果)。
谢谢。
【问题讨论】:
-
试试 background-size: 1% 100%;
标签: css
我希望背景图片适合 div 的高度,有办法吗? background-size:cover; 只适合宽度和高度自动缩放(我需要相反的效果)。
谢谢。
【问题讨论】:
标签: css
background-size: contain;
适合我
【讨论】:
background-size: auto 100%;)
试试
.something {
background: url(images/bg.jpg) no-repeat center center fixed;
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
z-index: -100;
}
【讨论】:
我知道这是一个旧答案,但对于其他正在寻找这个的人来说;在你的 CSS 中尝试:
background-size: auto 100%;
【讨论】:
min-height: 100% 来处理“比背景高”的情况。
background-size: auto auto;,这样无论哪个轴更小(宽度或高度),它都会保持在那个范围内。否则在调整页面大小时它会被剪裁。
body.bg {
background-size: cover;
background-repeat: no-repeat;
min-height: 100vh;
background: white url(../images/bg-404.jpg) center center no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
}
Try This
body.bg {
background-size: cover;
background-repeat: no-repeat;
min-height: 100vh;
background: white url(http://lorempixel.com/output/city-q-c-1920-1080-7.jpg) center center no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
}
<body class="bg">
</body>
【讨论】:
我刚刚遇到了同样的问题,这对我有帮助:
html {
height: auto;
min-height: 100%;
background-size:cover;
}
【讨论】:
height: auto; 甚至不需要,只需使用background-size: cover; min-height: 100%)。
height 不能像我一直在做的那样使用,但无论如何,谢谢。作为记录,我发现在 HTML 元素本身上设置 background-size:cover 很好。