Stretch and scale CSS background

前端 未结 16 2291
难免孤独
难免孤独 2020-11-22 02:28

Is there a way to get a background in CSS to stretch or scale to fill its container?

16条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 02:58

    This is what I've made of it. In the stretch class, I simply changed the height to auto. This way your background picture has always got the same size as the width of the screen and the height will allways have the right size.

    #background {
        width: 100%;
        height: 100%;
        position: absolute;
        margin-left: 0px;
        margin-top: 0px;
        z-index: 0;
    }
    
    .stretch {
        width:100%;
        height:auto;
    }
    

提交回复
热议问题