What could be causing my background image to resize while scrolling on mobile devices?

While browsing on mobile, I noticed that my div with a background-image gets zoomed in when scrolling down the page. I did some research and realized that others have experienced this issue as well, but there doesn't seem to be a definitive solution. However, I stumbled upon this interesting link: https://www.w3schools.com/howto/tryhow_css_parallax_demo.htm (source: https://www.w3schools.com/howto/howto_css_parallax.asp), and it seems to work perfectly. So what is w3schools doing differently? I examined their code, but couldn't pinpoint any significant differences.

(Just for your information, I still encounter the problem even without the parallax effect from "background-attachment: fixed;" and even if I don't use a vh unit for the height).

Here's a snippet of my code:

HTML:

<div class="landingContainer">                
    <h1>Welcome to Restaurant</h1>
    <h2>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</h2>
</div>

CSS:

.landingContainer {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  
  background-image: linear-gradient(rgba(0,0,0,0.4),rgba(0,0,0,0.4)),
                    url('/src/components/Home/homeImages/pexels-emre-can-acer-2079438.jpg');
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  
  width: 100%;
  min-height: var(--vh100);
  padding: 1rem;
  color: white;
  text-align: center; 
  overflow-wrap: anywhere; 
}
.landingContainer h1 {
  font-size: clamp(2.2rem, 2.0058rem + 1.2427vw, 3rem);
  font-weight: 600;
  margin: 1em 0 .5em;
  color: white;
}
.landingContainer h2 {
  font-size: clamp(1rem, 0.9892rem + 0.0693vw, 1.1rem);
  font-weight: 300;
  width: 85%;
  max-width: 45ch;
  line-height: 1.6;
}

Thank you!

Update: To better illustrate the issue, please refer to this video:

Answer №1

It appears that the issue was caused by the address bar automatically hiding at the top of the screen. After implementing this fix: https://example.com/fix123, I adjusted the CSS from "top: 0.5px;" to 0.1px.

Answer №2

100vh adjusts as the mobile browser's interface "retracts" during scrolling. If this results in your .landingContainer changing sizes, and since your background is set to cover the container with background-size: cover;, you will notice it being resized.

w3schools appears to be utilizing min-height: 100%; to establish a height instead. Depending on your specific needs, this approach may also work for you.

A while ago, I created a tool to determine which values change and remain constant based on various factors (scrolling, resizing, etc.). It might be valuable for you too. Check it out here:

Answer №3

I encountered the same issue and managed to resolve it thanks to a helpful article I came across:

https://www.w3schools.com/howto/howto_css_parallax.asp

At the bottom of the page, there is a code snippet provided in their code editor:

/* Disabling parallax scrolling for tablets and phones. Adjust pixels as necessary */
@media only screen and (max-device-width: 1366px) {
  .parallax {
    background-attachment: scroll;
  }
}

In addition to that, I made a change to the height of the components affected (I'm using React), setting them to 100% instead of 100vh (even though you mentioned you had already tried this). I believe applying background-attachment: scroll should solve the issue. Ensure no other CSS selectors or media queries are conflicting with this solution. Initially, it didn't work for me because I had inline styling `backgroundAttachment: 'fixed'`. I moved all the styling of those components to my index.css file, and voila, problem solved! It's unfortunate to lose the parallax effect on mobile devices, though.

If none of the above methods prove successful, I recommend checking out another informative article that offers a clever workaround:

https://css-tricks.com/the-fixed-background-attachment-hack/

Answer №4

Indeed, the issue lies in the URL bar automatically hiding and revealing itself, causing fluctuations in the element's height and triggering background readjustments (likely due to background-size: cover).

However, I am hesitant about implementing that fix as I prefer not to have the URL bar constantly visible. I don't want to sacrifice those valuable pixels. If you are comfortable with it always being displayed, then it may be an ideal solution for you.

Answer №5

To enhance the visual appeal of your background image, consider implementing a media query that modifies the background-size: cover; property to a specific size that complements your design.

This method may prove beneficial, particularly if your background consists of a repetitive pattern. With some fine-tuning of the positioning, it could also serve as a viable option for various other image types.

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

Centered alignment with extra margin

How can I horizontally center an element on a page with a margin of 100px on both the left and right if the viewport gets smaller? Do I need to use a parent container for this layout? margin: 0 auto; margin-left: 100px; margin-right: 100px; ...

What is the purpose of using double % in Java or JSP?

Yesterday, while reviewing some code, I came across a line that seemed very peculiar to me. In a JavaScript function, there is a condition checking for a string passed as a parameter in the following manner: "%%unsubscribe%%". See the snippet below for re ...

What is the reason behind the android code being incompatible with IOS operating systems?

I'm encountering an issue where a particular code is working on Android, but not on IOS. The code in question involves using JavaScript to differentiate the items in a list within an HTML select tag. It's perplexing to me how the code can operat ...

Achieve responsiveness for Google Chart Gauge

I am trying to create a Google Gauge chart that is web-responsive, but setting the width to '100%' is not working as expected. Here is the code I am using: <script type='text/javascript'> google.load('visualization&apo ...

I'm attempting to utilize a basic webcam capture upload feature, but it seems that the upload function is not functioning properly

UPDATE: This is the complete code that I simply copied and pasted. <!DOCTYPE HTML> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script language="JavaScript" type="text/javascrip ...

Having issues with PHP not displaying your SQL query results?

Can someone lend a hand? I'm attempting to add some content as per the requirement of stackoverflow for more text instead of just code. ...

Troubleshooting Problem with Matching Heights in Bootstrap Columns

I am experiencing an issue with the bootstrap grid columns on my site not having equal heights. The problem arises when a product name contains a long text, causing height discrepancies among the columns. Despite trying to fix the height issue by adding tw ...

Modify the background of a div according to the size of the screen. Implement this functionality using JavaScript and

After reviewing a specific post at , I find myself unsure of how to target a particular class for reference. I am certain that I want to implement document.onload = checkWidth(); window.onresize = checkWidth(); However, I am uncertain about the procedur ...

Neglecting Margins: The Challenge of Specificity

While utilizing the Foundation 4 framework, I've encountered an issue where the margins are being overridden by the framework's default settings. This situation forces me to use the !important keyword to apply margins to specific elements. Below ...

Steps to create a horizontal animation using CSS3

I have 6 different CSS3 animations that I want to split between going right and left. Can these animations be applied separately or do they all have to be the same direction? Is it also possible to adjust the speed of each animation individually? Here is ...

Troubles encountered with ionic's usage of [innerHTML] within <ion-list>

Encountering an issue while using ionic 3 and angular 2 with styling a large HTML string using [innerHTML]. Strangely, the string gets cut off at the end of the screen instead of wrapping to the next line when placed within an ion-list tag. Any insights on ...

Incorporate an image into the background using the default.css file within the Eclipse e4

As I work on my Eclipse e4 RCP Application, I am trying to set a background image. I have attempted adding the property in plugin.xml: <property name="applicationCSSResources" value="platform:/plugin/rcptutorial/image/image.jpg"> In ...

Unable to configure slick carousel to a 12-column grid in Bootstrap

When attempting to set my carousel to col-xs-12, the columns do not align correctly within the slick carousel in Bootstrap. If I change it to col-xs-6, it works fine but then two grids appear in one row. I require more space for my carousel. Could I be ov ...

I would like to showcase the image with specific dimensions in CakePHP

Currently working on a CakePHP application where I need to upload an image. However, for some sections of my app, I need to display the image at a specific height and width without losing image quality. Any suggestions on how to achieve this? ...

Hiding a related field using a designated delimiter can be achieved with JavaScript

I am looking to create a function that hides the corresponding textarea field of a dropdown. This functionality will be utilized in multiple instances, hence the need for it to be within a function. <div id="formElement1" class="field"> <labe ...

prepend an element before the li element

I am currently working with Angular Material and I am trying to add the md-fab-speed-dial in front of an li element. However, when I attempt to do this, the md-fab-speed-dial appears below the li element, as shown in this image: https://i.sstatic.net/Rqz ...

How can you modify the HTML tags before Bootstrap overrides them?

Imagine having a code snippet like this: <style> #container ul li{ font-size:50px;} .myclass1 ul li{ font-size: 20px;} </style> <div id="container"> <ul> <li> <div class="myclass1"> ...

JavaScript Scroller that keeps on scrolling

Unique Continuous JavaScript Scroller I am currently working on a JavaScript scroller script that enables left to right and right to left scrolling on the click of next and previous buttons. The script is designed to work with 1 to 3 div elements in a con ...

Resolving the issue of "Cannot set properties of null (setting 'innerHTML') at HTMLInputElement.loader" involves identifying the root cause and implementing

Need assistance with this code. Although I believe everything is correct, it still isn't working as expected. The error mentioned in the title comes up whenever I try to use it. I've made multiple modifications to the code but it's still not ...

Is there a tool available that can provide links to all websites within a specific domain?

Can all the links from a given URL be extracted to understand the structure of a website? For instance: www.samplewebsite.com www.samplewebsite.com/page1.aspx www.samplewebsite.com/page2.aspx www.samplewebsite.com/page3.aspx www.samplewebsite.com/page1.a ...