Search code examples
cssshadowbox-shadowcss-filters

How to use CSS to reproduce the shadow under a device


Is it possible to use CSS to reproduce the shadow under the device of the below image?

The following CSS and different variants don't work:

filter: drop-shadow(rgba(22, 22, 22, 0.25) 0px 12px 15px);

The problem is concentrating the shadow under the device and flattening it. The CSS above cannot make the shadow appear as if it's projected on the ground.

Codepen: https://codepen.io/Crashalot/pen/MWYzoJV

Smartphone with shadow


Solution

  • A pseudo element is better suited to get this done, see below example:

    Bottom shadow with pseudo element

    .object {
      margin: 20px;
      width: 70px;
      height: 140px;
      position: relative;
      border-style: solid;
      background: #E6E6FA;
    }
    .object:before {
      content:"";
      z-index: -1;
      position: absolute;
      bottom: -50%;
      width: inherit;
      height: inherit;
      border-radius: 40%;
      background: rgba(0,0,0,.55);  
      transform:  scaleX(1.3) scaleY(0.12);
      filter: blur(5px);
    }
    <div class="object"></div>

    Alternatively, you can use box-shadow:

    .object {
      margin: 20px;
      width: 70px;
      height: 140px;
      position: relative;
      border-style: solid;
      background: #E6E6FA;
      /* This is .shadow-lg from tailwindCSS */
      /* See https://tailwindcss.com/docs/box-shadow/ */
      box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    }
    <div class="object"></div>