css3 background-size:cover leaving white space on resize

后端 未结 4 971
抹茶落季
抹茶落季 2021-01-07 01:12
body {
    margin:0;
    padding:0;
    position:relative;
    background:url(../images/imgs/backgrnd.png) no-repeat;
    background-size:cover;
}

相关标签:
4条回答
  • 2021-01-07 01:56

    Not sure if you solved this problem, but I was tearing my hair out over it for a couple of days and nothing I could Google was helping. I talked to someone at work on our UI team and it turns out that the body of the web page won't necessarily cover the whole height of the page. Adding the following to my css file completely solved the problem:

    html {
        height: 100%;
    }
    
    body {
        background-size: cover;
        height: 100%;
    }
    

    This stretches the content of the body to fit the whole height of the window. Just including the "body" bit worked for me, but my coworker informed me that it's better to include both because some browsers will act funny with just one.

    0 讨论(0)
  • 2021-01-07 02:00

    Add an overflow:auto; to resolve this issue.

    For Instance,

    body {
        margin:0;
        padding:0;
        position:relative;
        background:url(../images/imgs/backgrnd.png) no-repeat;
        background-size:cover;
        overflow:auto;
    }
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-07 02:07

    Perhaps you can also try using:

    background-size:100% 100%;
    

    just a suggestion

    0 讨论(0)
  • 2021-01-07 02:10

    It's 2021 and I encountered a similar problem and none of the solutions worked for me.

    I managed to figure it out and hope this will help someone.

    You will need to set the viewport height and width to 100%. According to w3schools, The viewport is the user's visible area of a web page. This will eliminate any white spaces by setting the minimum height to that.

    height: 100vh;
    width: 100vw;
    
    0 讨论(0)
提交回复
热议问题