Potential issue with background-attachment: fixed function on Android Chrome

The CSS property background-attachment: fixed behaves as expected on Desktop Firefox, Android Firefox, and Desktop Chrome, but it has issues on Android Chrome.

Similar problems have been discussed on Stackoverflow before, but my scenario differs slightly from others. While most cases focus on fixing a background image for the entire viewport, my issue involves using it as a fixed background displayed behind a div element as the user scrolls past.

Devices that were tested include:

  • Windows 8.1 - Chrome Desktop: Version 73.0.3683.86 (Official Build) (64-bit)
  • Android 4.4.4; XT1080 Build/SU6-7.7 - Chrome 73.0.3683.90

I've created a simple example to showcase this problem in a CodePen demonstration:

View the CodePen here

Below is a simplified version of the problematic code:

<!DOCTYPE html>
<html lang=en>
    <head>
    <meta charset=utf-8>
    <title>Fixed Problem</title>
    <meta name=viewport content="width=device-width, initial-scale=1">
<!-- https://codepen.io/bmt-codepen/pen/drxbpN -->
<style>
body {
    margin: 0;
    padding: 1em;
}

.fixed {
    height: 100%;
    position: relative;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    background-position: 100%;
    padding: 1em;
    color: white;
}

.image1 {
    background-image: url('https://images.unsplash.com/photo-1515694346937-94d85e41e6f0');
}
.image2 {
    background-image: url('https://images.unsplash.com/photo-1534274988757-a28bf1a57c17');
}

</style>
</head>
<body>
    <div>
        <p>Lorem ipsum dolor sit amet...</p>
        <!-- Content omitted due to length--!>
    </div>
    <div class="fixed image1">
        <h1>The background fails to remain fixed on Chrome for Android</h1>
    </div>
    <!-- Additional similar div elements with different images--!>
</body>
</html>

If anyone has encountered and solved this particular issue before, I would greatly appreciate some guidance on how to address the styling problem in this specific context.

Answer №1

If you're encountering this issue, the provided solution should do the trick. It's a common problem that can occur on certain devices. I hope this resolves the issue for you.

 .fixed {
    background-image: url('https://images.unsplash.com/photo-1515694346937-94d85e41e6f0');
  background-attachment:scroll;
  background-position:left bottom;
  background-size:cover;
}

$(window).scroll(function() {
  var scrolledY = $(window).scrollTop();
  $('.fixed').css('background-position', 'left ' + -((scrolledY)) + 'px');
});

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

Using jQuery to dynamically disable a textbox when a checkbox is checked

I'm trying to implement a functionality where a textbox becomes disabled and changes its background color when a user unchecks a checkbox. Conversely, if the user checks the checkbox, the textbox should become editable and change its background color ...

Tips for designing scrollable overlay content:

I am currently in the process of building a page inspired by the design of Hello Monday. Right now, I have added static content before implementing the parallax effect and auto-scroll. Here is my progress so far: Check out the Sandbox Link One challenge ...

The key to crafting the perfect container rests in getting the dimensions, height settings, and footer just right for showcasing the figure with an icon and Font

I am currently tackling one of the bootstrap challenges and I find myself at the final stage where I am working on completing the landing page. However, I have encountered a few minor issues with the container in the div as well as the footer. My main con ...

Center aligned columns on a small screen in Bootstrap by shifting them from left or right

My website has a header with a logo on the left and text on the right, but when the page gets smaller, the text overlaps with the logo. It doesn't look good at all. Check out the Fiddle here <div class="container"> <div class="row"> ...

Problem with Google Play Store app signing key due to a 105-character hexadecimal issue

I am currently attempting to integrate my keystore with Google Play app signing. According to the guidelines, the command should be: java -jar pepk.jar --keystore=foo.keystore --alias=foo --output=output.zip --encryptionkey=The_Hex_Which_Is_105_Char The ...

What is the method for customizing the hover color in a mantine.ui menu?

I've been attempting to modify the menu color when hovering over it, but unfortunately, it's not working. Could anyone provide guidance on how to change the hover color in mantine.ui menu? ...

What is the best way to keep the heights of two divs in sync?

Is there a way to make two divs have the same height, even though their content determines their individual heights? I am looking for a solution that doesn't involve using a table or a parent div. I'm new to JavaScript, so I'm hoping there i ...

Adding space between Bootstrap columns to align them one below the other on small devices

https://i.sstatic.net/turhY.png Within this illustration, there are two columns - one with the class col-sm-3 and the other with col-sm-8, situated in a single row. Upon resizing the window (as shown in the screenshot provided), the two columns appear ...

Show two <p>'s next to each other

I am trying to arrange 2 paragraphs side by side. <p class = "firstClass">This is the first paragraph.</p> <p class = "secondClass">This is the second paragraph.</p> Result: This is the first paragraph. This is the second paragra ...

Body becomes overwhelmed by the presence of white space when margins are reduced to zero

I am puzzled by this issue because I have checked the developer tools and can't find anything causing the body to extend beyond the white space. The only thing that appears over the whitespace is when I hover my mouse over the HTML in the source code ...

Creating a fullscreen layout in HTML with specific minimum and maximum widths

Looking to create a layout where one item takes up 1/3 of the screen-width with a minimum width of 500px and a maximum width of 700px, while another item fills the rest of the screen. Ideally, setting a width of 66% should work, but issues arise when the h ...

Ways to position an element at the edge of the browser either on the left or right side when the image is not in a centered container

Looking to create a unique layout that involves: Utilizing a three-column structure Incorporating a div element that spans two columns Positioning an image within the two-column div so that it extends to the left edge of the browser window while staying ...

Tips for establishing a fixed point at which divs cease to shrink as the browser size decreases

There are numerous dynamically designed websites where divs or images shrink as the browser size decreases. A great example of this is http://en.wikipedia.org/wiki/Main_Page The div containing the text shrinks proportionally to the browser size until it ...

What is the best way to create a flexible Ul-LI menu?

Is it possible to create a flexible menu where clicking on an item does not cover other items in the menu? #demo { margin: 30px 0 50px 0; font-family: sans-serif; } #demo .wrapper { display: inline-block; width: 180px; height: 20px; position ...

The Hamburger Menu in the Bootstrap Navbar is failing to collapse

My website uses Bootstrap v5.3 and I've added a navbar, but the hamburger icon is not collapsing on mobile devices when clicked. Can someone please review my code to see if there are any issues? I can't figure out what's wrong. <!DOCTY ...

Span element not displaying as a clickable hyperlink

The magnifying glass icon inside the orange search bar at the top of this page is not appearing as a link, so when hovered over there is no pointer.. Any thoughts on why this might be? Below is the CSS: .searchGlass { background-image: url(/images/search ...

Enable the choice for Bootstrap collapse animation to be customized

Is there a way to allow users or admins on my website to decide whether or not Bootstrap 4 collapse elements should be animated? The concern is that when many elements are moved by the animation, it becomes less smooth. Therefore, it would be ideal to give ...

Arranging items in CSS and showing them stacked on top of each other

How can I rearrange the order of id3 to be displayed below id2 instead of on the side using CSS? Here is an example of the HTML and CSS: HTML <div class="main" ></div> <div id="id1">Im in div1</div> <div id="id2">Im in div2 ...

What is the best way to eliminate the curved border and inset box shadow from the Material UI TextField component in React?

Check out this input field: https://i.sstatic.net/Z6URV.png Notice the inner shadow and curved borders. Take a look at the given code snippet: import React, {Component} from 'react'; import TextField from 'material-ui/TextField'; im ...