The moving background is not remaining still

Hi everyone, I'm currently in the process of creating a new background design for my website and I want to add some animation to it. You can view my progress here:

One issue I am facing is that the "background" div does not remain fixed when scrolling down the page. I have tried various methods but can't seem to make it work.

This is what my CSS code looks like:

body {
    background-color: #0a0a0a;
    color: #a2a2a2;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    line-height: 1.4;
    margin: 0;
    padding: 0;
    background-image: url(http://wow-x.com/board/images/blackevo4-pure/background.jpg);
    background-repeat: no-repeat;
    background-position: center top;
    background-attachment:fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

#background {
    background: url(http://wow-x.com/board/images/blackevo4-pure/back1.jpg);
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    min-width: 100%;
    min-height: 100%;
    opacity:0.1;
    z-index:-1;
}

#midground {
    background: url(http://wow-x.com/board/images/blackevo4-pure/midground.png) repeat 20% 20%;
    background-attachment:fixed;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index:-2;
    height:100%;
    width:100%;
}

#foreground {
    background: url(http://wow-x.com/board/images/blackevo4-pure/foreground.png) repeat 90% 110%;
    background-attachment:fixed;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index:-3;
    height:100%;
    width:100%;
}

Answer №1

To ensure the #background div remains fixed, switch its positioning from position: absolute; to position: fixed;

When the position property is set to absolute, the element will be placed in relation to its closest ancestor with a non-static positioning. In this scenario, the body itself serves as that ancestor, causing the image to move along with the page scroll.

By setting the position property to fixed, the element will be positioned relative to the browser window instead. This means its location will not change when scrolling up or down.

Answer №2

Quick solution:

Add the following CSS rule to your #background selector:

position: fixed;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with the functionality of Angular 2 routerLink

Unable to navigate from the homepage to the login page by clicking on <a routerLink="/login">Login</a>. Despite reviewing tutorials and developer guides on Angular 2 website. Current code snippet: index.html: <html> <head> ...

Items vanish when hovering over them

Creating nested links in the sidebar navigation bar with CSS has been a challenge for me. While hovering over main links, I can see the sub-links being displayed. However, when I try to hover/click on the children of the sub-links, they disappear. Note: M ...

Error: Attempting to access property 'PRM_ServerError' on an undefined object is not permitted

After deploying my code to the QA server for testing, I discovered that my image button was not triggering an event. This issue did not occur on the development server. To investigate further, I compared the console output between my local environment and ...

I am experiencing an issue where my jQuery navigation bar is not displaying correctly on IE

I've been wracking my brain trying to figure out this problem with my navbar. It seems pretty straightforward but for some reason I can't get it to work properly. The issue is that I have a jquery-powered navbar that changes the background image ...

What's causing this element to overlap the previous one (apologies, once more)?

This is the code: <form id="the_form" action="">ev </div> <!-- final pay_block --> <div class="linea"/> <div class="spacer"/> The "linea" element is currently overlapping the buy button because a margin-bottom of 15px has be ...

In search of javascript implementations of the pubhubsubbub protocol that are open source

Can you list out some of the open-source Javascript implementations for the PubSubHubbub protocol, starting with the publishing side? ...

Steps for removing the bottom border for the last child in the Material UI Box Component

I have a Box Component where ListItems are wrapped. For each child, I have a border-bottom of 1px solid class set for the Box component, but I don't want it for the last child. How can I achieve that? {data.map((item, i) => { return ...

Can you show me the way to open a single card?

Can someone assist me in making it so only one card opens when clicked, rather than all of them opening at once? Additionally, if there is already an open card and I click on another one, the currently open card should close automatically. If you have any ...

Typescript check for type with Jest

Assume there is an interface defined as follows: export interface CMSData { id: number; url: string; htmlTag: string; importJSComponent: string; componentData: ComponentAttribute[]; } There is a method that returns an array of this obj ...

What causes my XMLHttpRequest to be terminated prematurely?

My code utilizes an XMLHttpRequest to log in to a remote server by sending login parameters (username and password) as JSON. Here is my code snippet: var json_data = JSON.stringify({ "method": "login", "user_login": user, "password": password ...

Combining AngularJS, D3, and JQuery on a single webpage can pose challenges, specifically with D3's ability to accurately read DOM dimensions

I am encountering an issue with my webpage not loading properly. I have structured it using a simple header-body-footer layout in html5 and CSS3. +----------+ | HEADER | +---+----------+---+ | BODY | +---+----------+---+ | FOOTE ...

Implementing server-side validation measures to block unauthorized POST requests

In my web application using angular and node.js, I am in the process of incorporating a gamification feature where users earn points for various actions such as answering questions or watching videos. Currently, the method involves sending a post request t ...

Break down objects into arrays of objects in JavaScript

Hello, I'm struggling with transforming this object into an array of objects. The 'number' and 'reason' fields are currently stored as strings separated by commas. I need to split them into their own separate objects. The example ...

There are two distinct varieties of object arrays

I recently encountered an issue while trying to sort my array of Star Wars episodes by episode ID. The problem emerged when comparing two arrays: one manually inputted in the code snippet labeled as "1" on the screenshot, and the other generated dynamicall ...

What is the best way to create a video that adjusts to different screen sizes within

I am working on the responsive version of client's website and I stumbled upon this issue - when I make smaller the browser window, the youtube video is not centered - the padding-right: 10px; is ignored - why? How could I fix that? Here is the CSS ...

What is the best way to update the value of a specific key in discord.js?

As I struggle to explain properly due to my limited English proficiency, I am reiterating my question. In my config.json file, there is a key named "status" with a corresponding value of "online". I am attempting to change this value but haven't been ...

What is the process for retrieving the search function value in Node Js?

I have been working on creating a search bar functionality using the Express Framework in Node.JS, Handlebars, and MySQL. The connection to MySQL is all set up and functioning properly, I have installed body parser, and even tested the query directly in ph ...

Tips on obtaining the element's ID as a function parameter

I am currently learning front-end development and I am just starting to delve into JavaScript. Recently, when I tried to execute a piece of JavaScript code from the backend by passing some element ids, I encountered an error that says Cannot read property ...

Retrieving Parameter from DHTMLWindow

Hey there, I've run into a bit of a dilemma with my HTML web page. I have a DHTMLWindow that opens, and I want to send a parameter from the 'parent' HTML web page to that window. The goal is for the DHTMLWindow to then pass on the parameter ...

Concealing the source code within a Next.js application

Currently, I am utilizing next.js for a project. We have a contact page where we display email addresses in cards, but we want to prevent bots from accessing this information. A solution was discovered by one of my colleagues to hide the email addresses i ...