Tips for avoiding background-image zoom on mobile browsers when stacking elements vertically:

Within my ReactJS application, I have implemented a feature where elements are added vertically from top to bottom when the "Post" button is clicked.

https://i.sstatic.net/KwPYV.jpg

These elements display correctly on both mobile and desktop browsers. However, there is an issue when viewing on mobile - the background image keeps zooming in each time an element is added.

https://i.sstatic.net/OGfCO.jpg

To address this issue, I have included JQuery in my public/index.html file as I require it to randomly change the background image using a specific function. This functionality cannot be achieved by calling a JavaScript function within the CSS url() for the background-image.

For the desktop version, I have successfully implemented the random background image feature. Therefore, I need to update my CSS to resolve the zooming background image problem on mobile.

$(document).ready(function() {
  const randomImage = chooseBackground();
  if ($(window).width() < 650) //mobile browser
  {
    $('html, body').css('background-image', `url(${randomImage})`);
    $('html, body').css('background-repeat', 'no-repeat');
    $('html, body').css('background-attachment', 'fixed');
    $('html, body').css('background-size', 'cover');
    $('html, body').css('background-position', 'center');
  }
});

My goal is to have a fixed background image on mobile without any zooming or movement, allowing users to scroll down to view all the elements seamlessly.

Answer №1

background-size: cover will adjust the image to perfectly fit either the vertical or horizontal box limits of the container. As elements are added, the body expands vertically, requiring the image to fit into a larger container, giving it a zoomed appearance.

To address this issue, setting max-height: 100vh on the body with overflow: auto should resolve it. You can see an example in this fiddle.

It is recommended to use React for managing background images as well, as using multiple frameworks simultaneously may not be ideal.

Update:

After testing on my phone, I discovered that the scaling issue is specifically related to Safari mobile. By enclosing your page within a <div class="wrapper"> or similar container and applying the styles to it, you can maintain the background image size without distortion.

However, please be aware that due to Safari's spring scrolling behavior, there may be some limitations in the smoothness of scrolling, as shown in this codepen.

In my opinion, a simple solution would be to utilize a <div class="background"> container with position: fixed and a low z-index, as demonstrated in this codepen.

.background {
  position: fixed;
  top: 0px;
  left: 0px;
  right: 0px;
  bottom: 0px;
  z-index: -1;

  background-image: url(http://placehold.it/300x300);
  background-attachment: fixed;
  background-size: cover;
  background-position: center center;
}

This approach worked successfully on my phone (you can view the result here on your phone).

Answer №2

It seems like the issue may be with this particular line of code:

$('html, body').css('background-size', 'cover');
Have you considered experimenting with a different value for background-size? For more information on this topic, check out this resource.

The current value for background-size, which is set to "cover," adjusts the background image to completely cover the container, even if it means stretching or cropping the image slightly.

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

Infinite Vue.js Scrolling: Elevate Your Web

Do you know of any directive that can be used to implement Ajax infinite scroll with vue.js? Or is there a directive already available for this purpose? Your assistance would be highly appreciated. ...

What advantages does incorporating inline CSS offer in comparison to using an external CSS file for background images?

I'm currently debating the best approach to take in this situation. Let's say I have a background image that I want to use on multiple pages, each with a different background image. The pages themselves must be responsive. Would it be better to g ...

In TypeScript, make sure to verify the type of an object to avoid any potential compilation errors stemming

Here is the code snippet: export default class App { el: HTMLElement; constructor(el: string | HTMLElement) { if (typeof el === "string") { this.el = document.getElementById(el); } if (typeof el === typeof this.el) { t ...

What is the best way to prevent an HTML form from being submitted when a user is not logged in, but still allow submission when the user is signed

One of the requirements for users of my application is to be signed in before they can submit form information. Upon clicking on the submit button, my jQuery script verifies if the user is already signed in. If the user is not signed in, an error message ...

The JSX syntax was not properly parsed by Jest despite correct configuration settings

After setting up a new Next.js project and configuring jest and testing library as instructed, I encountered a syntax error causing my test to fail. It seems that ts-jest is having trouble parsing stayles.container for some reason. What could be the cause ...

Exploring alternative server options for executing GraphQL queries on the client side

I am currently working with a Create-React-App server that proxies all API requests to my back-end Express-GraphQL server, which is located at localhost:3001. Everything works well when I make a request on the homepage component '/', as it sends ...

What is the best way to align a button with the edge of an image?

How can I use CSS to position a button on the edge of an image? https://i.stack.imgur.com/FEFDC.png Here is my code: <div> <button class="" aria-label="Eat cake">Btn</button> <img class="pull-left" src="style.png" style="widt ...

Ways to reduce the size of a Select box when the option text is too lengthy

How can I reduce the size of the select box by splitting the lines of the options I attempted to modify the CSS with: select { width:100px; } However, only the select element was affected. The options remained the same size. My goal is to have ...

The command "react-app-rewired" cannot be found, even after being uninstalled from the system

A while back, I added the following package to my project: npm install react-app-rewired --save-dev This required me to modify the package.json file like so: "scripts": { "start": "react-scripts start", "build&qu ...

I am encountering some difficulties with the functionality of the angularjs dialog

I've been attempting to integrate an AngularJS dialog feature into my application by following the examples provided on material.angularjs.org. However, despite copying everything accurately, I am unable to get it to function. Can anyone help identify ...

Tips for using Jquery to retrieve HTML from external sources

I have successfully managed to display an internal webpage on my HTML, as shown below: <!doctype html> <html> <head> <script type="text/javascript" src="jquery-1.4.4.js"></script> </head> <body> & ...

Could you please explain the output of the offset function in jQuery?

After reviewing the code, I am struggling to comprehend the output of the offset function: $(document).ready(function() { $('#goto-show-form').click(function() { $('html, body').animate({scrollTop: $("#show-form").off ...

What steps can I take to resolve the issues I encounter while attempting to set up a Material-UI DataGrid?

Trying to set up a Material-UI DataGrid, but encountering some errors: Error: MUI: The data grid component requires all rows to have a unique id property. A row was provided without id in the rows prop Also receiving: Warning: Failed prop type: Invalid ...

The default choice vanishes once a non-empty option is chosen

Here is a simple example illustrating my issue: JSFiddle. Initially, I have an empty/default option, but when I select something else from the drop-down, this default option disappears. How can I keep this default option visible after making a selection? ...

Guide on automating the process of sending scheduled emails to registered users on various dates

Currently, I am working on a MERN Stack project and in my collection on MongoDB, each document includes fields such as: userName email lastDate The property lastDate varies for each user. My goal is to automatically send an email to each user three days ...

Error: The input '{ projectId: string | undefined; }' does not match the expected parameter type 'SCL| SPD | undefined'

While attempting to deploy my project on Vercel, I encountered an error message indicating the following: A type error has been observed with the argument provided in the configuration. It states that '{ dataset: string; projectId: string | undefined ...

Understanding how flex-wrap property works in flexbox is essential for creating

Take a look at the code snippet below: .flex-container { padding: 20px; margin: 20px; list-style: none; border: 1px solid silver; -ms-box-orient: horizontal; display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -moz- ...

What is the best way to create a CSS grid with horizontal overflow?

I'm currently working on a 4-column layout using CSS grid. As the browser width reaches a specific breakpoint, I want the first column to be fixed on the left side of the browser (X-axis) while the other columns scroll under it. Right now, I am utiliz ...

LinkButton not triggering on Master Page when accessed from Second Child Page in ASP.NET

I am currently working on a project in ASP.NET (Framework 4.0) where I have implemented an Asp LinkButton in the Master Page that is linked to two different pages (Home.aspx and service.aspx). The question at hand is: The LinkButton1 functions properly on ...

When using ContentEditable in Firefox, it generates double line breaks instead of a single one

Noticed an interesting issue with div contenteditable where Firefox is interpreting 1 newline as 2 newlines. Is this a bug or am I overlooking something? Test it out in the example below by typing: Hello World within the contenteditable. When accessi ...