Video and z-index inside scaled element: some divs disappear Video and z-index inside scaled element: some divs disappear google-chrome google-chrome

Video and z-index inside scaled element: some divs disappear


It looks like a bug in Chrome. Notice that when you scale the image, the element inspector keeps telling you that the size of #scaled is 1024x768:

Chrome Scale

Where as in Firefox:

Firefox Scale

Now, apparently, Chrome uses the wrong size to conclude that .on-top is completely outside .content and hides it because of hidden overflow (it should not be doing this but apparently it is trying to optimize away any element that displays above a video). Examples:

Scale: 1.225Parent width: 1254.40Child left: 1254.40 - (100 + 90) * 1.225 = 1021.65Result: less than 1024 (partially inside)Scale: 1.230Parent width: 1259.52Child left: 1259.52 - (100 + 90) * 1.230 = 1025.82Result: greater than 1024 (completely outside)

Unfortunately I could not find an elegant solution. Ideally you should revise your HTML markup and CSS, perhaps align the top element with left edge. As a last resort, you can move the elements more towards left using transparent border:

var goalScale = 140;var startScale = 100;var currentScale = 100;var shouldScaleUp = true;var container = document.getElementById("scaled");var scaleInfo = document.getElementById("scale-info");function step() {  container.style.transform = "scale(" + (currentScale / 100) + ")";  scaleInfo.innerText = "Scale: " + (currentScale / 100);  if (currentScale === goalScale) {    shouldScaleUp = false;  }  if (currentScale === startScale) {    shouldScaleUp = true;  }  if (shouldScaleUp) {    currentScale += 0.5;  } else {    currentScale -= 0.5;  }  window.requestAnimationFrame(step);}window.requestAnimationFrame(step);
.scale-info {  position: fixed;  left: 0;  top: 0;}#scaled {  background: #cccccc;  width: 1024px;  height: 768px;  position: fixed;  left: 200px;  top: 200px;}.content {  height: 100%;  overflow: hidden;  position: relative;  margin-left: auto;  margin-right: auto;  background: rgba(34, 34, 56, 0.2);}.below {  position: absolute;  width: 300px;  height: 300px;  right: 0px;  top: 100px;  background: purple;  z-index: 1;  opacity: 0.8;}.below-2 {  z-index: 3;  right: 100px;}.below-3 {  z-index: 4;  right: 400px;}.on-top {  position: absolute;  width: 50px;  right: 100px;  top: 150px;  background: pink;  z-index: 5;  padding: 20px;  /* a 200px border moves the element towards left */  border-left: 200px solid transparent;  background-clip: padding-box;}.on-top h1 {  font-size: 20px;}#video {  position: absolute;  z-index: 4;  width: 1024px;  height: 768px;  background: rgba(0, 0, 0, 0.4);}
<div id="scale-info"></div><div id="scaled">  <div class="content">    <h2 class="below below-1"> I have z-index 1</h2>    <div class="on-top">      <h1> I should always be on top.<br> I have z-index 5</h1>    </div>    <h2 class="below below-2"> I have z-index 3</h2> <video id="video" src="https://www.w3schools.com/html/mov_bbb.mp4"></video>    <h2 class="below below-3"> I have z-index 4</h2>  </div></div>