Search code examples
htmlcssbackground-image

How to keep background image from moving when a scroll is added?


I have a website with two pages. Both pages share the same CSS body properties.

When you click the button I have on the home page, it will bring you to a page with a scroll bar. When this page is displayed, the background shrinks/moves to the left just a tiny bit to accommodate for the scroll bar appearing. How can I stop this behavior?

I would like for the background image to be totally static. Here's a small snippet of the CSS.

html {
    min-height: 100%;
    cursor: url('Pictures/glove-lg.png'), auto;
}

body {
    display: flex;
    justify-content: center;
    font-family: 'Lora', serif;
    margin: .8em;
    background-color: #151b20;
    background-image: linear-gradient(rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.1)), url(Pictures/backgroundDala.jpg);
    background-size: cover;
    background-position: center;
    background-attachment: fixed;
    background-repeat: no-repeat;
}

Solution

  • The only way to avoid that is to use overflow-y: scroll for the body also on those pages whose contents is equal to or not more than 100% high.

    That way a vertical scollbar will be displayed on each page and therefore the background will remain the same. Not a pretty solution, but the only one that keeps the background-image consistent.