Stretch and scale a CSS image in the background - with CSS only

后端 未结 22 2198
迷失自我
迷失自我 2020-11-22 01:37

I want that my background image stretch and scale depending on the browser viewport size.

I\'ve seen some questions on Stack Overflow that do the job, like

22条回答
  •  迷失自我
    2020-11-22 01:47

    I wanted to center and scale a background image, without stretching it to the entire page, and I wanted the aspect ratio to be maintained. This worked for me, thanks to the variations suggested in other answers:

    INLINE IMAGE: ------------------------

    CSS ----------------------------------

    html {
        height:100%;
    }
    
    #background {
        text-align: center;
        width: 100%; 
        height: 100%; 
        position: fixed;
        left: 0px; 
        top: 0px; 
        z-index: -1;
    }
    
    .stretch {
        margin: auto;
        height:100%;
    }
    

提交回复
热议问题