Perspective shadow with CSS

4

I have a div of 950x350 pixels 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 created an image in Photoshop and put it below the div, but I would not use an image for it, it would be 1 requisition less. Is it possible and how do I get this effect using HTML / CSS?

    
asked by anonymous 25.09.2018 / 00:49

2 answers

4

Following 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>

We basically got the after and rotated with rotateX. The values I "kicked" quickly just to demonstrate the technique, but trying to individually change each one, gives a good idea of how it works.

    
25.09.2018 / 01:10
2

Option 1

Option using shadow on a ::after element, a rotateX() to tilt 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 to use a negative value for the fourth Box-shadow attribute (the fourth value is known as spread ). link

You can adjust these values until you reach a more pleasing one, but remember that the fourth value should always be negative to have the effect that 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>
    
25.09.2018 / 00:57