I have a somewhat strange behaviour in Chrome and Safari. I have a scaled (transform: scale()
) container with a video and other elements inside of it. At some scalings the absolute positioned elements with a high z-index disappears and does not come back again.
How can I fix this?
Note that I cannot give the video element a negative z-index and I need to use overflow: hidden;
.
Example
I have made an example that scales the outermost container up and down. At a specifik scale value the element with class .on-top
(and text "I should always be on top."
) disappears. When scaling down again it suddenly appears.
Link to exmaple: https://jsfiddle.net/iafiawik/Lcox1ecc/
Conclusions
- It seems like the size of the element matters. The larger I make it, the larger is the scale value before it disappears.
- I have also tested to set
transform: scale(1.4)
with CSS directly on the element and the behaviour is the same.
The issue does not exist if I:
- Replace the video tag with a div
- Remove
position: absolute;
from siblings to.on-top
(that is,.below
) - Remove
overflow: hidden;
from.content
- If I move
.on-top
so it is placed after the video tag in the document flow
(But of course none of these workarounds work for me in reality because of project specific reasons. I also cannot give the video element a negative z-index and I need to use overflow: hidden;
.)
Suggested workarounds from the community (thanks!)
- Give the video tag a negative z-index (can't do this because I sometimes have elements placed behind the video)
- Remove
overflow: hidden;
(I can't removeoverflow: hidden;
)
Browsers
I have seen this issue in Chrome (Mac) and Safari (Mac).
Update 1
Seems like this bug report pretty much covers my problem. However, it does not provide a fix for it.
Update 2
I've answered my own question by providing my solution to this problem.
Update 3
There are a lot of answers coming in that either modify the z-index
of the video or adds translateZ
to the .on-top
element. Demos have shown that both of those approaches do fix the issue.
However, since my HTML structure is the output from a visual HTML editor (long story ...), I do not know what elements will be there or if they should be in front, below or next to a video. Therefore I am looking for a solution that does not require changes to individual elements that are inside the scaled element.