Explain Codes LogoExplain Codes Logo

Background image jumps when address bar hides iOS/Android/Mobile Chrome

javascript
responsive-design
performance
best-practices
Nikita BarsukovbyNikita Barsukov·Dec 2, 2024
TLDR

Keep your background image stable with CSS using background-attachment: fixed; and background-size: cover;. These tactics enforce a fixed position against viewport resizing and ensure full coverage of the image.

.element { background-image: url('image.jpg'); background-attachment: fixed; background-size: cover; }

Assign these properties to your element for a stable background irrespective of address bar motions.

Solving the height shifting problem with JavaScript

Though the background-attachment: fixed; solution is effective, the notorious iOS vh bug might cause hiccups. Leverage JavaScript to dynamically adjust element heights based on the viewport size.

function resizeBackground() { var extraHeight = 60; // Extra space, because address bar is a diva document.getElementById('bg1').style.height = window.innerHeight + extraHeight + "px"; document.getElementById('bg2').style.height = window.innerHeight + extraHeight + "px"; } window.addEventListener('resize', resizeBackground); // Adding a bouncer at the resize event club resizeBackground(); // First serve, coming right up!

This script acts like a vigilant watchman, monitoring for resize events, including those fired by address bar changes. It adjusts the new heights with some extra headroom to avoid any visual jumps.

Avoiding the resize dance - Smooth transitions

A clever CSS transition trick can smoothen out unintentional layout shifts. This technique introduces a virtual delay in any dimension alterations, thereby making them practically invisible.

.element { transition: height 999999s; // Slow 'n' steady wins the race }

Add the above to your CSS to ensure any background resizing operation is as smooth as aged whiskey.

Handling mobile browser quirks

Catering to diversity in aspect ratios

Some devices might showcase peculiar behaviours due to their unique aspect ratios. Harness the power of media queries in CSS to pinpoint these exceptions and tailor the experience to circumvent any potential shifts.

@media (aspect-ratio: 16/9) { .element { /* Dress code for 16:9 screens */ } } @media (aspect-ratio: 3/2) { .element { /* Tailor-made for 3:2 screens */ } }

Bear in mind, different devices and browsers don unique attire. Cross-check for any wardrobe malfunctions thoroughly!

Locking down the mobile user experience

To ensure a consistent performance, apply overflow: hidden; on the html element and overflow-y: scroll; on the body. This can help cage the URL bar and negate any corresponding resize effects:

html { overflow: hidden; // Look ma, no hands! } body { overflow-y: scroll; // Now do the stroll }

This trick could be a hard-hitter in eradicating background shifts.

Contemplating design alternatives

If you're finding it hard to hit the sweet spot with these solutions, it might be time to rethink the mobile design. Say 'pass' to full-screen backgrounds or consider strategically positioned image elements for a more fluid user experience.