Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a partial box shadow on a skewed div

I'm trying to create a partial shadow on a skewed div, as close as I can get to this creative.

enter image description here

Right now I've been trying to do this with pseudo elements (before specifically) but I found that those elements behave strangely whenever I skew the element they are applied to. The pseudo element keeps appearing on top of my div, even though the z-index is set to -1. No matter what I do with z-index, it will stay on top of it. I want it to be behind the div it's applied to, and in front of the div below, like in the creative.

enter image description here

Here's my ::before code and a link to the codepen

CSS

/*! Shadows */
#test-title {
  position: relative;
}

#test-title:before {
  z-index: -1;
  position: absolute;
  content: "";
  bottom: 15px;
  left: 10px;
  width: 50%;
  top: 80%;
  max-width:300px;
  box-shadow: 0 15px 10px #777;
  -webkit-transform: rotate(-3deg);
  -moz-transform: rotate(-3deg);
  -o-transform: rotate(-3deg);
  -ms-transform: rotate(-3deg);
  transform: rotate(-3deg);
}

http://codepen.io/kathryncrawford/pen/WwWEma

like image 973
Kathryn Crawford Avatar asked May 16 '16 16:05

Kathryn Crawford


People also ask

How do you add a shadow to only one side?

To set a box-shadow on one side of the element, use the box-shadow property. This property has four length parameters and a color. box-shadow: h-offset v-offset blur spread color; h-offset sets the shadow horizontally.

What is the difference between box shadow and drop shadow?

The box-shadow property creates a rectangular shadow behind an element's entire box, while the drop-shadow() filter function creates a shadow that conforms to the shape (alpha channel) of the image itself.

Can we apply transform property 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 do you add a box shadow left and right?

Probably, the best way is using the CSS box-shadow property with the “inset” value. Also, use the :before and :after pseudo-elements, which are absolutely positioned on the right and left sides of the element.


1 Answers

Skew the parent then unskew the child at the same degree.

* {
  box-sizing: border-box
}
body {
  padding: 40px 0
}
section {
  width: 60vw;
  clear: both;
  overflow: hidden;
  min-height: 100px;
  margin: 0 auto;
  position: relative;
  background: #035076
}
section article {
  width: 60%;
  padding: 20px;
  color: white;
  margin: 0 auto
}
section:nth-child(even) {
  transform: skew(-45deg) translate(5vw);
  box-shadow: inset 0 0 2px 0 black;
}
section:nth-child(odd) {
  transform: skew(45deg);
}
section:nth-child(even) article {
  transform: skew(45deg) translate(5vw);
}
section:nth-child(odd) article {
  transform: skew(-45deg);
}
section:before,
section:after {
  content: '';
  position: absolute;
}
section:nth-child(even):before {
  width: 100%;
  height: 0;
  bottom: 100%;
  left: 0;
  z-index: 6;
  opacity: 1;
  transform: rotate(-10deg) translateY(-30px);
  box-shadow: 0px 0px 64px 30px rgba(0, 0, 0, 0.8);
}

section:nth-child(odd):not(:first-child):before {
  width: 100%;
  height: 0;
  bottom: 100%;
  right: 0;
  z-index: 6;
  opacity: 1;
  transform: rotate(10deg) translateY(-30px);
  box-shadow: 0px 0px 64px 30px rgba(0, 0, 0, 0.8);
}
<section>
  <article>What our clients say About Us</article>
</section>
<section>
  <article>Read More!</article>
</section>
<section>
  <article>Read More!</article>
</section>
<section>
  <article>Read More!</article>
</section>
like image 123
Gildas.Tambo Avatar answered Sep 30 '22 00:09

Gildas.Tambo