Avoid Using "background-attachment: fixed" with background-image:(url("./assests/bg.png")) without fallback or will get bug in mobile first Design...

Kunal MathuriKunal Mathuri
1 min read

. hero {

background-image: url('banner.jpg');

background-size: cover;

background-position: center;

background-attachment: fixed;

height: 100vh;

}

  • Doesn't always work on mobile browsers (especially iOS Safari)

  • Some devices disable it by default for performance reasons

  • If you want cross-device parallax, you'd need JavaScript solutions or CSS tricks using transform/position: sticky.

  • better solution to avoid this kind of performance issue is using fallback or responsive version of background-attachment: fixed; with @media that will make it degrade gracefully on phones too.

  • so, the solution is as follows

  • .hero {

  • background-image: url('banner.jpg');

  • background-size: cover;

  • background-position: center;

  • background-attachment: fixed; /* desktop parallax effect */

  • height: 100vh;

  • display: flex;

  • justify-content: center;

  • align-items: center;

  • color: white;

  • text-align: center;

  • }

    @media (max-width: 768px) {

  • .hero {

  • background-attachment: scroll; /* fallback for mobile */

  • }

  • }

0
Subscribe to my newsletter

Read articles from Kunal Mathuri directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kunal Mathuri
Kunal Mathuri