Perspective Shadow with CSS

Asked

Viewed 375 times

4

I have a 950x350 pixel div and would like to create a perspective (and blurred) shadow as shown in the code below (run in full screen):

#slider{
   width: 950px;
   height: 350px;
   background: blue;
   position: relative;
}

#slider img{
   width: 962px;
   height: 31px;
   position: absolute;
   left: -6px;
   bottom: 0px;
}
<div id="slider">
   <img src="https://i.stack.imgur.com/OGKsw.png">
</div>

I did creating an image in Photoshop and placing it below the div, but I would not use image for this, would be 1 request less. It is possible and how to get this effect using HTML/CSS?

2 answers

4


Here is a version with 3D Transform:

#slider{
  position:relative;
    width: 300px;
    height: 150px;
    background: blue;
    position: relative;
}

#slider:after {
  display:block;content:"";
  position:absolute;
  width:100%;height:30px;
  top:135px;
  border-radius:5px;
  transform: perspective(10px) translateZ(-2.5px) rotateX(5deg);
  background:#666;
  box-shadow:0 0 10px 10px #666;
  z-index:-1;
}
<div id="slider"></div>

Basically we take the after and rotate it with rotateX. The values I "kicked" quickly just to demonstrate the technique, but trying to individually change each one, gives you a good sense of how it works.

  • Well master pasta, I just included an option, but I only used rotatX and perspective on the father... I think your result is even easier to customize

  • It was cool, just made some mods and was perfect, see: https://i.stack.Imgur.com/6jWUA.png

  • If you have patience, don’t do as I did of rotating through the middle, try to pick up the axle on the "foot" of the rectangle, which gives a dry CSS

  • Although since you’re working at a fixed size, it doesn’t make much difference. If you are going to do something "responsive" (I hate this term) you need to change the center of rotation to be more "automatic" the size (without having to move in Z)

  • And worse it will be responsive rs... but I manage here, thanks for the tips.

2

Option 1

Option using shadow on an element ::after, one rotateX() to tip the shadow and a filter:blur to make the element look shaded.

#slider{
    width: 950px;
    height: 350px;
    position: relative;
    perspective: 300px;
}
#slider::after{
    content: "";
    position: absolute;
    width: 950px;
    height: 350px;
    background: blue;
    top: 0;
    left: 0;
    z-index: 1;
}
#slider::before{
    content: "";
    background: rgba(0, 0, 0, 0.5);
    width: 90%;
    height: 40px;
    position: absolute;
    bottom: -20px;
    right: 0;
    left: 0;
    margin: auto;
    border-radius: 5px;
    filter: blur(5px);
    z-index: -1;
    transform: rotateX(45deg);
}
<div id="slider"></div>


Option 2

It is also possible using a negative value for the fourth Box-shadow attribute (qurto value is known as the spread ). https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow

You can adjust these values until you get one that you like, but remember that the fourth value should always be negative to have the effect you want

#slider{
    width: 950px;
    height: 350px;
    background: blue;
    position: relative;
    box-shadow: 0 40px 20px -20px rgba(0, 0, 0, 0.5);
}
<div id="slider"></div>

  • Thanks young, but it’s not exactly what I’d like. If you notice in the shadow that I put, there’s an inward slope on the sides, a kind of "skew".

  • It is I even noticed, I am here maturing looking pa image and dining at the same time, but I think I’ll update my answer with another rss option

  • Quiet young man... dine in peace there rs.

  • @sam edited response. But I think that Bacco’s has become easier to understand rss

  • 1

    He got good young. Bacchus really got a little easier.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.