Only CSS rotate box-shadow without original element
Asked Answered
H

3

6

I have a small problem, I want to create 45 degree shadow for a picture. But if I use my code my object is rotating too. So I would like to ask for help with this problem.

My code:

.item {
    box-shadow: -50px 80px 4px 10px #555;
    -webkit-transform: rotate(10deg);
    -moz-transform: rotate(10deg);
    -o-transform: rotate(10deg);
    -ms-transform: rotate(10deg);
    transform: rotate(10deg);
}
Hypolimnion answered 11/8, 2015 at 12:2 Comment(5)
Use a pseudo-element with box-shadow and rotate it instead of the container element.Uncouth
But is it possible to create pseudo element with only CSS?Hypolimnion
You can use ::after or ::before, more pseudo element can be found here: w3schools.com/css/css_pseudo_elements.aspAid
Pseudo-elements are created through CSS :) Please have a look at this MDN link.Uncouth
Thanks You guys, i always thought they wew HTML related. Thanks againHypolimnion
A
10

Most flexible answer using only CSS is probably this:

.item {
    position: relative; /* or absolute */
}

.item:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: transparent;

    box-shadow: -50px 80px 4px 10px #555;
    -webkit-transform: rotate(10deg);
    -moz-transform: rotate(10deg);
    -o-transform: rotate(10deg);
    -ms-transform: rotate(10deg);
    transform: rotate(10deg);
}
Anatollo answered 11/8, 2015 at 13:18 Comment(0)
A
4

You can do it using peudo-element (I've used arbitrary values, you need to tweak it yourself) :

.item {
  margin-left: 50%;
  position: relative;
  width: 100px;
  height: 100px;
  background-color: red;
}
.item:before {
  z-index: -1;
  position: absolute;
  content: "";
  display: block;
  width: 100px;
  height: 100px;
  top: -30px;
  left: 0;
  background-color: transparent;
  box-shadow: -50px 120px 4px 10px #555;
  -webkit-transform: rotate(10deg);
  -moz-transform: rotate(10deg);
  -o-transform: rotate(10deg);
  -ms-transform: rotate(10deg);
  transform: rotate(10deg);
}
<div class="item"></div>
Ankylosaur answered 11/8, 2015 at 12:11 Comment(0)
J
0

Well in my case i was already using the pseudo elements ::before and ::after of that element that i wanted a rotating shadow as well... so i had to do an animation with box-shadow keyframes. Works very well.

Junji answered 12/11, 2023 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.