Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS not respecting z-index with -webkit-overflow-scrolling

Tags:

Problem:

On iOS the z-index of a scrollable area is ignored when using -webkit-overflow-scrolling. If two objects with -webkit-overflow-scrolling overlap the lower one is scrolled instead of the one being displayed above.

How to reproduce:

Create two elements overlaying each other (with position: absolute for example), one of them having a higher z-index and add

.selector {     overflow-y: auto;     -webkit-overflow-scrolling: touch; } 

to both of them. Both elements should have enough content to be scrollable.

Additionally add

<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="apple-mobile-web-app-capable" content="yes">  <meta name="apple-mobile-web-app-status-bar-style" content="black"> 

to your <head>. Then add the page to your home screen and launch it from there.

If you then try to scroll the upper element the element beneath is scrolled instead.

MCVE:

Alternatively just check out this pen. Launch the full version from your iOS device, add it to your home screen and launch from there.

Environment:

Tested on iPhone 5 and iPhone 6 with iOS 9.1 and iOS 9.3.2

Observations:

  • The issue only occurs when launching the page/app from homescreen (pinned app) or inside a Xamarin Webview (might have something to do with UIWebView and WKWebView)
  • After changing the device orientation (portrait/landscape) after page load the problem is fixed until the page is reloaded (maybe re-triggering layout fixed it?)
  • Changing the lower elements overflow-y to hidden via JS does fix the problem, however toggling overflow causes a repaint causing performance issues
  • Removing height: 100%; width: 100% from html, body fixes the problem as well, however those have to be set for percentage values to work properly

Needed is a proper solution / workaround to fix this issue without causing any troublesome side-effects. Also explanation of why this happens would be appreciated.

like image 840
Aides Avatar asked Jun 08 '16 08:06

Aides


People also ask

Why Z-Index property is not working?

z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.

How do you fix Z-index issues?

To sum up, most issues with z-index can be solved by following these two guidelines: Check that the elements have their position set and z-index numbers in the correct order. Make sure that you don't have parent elements limiting the z-index level of their children.

Does Safari support Z-index?

Bookmark this question. Show activity on this post. The Z-index isn't rendering properly on Safari - but it is working fine on Chrome and Firefox.

How do you override Z-index?

Solution: Move the modal outside of the content parent, and into the main stacking context of the page. Now, the modal element is a sibling element to the two others. This puts all three elements in the same stacking context as them, so each of their z-index levels will now affect one another.


2 Answers

Essentially, what you're experiencing is an iOS bug with -webkit-overflow-scrolling: touch;. In order to solve this bug, as per this answer, just add the following CSS styles to the scrollable div:

-webkit-transform: translateZ(0px); -webkit-transform: translate3d(0,0,0); -webkit-perspective: 1000; 

So all in all, if you add the above styles to the styles for the scrollable class in your CSS, it should work. Tested on an iPhone 5s running iOS 9. Note that if you scroll down to the bottom or top of the scrollable section that is above everything else, it will start to scroll the body.

I believe that what those extra styles do is trick the iPhone into using the GPU, but remember that they're only necessary due to a bug with Safari - it's not your fault and these extra styles shouldn't really need to be included. But pop them into your CSS and it should work like a dream!

like image 86
Isaac Adni Avatar answered Oct 09 '22 03:10

Isaac Adni


Assuming you face the problem like them. Please, take a look at these post:

  • https://stackoverflow.com/a/20898012/4168867

  • [Solved] Safari for iOS: z-index order bug scrolling a page with a fixed element

  • https://stackoverflow.com/a/9227478/4168867

Hope this help!

like image 39
AnLT Avatar answered Oct 09 '22 03:10

AnLT