CSS背景图像适合高度,宽度应按比例自动缩放

时间:2013-11-20 10:48:45

标签: css css3

我希望背景图片与div的高度相符,有办法吗? background-size:cover;仅适用于自动缩放的宽度和高度(我需要相反的效果)。 感谢。

5 个答案:

答案 0 :(得分:85)

我知道这是一个古老的答案,但对于其他寻找此问题的人来说;在你的CSS尝试:

background-size: auto 100%;

答案 1 :(得分:35)

background-size: contain; 

适合我

答案 2 :(得分:14)

.something { 
    background: url(images/bg.jpg) no-repeat center center fixed; 
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: -100;  
}

答案 3 :(得分:7)

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>

答案 4 :(得分:4)

我遇到了同样的问题,这有助于我background-size:cover

上的body
html{
    height: auto;
    min-height: 100%;
}