Background image jumps when address bar hides iOS/Android/Mobile Chrome
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Mobile browsing has its unique set of challenges, and one common issue experienced across browsers like iOS Safari, Android, and Mobile Chrome is the "background image jump" phenomenon. When users scroll down a webpage, the address bar hides for better content visibility. However, this action can cause the background images or other elements to "jump," leading to a disruptive user experience. This article explores the technical details behind this issue, providing examples, solutions, and considerations for web developers.
Understanding the Issue
Why Does It Happen?
When a user scrolls on a mobile device, the address bar automatically hides to maximize the visible area on the screen. This change is essentially a dynamic adjustment to the viewport height, which causes a reflow and repaint of the web page. During this reflow process, the following can occur:
- Viewport Resizing: When the browser's address bar hides, the viewport height changes, affecting how percentages and viewport units (vh, vw) are calculated.
- CSS Rendering: If elements, like background images, depend on viewport units for sizing or positioning, they might suddenly shift or resize to align with the new viewport dimensions.
- Repaint and Reflow: The DOM elements need to adjust immediately to the new dimensions, which can cause a visible jump on the screen, especially if transitions have delays.
Common Scenarios
- Fixed Headers: Elements with fixed or sticky positioning often re-calculate their position based on the new viewport size, causing a noticeable movement.
- Fullscreen Modals: If modals or overlays are configured using viewport-relative units, they can experience sudden size changes.
- Background Images with `background-attachment: fixed;`: These images can suffer from visible glitches during scroll due to reflow as the viewport height changes.
Technical Explanation with Examples
Viewport Units
CSS viewport units like `vh` for viewport height can be deceivingly responsive. Before and after the address bar hides, `100vh` represents different pixel values:
- Avoid 100vh: Prefer using JavaScript calculations or CSS `min-height` settings.
- Refrain from Fixed Backgrounds: Try alternative background settings to prevent background attachment from fixed issues.
- Testing Across Devices: Always test the web application on various mobile devices with different orientations and browser versions.
Related reading
- Background task, progress dialog, orientation change - is there any 100 working solution?
- Base64 Decoding in iOS 7
- Base64 Decoding in iOS 7
- Basic Drag and Drop in iOS
- Basic example for sharing text or image with UIActivityViewController in Swift
- Basic example for sharing text or image with UIActivityViewController in Swift
- Behaviour of Future.delayed in dart
- Best architectural approaches for building iOS networking applications REST clients
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.