简体   繁体   中英

css3 background-size:cover leaving white space on resize

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

The background-size:cover works perfectly for the most part and resizing is generally not a problem, but in some cases it leaves a large white strip down at the bottom of the page on some resizes.
The background is a 1920x1080 image

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.

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.

Perhaps you can also try using:

background-size:100% 100%;

just a suggestion

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM