Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to generate a box-shadow that follows the shape of a clip-path polygon?

Tags:

css

Let's say I have this clip path (a triangle generated here)

-webkit-clip-path: polygon(50% 0%, 0% 100%, 100% 100%); clip-path: polygon(50% 0%, 0% 100%, 100% 100%); 

Is it possible to create a box-shadow from this clip path? Something like this:

box-shadow: 20px 25px 50px -25px #000; 
like image 641
NineCattoRules Avatar asked Nov 11 '16 17:11

NineCattoRules


People also ask

How do you shadow a clip path?

clipped Object using Clip-Path() function of CSS. Approach: We are going to create two divs, one for the main and the other for our clipped shape. Then we are going to use the drop-shadow() function to apply shadow effects.

Can we apply transform to box shadow?

Using transforms on the box-shadow (& transform ) property, we can create the illusion of an element moving closer or further away from the user.

How does box shadow work?

The box-shadow CSS property adds shadow effects around an element's frame. You can set multiple effects separated by commas. A box shadow is described by X and Y offsets relative to the element, blur and spread radius, and color.


2 Answers

You can use a filter on the containing div, try:

.container {    filter: drop-shadow(0px 10px 5px rgba(0,0,0,0.1)) } 

eg: https://plnkr.co/edit/kePuv7OLQwawPjiBLg3J?p=preview

like image 53
tpee Avatar answered Sep 26 '22 18:09

tpee


I'm assuming you mean, is it possible to create the shadow along the polygon. If so, then no. box-shadow is unfortunately only a "box", so it can't follow the clip path. It'd still apply to the rectangle of the element itself.

You could however pair it with another element that has the same clipping, but is set below it and offset and create a pseudo-shadow:

#box {    position: relative;    width: 200px;    height: 200px;  }    #content {    width: 100%;    height: 100%;    background: #3CF;    -webkit-clip-path: polygon(0 100%, 0 0, 100% 0, 80% 100%);  }    #shadow {    position: absolute;    z-index: -1;    content: "";    background: rgba(0, 0, 0, .5);    width: 200px;    height: 200px;    left: 5px;    top: 5px;    -webkit-clip-path: polygon(0 100%, 0 0, 100% 0, 80% 100%);  }
<div id="box">    <div id="content"></div>    <div id="shadow"></div>  </div>

Depending on your use-case, with some clever use of a background image, multiple borders, and/or gradients, you could make the background look between with a fading shadow and what not.

like image 39
samanime Avatar answered Sep 25 '22 18:09

samanime