After conducting my own experiments and delving into the topic online, I discovered that elements using position: fixed
do not adhere to the overflow: hidden
property of their parent element. This also applies to children within the fixed
positioned element as they too ignore the ancestor's overflow: hidden
property.
Surprisingly, Apple seems to have found a way around this on Apple Music.
Container with overflow: hidden
https://i.stack.imgur.com/VKfZU.png
Inner element with position: fixed
https://i.stack.imgur.com/ZU18Y.png
The device itself
https://i.stack.imgur.com/W5uhX.png
In the second image where there is a blue highlighted area, you can see that the fixed
positioned element overflows its container along with the phone inside it (as shown in the third image). Despite this overflow, the contents of the fixed
positioned element (the phone) are still truncated by the ancestor with an overflow: hidden
property. If I remove the overflow: hidden
property from the ancestor using Chrome's dev tools, the entire phone becomes fully visible.
How has Apple achieved this? I have attempted to recreate it using the same combination of fixed, relative, overflow properties, among other settings but without success, indicating that something crucial eludes me.
UPDATE
As elucidated by @user3790069 in his response, a fixed
positioned element may be clipped by a relatively positioned one if the latter boasts a higher z-index
, or it is simply located after the fixed
positioned element (thus automatically assigned a greater z-index). Nonetheless, in the case of the Apple Music example, it appears that the overflow: hidden
property continues to play a pivotal role for them. To confirm this, I eliminated several DOM elements to minimize clutter. The following screenshots showcase:
a) a wrapping section
element incorporating overflow: hidden
b) nested within it, a position: fixed
element.
In the initial screenshot, note how the phone is cropped. Contrastingly, in the subsequent screenshot I withdrew the overflow: hidden
property, thereby unveiling the overflowing segment of the phone.
https://i.stack.imgur.com/NU8Gc.png https://i.stack.imgur.com/K2plb.png