Matthieu Charbonnier
Matthieu Charbonnier

Reputation: 2982

Applying rotation breaks shadow effect

I am trying to apply a rotation to a div.

But when I do, it breaks the "piece of paper shadow effect" on it. Why?

And what can I do to keep this effect?

.test {
  position: relative;
  margin: 20px auto;
  width: 300px;
  height: 100px;
  border: 1px solid #ccc;
  background: #fff;
}

.test:before {
  z-index: -1;
  position: absolute;
  content: "";
  bottom: 15px;
  left: 12px;
  width: 45%;
  height: 20px;
  background: #777;
  -webkit-box-shadow: 0 15px 19px #aaa;
  -moz-box-shadow: 0 15px 19px #aaa;
  box-shadow: 0 15px 19px #aaa;
  -webkit-transform: rotate(-3deg);
  -moz-transform: rotate(-3deg);
  -o-transform: rotate(-3deg);
  -ms-transform: rotate(-3deg);
  transform: rotate(-3deg);
}

.test:after {
  z-index: -1;
  position: absolute;
  content: "";
  bottom: 15px;
  right: 12px;
  width: 45%;
  height: 20px;
  background: #777;
  -webkit-box-shadow: 0 15px 19px #aaa;
  -moz-box-shadow: 0 15px 19px #aaa;
  box-shadow: 0 15px 19px #aaa;
  -webkit-transform: rotate(3deg);
  -moz-transform: rotate(3deg);
  -o-transform: rotate(3deg);
  -ms-transform: rotate(3deg);
  transform: rotate(3deg);
}
<div class="test">Without transform</div>

<div class="test" style="transform:rotate(2deg)">With transform:rotate(2deg)</div>

Upvotes: 0

Views: 38

Answers (1)

Temani Afif
Temani Afif

Reputation: 272842

It's because transform create a stacking context making the shadow to be placed inside the container instead of outside. You will face the same issue if for example you add z-index value to the container:

To fix this you can consider another way to do this like below:

.test {
  position: relative;
  margin: 20px auto;
  width: 300px;
  height: 100px;
  border: 1px solid #ccc;
  z-index: 0;
}


/*this will create the shadow*/

.test:before {
  z-index: -2;
  position: absolute;
  content: "";
  bottom: 0;
  left: 15px;
  right: 15px;
  height: 10px;
  background:
   linear-gradient(to top right, transparent 49.5%, #aaa 50%) top right/50% 100%, 
   linear-gradient(to top left, transparent 49.5%, #aaa 50%) top left/50% 100%;
  background-repeat: no-repeat;
  filter: drop-shadow(0 7px 5px #aaa);
}


/*this will be your background*/

.test:after {
  z-index: -1;
  position: absolute;
  content: "";
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: #fff;
}
<div class="test">Without transform</div>

<div class="test" style="transform:rotate(2deg)">With transform:rotate(2deg)</div>

Upvotes: 1

Related Questions