Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

box shadows on multiple elements at same level but without overlap?

I want to create something like the following screenshot, but I can't figure out any z-index value for which the shadow doesn't appear either over the first or second box (they are always stacked either with the first one on top, or the second). Overlapping boxes

Is there a way to achieve the following?

Desired - no overlap

body { background: darkgrey; padding-top: 50px}
div { background: white; width: 200px; height: 200px; box-shadow: 0 0 20px 
black; margin: auto; position: relative; }
#box-one { left: -50px; z-index: 1; }
#box-two { right: -50px; z-index: 1; }

https://codepen.io/eoghanmurray/pen/oVEEVK

like image 297
EoghanM Avatar asked Mar 14 '19 15:03

EoghanM


People also ask

Can you have multiple box shadows in CSS?

You can comma separate box-shadow any many times as you like.

Can you specify more than one box shadow to an element?

If a border-radius is specified on the element with a box shadow, the box shadow takes on the same rounded corners. The z-ordering of multiple box shadows is the same as multiple text shadows (the first specified shadow is on top). Box-shadow generator is an interactive tool allowing you to generate a box-shadow .

How do you add a shadow to only one side?

To apply a shadow effect only on one side of an element set the blur value to a positive number and set the spread value to the same size but with a negative sign. Depending on which side you want the shadow on, set the offset value as follows: Top shadow: offset-x: 0 and offset-y: -5px.


2 Answers

If you can use filter and drop-shadow then you can apply a drop-shadow to the container. This shadow differs as it conforms to the alpha channel of the image (in this case, the outline of the content) instead of a simple rectangle:

body {
  background: darkgrey;
  padding-top: 50px
}

#box-one,
#box-two {
  background: white;
  width: 200px;
  height: 200px;
  margin: auto;
  position: relative;
}

#box-one {
  left: -50px;
  z-index: 1;
}

#box-two {
  right: -50px;
  z-index: 1;
}

#top {
  filter: drop-shadow(0 0 20px black);
}
<div id="top">
  <div id="box-one"></div>
  <div id="box-two"></div>
</div>
like image 180
chazsolo Avatar answered Sep 16 '22 16:09

chazsolo


You can consider drop-shadow filter on a parent element:

body {
  background: pink;
}

.b1,
.b2 {
  width: 150px;
  height: 150px;
  background: #fff;
}

.b2 {
  margin-left: 100px;
}
.container {
  filter:drop-shadow(0 0 10px #000);
}
<div class="container">
  <div class="b1"></div>
  <div class="b2"></div>
</div>

Or use an extra element to hide the overlapping shadows:

body {
  background: pink;
}

.b1,
.b2 {
  width: 150px;
  height: 150px;
  background: #fff;
  box-shadow: 0 0 13px #000;
  position: relative;
}

.b2 {
  margin-left: 100px;
}

.b1:before,
.b2:before {
  content: "";
  position: absolute;
  bottom: 0px;
  right: 0;
  left: 0;
  height: 15px;
  background: inherit;
  z-index: 1;
}

.b2:before {
  top: 0;
  bottom: initial;
}
<div class="container">
  <div class="b1"></div>
  <div class="b2"></div>
</div>

You can also build this using only one element:

body {
  background: pink;
}
.container {
  width:250px;
  height:300px;
  background:
     linear-gradient(#fff,#fff) top left,
     linear-gradient(#fff,#fff) bottom right;
   background-size:150px 150px;
  background-repeat:no-repeat;
  filter:drop-shadow(0 0 10px #000);
}
<div class="container">
</div>
like image 30
Temani Afif Avatar answered Sep 20 '22 16:09

Temani Afif