Categories
css transform z-index

z-index is canceled by setting transform(rotate)

110

Using transform property, z-index is canceled and appeared in the front.
(When commenting out -webkit-transform, z-index is properly working in below code)

.test {
  width: 150px;
  height: 40px;
  margin: 30px;
  line-height: 40px;
  position: relative;
  background: white;
  -webkit-transform: rotate(10deg);
}

.test:after {
  width: 100px;
  height: 35px;
  content: "";
  position: absolute;
  top: 0;
  right: 2px;
  -webkit-box-shadow: 0 5px 5px #999;
  /* Safari and Chrome */
  -webkit-transform: rotate(3deg);
  /* Safari and Chrome */
  transform: rotate(3deg);
  z-index: -1;
}
<html>

<head>
  <title>transform</title>
  <link rel="stylesheet" type="text/css" href="https://stackoverflow.com/questions/20851452/transformtest.css">
</head>

<body>
  <div class="test">z-index is canceled.</div>
</body>

</html>

How do transform and z-index work together?

3

  • This is the fiddle I made, Do you want to hide the text with z-index?

    Dec 31, 2013 at 5:04

  • Working fine jsfiddle.net/raunakkathuria/8MQkP what is the doubt actually?

    Dec 31, 2013 at 5:05

  • Thank you for making fiddle. I want to show the shadow behind text rectangle (like a post-it).

    – kbth

    Dec 31, 2013 at 5:31

168

Let’s walk through what is occurring. To start, note that z-index on positioned elements and transform by itself create new “stacking contexts” on elements. Here’s what’s going on:

Your .test element has transform set to something other than none, which gives it its own stacking context.

You then add a .test:after pseudo-element, which is a child of .test. This child has z-index: -1, setting the stack level of .test:after within the stacking context of .test Setting z-index: -1 on .test:after does not place it behind .test because z-index only has meaning within a given stacking context.

When you remove -webkit-transform from .test it removes its stacking context, causing .test and .test:after to share a stacking context (that of <html>) and making .test:after go behind .test. Note that after removing .test‘s -webkit-transform rule you can, once again, give it its own stacking context by setting a new z-index rule (any value) on .test (again, because it is positioned)!

So how do we solve your problem?

To get z-index working the way you expect, make sure that .test and .test:after share the same stacking context. The problem is that you want .test rotated with transform, but to do so means creating its own stacking context. Fortunately, placing .test in a wrapping container and rotating that will still allow its children to share a stacking context while also rotating both.

  • Here’s what you started with: http://jsfiddle.net/fH64Q/

  • And here’s a way you can get around the stacking-contexts and keep
    the rotation (note that the shadow gets a bit cut off because of .test‘s white background):

.wrapper {
    -webkit-transform: rotate(10deg);
}
.test {
       width: 150px;
       height: 40px;
       margin: 30px;
       line-height: 40px;
       position: relative;
       background: white;
}
.test:after {
       width: 100px;
       height: 35px;
       content: "";
       position: absolute;
       top: 0;
       right: 2px;
       -webkit-box-shadow: 0 5px 5px #999; /* Safari and Chrome */
       -webkit-transform: rotate(3deg); /* Safari and Chrome */
       transform: rotate(3deg);
       z-index: -1;
}
<div class="wrapper">
    <div class="test">z-index is canceled.</div>
</div>

There are other ways to do this, better ways even. I would probably make the “post-it” background the containing element and then put the text inside, that would probably be the easiest method and would reduce the complexity of what you have.

Check out this article for more details about z-index and stacking order, or the working W3C CSS3 spec on stacking context

3

  • 8

    This was a great explanation of one of the most complex aspects of CSS layout. For future reading, check the W3 spec

    – AndyBean

    Apr 21, 2014 at 16:11


  • Fantastic explanation. I was using transform on my parent element and was wondering why my ::before element was not stacked behind the parent.

    – OGreeni

    Nov 8 at 18:59

  • I am using it in the pseudo-element, and I can’t make it keeps its z-index value.

    Nov 21 at 11:10

27

Set the div you want to stay on top to position:relative

3

  • 2

    It worked for me. Is it related with the stacks context mentioned above? If someone can help to clarify it will be greatly appreciated.

    – glihm

    Jul 28, 2019 at 6:52


  • maybe it’s to do with when you have a position of relative (or absolute) and a z-index set, a new stacking context is created?

    – nCardot

    May 26, 2021 at 2:06

  • Thanks for this! Worked like a charm after trying to set CSS for elements that are not selected of the same class!!

    – zqlimy

    Aug 9, 2021 at 7:23

11

Had a similar problem where siblings were being transform: translate()‘d and z-index wouldn’t work.

Most straightforward solution is to set position: relative on all siblings, then z-index would work again.