r/csshelp • u/Open-Carry3751 • Apr 27 '24
Trying to get a fixed background on mobile browsers
I'm trying to get a fixed background, but it doesn't work on mobile browsers (especially iOS, webkit). Instead, I tried to make a div which is absolute positioned (top 0, left 0, z-index -1 etc), which works better but not really good. It's fixed, but it disappears after some scrolling and the position bugs out after scrolling down and up again. Do you have any ideas?
5
Upvotes
2
u/CarefulDaredevil Apr 27 '24
Unfortunately, Safari often struggles with fixed backgrounds using
background-attachment: fixed. A more reliable approach is to assign a class to adiv, set thedivto a fixed position, and then apply a non-fixed background image to it. Or you could try usingbody::before. See https://jsfiddle.net/saeokn81/.1
u/gatwell702 Apr 27 '24
this is the shorthand way that you can do it:
background: url("image path") no-repeat center center/cover fixed;
1
u/Illustrious-Code-674 Apr 07 '25
I know this post is old, but maybe some people still look for answers and solutions.
I solved it a little out of the box. I described the solution in this post:
https://appcrates.pl/blog/fixed-backgrounds-in-safari-browser
1
u/ccmgc Jan 19 '26
Delete bg-image from body and add image in body::before like this:
body::before {
content: "";
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
background-image: url(img/.jpg); /* image HERE!!!!! */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
z-index: -1;
}