In my most recent web-app, I have created a background image that I want to be displayed statically, taking up the whole screen and having the content scroll over the top.
Using the ‘background-position: fixed;’ worked fine on desktop browsers, but when it came to Safari on iPhone, this line of code became irrelevant. Safari does not accept scrolling the same way as how you would do it in a desktop browser. It thinks you are moving the whole page within a viewport and therefore the background-position tag is not relevant, as the page is moving within the viewport rather than the content scrolling within the page.
This is a wrapper to apply the CSS to as my background was in body, which seemed to have even more problems when it came to mobile Safari. This wrapper was placed inside the body.
#background_wrap { z-index: -1; position: fixed; top: 0; left: 0; background-size: cover; background-image: url('../images/compressed/background-mobile.png'); background-repeat: no-repeat; background-attachment: scroll; }
@media only screen and (max-width: 750px) { #background_wrap { z-index: -1; position: fixed; top: 0; left: 0; height: 100%; width: 100%; background-size: cover; background-image: url('../images/compressed/background-mobile.png'); background-repeat: no-repeat; background-attachment: scroll; } }
Next, the CSS code. I have created a media query for the mobile site so that the code does not impact the desktop. The desktop version currently uses the image to cover the whole screen and the background attachment works.
As I like how this looks currently, I have created 2 separate IDs so that they apply to mobile sized screens only, but you don’t have to do that.
position: fixed; background-attachment: scroll; and background-size: cover; are the main components to getting the background to look like the first image (mobile) and enable the scrolling as well. You can play around with them, but it took me a while to get to this stage without messing up other areas.