Search code examples
cssbox-shadow

is it possible to do non-overlapping shadows?


If you put your mouse over the pill-shaped thing, It'll move over the circle (what I want to happen), but the shadow of the circle will remain visible. I want the shadows to not affect each other, but still be transparent.

body {
  background-color: aqua;
}
#circle1, #circle2 {
  position: relative;
  float: left;
  height: 50px;
  width: 50px;
  border-radius: 25px;
  margin-left: 8px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  z-index: 0;
  background-color: white;
}
#pill1, #pill2 {
  position: relative;
  float: left;
  height: 50px;
  width: 100px;
  border-radius: 25px;
  margin-left: 8px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  z-index: 1;
  background-color: white;
}
#pill2 {
  box-shadow: 0 4px 8px 0 rgba(180, 180, 180, 1), 0 6px 20px 0 rgba(200, 200, 200, 1);
}
#circle2 {
  box-shadow: 0 4px 8px 0 rgba(180, 180, 180, 1), 0 6px 20px 0 rgba(200, 200, 200, 1);
}
@keyframes animation {
  0% {right: 0px;}
  100% {right: 58px;}
}
#pill1:hover, #pill2:hover {
  animation: animation 0.5s linear forwards;
}
<div id="circle1"></div>
<div id="pill1"></div>
<div id="circle2"></div>
<div id="pill2"></div>

I forgot to mention something, it's supposed to be an animation. I put it in here as a transition but it wasn't working. I just fixed it, so it's now an animation.

The one on the right is what I want to happen, but that is max opacity.


Solution

  • You may remove the shadow on hover of the circle and make its z-index higher than the pill to catch its hover state:

    body {
      background-color: aqua;
    }
    #circle {
      position: relative;
      float: left;
      height: 50px;
      width: 50px;
      border-radius: 25px;
      margin-left: 8px;
      box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
      z-index: 2;
      background-color: white;
    }
    #pill {
      position: relative;
      float: left;
      height: 50px;
      width: 100px;
      border-radius: 25px;
      margin-left: 8px;
      box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
      z-index: 1;
      background-color: white;
    }
    #circle:hover + #pill, #pill:hover {
      right: 58px;
    }
    #circle:hover {
      box-shadow:none;
    }
    /*Added this to avoid the issue of hovering only the pill and not the circle*/
    #circle:hover::after {
      content:"";
      position:absolute;
      top:0;
      bottom:0;
      left:0;
      width:100px;
    }
    <div id="circle"></div>
    <div id="pill"></div>

    UPDATE

    By the way you can simplify your code like this:

    I also included the transition

    body {
      background-color: aqua;
    }
    #circle {
      position: relative;
      height: 50px;
      width: 50px;
      border-radius: 25px;
      margin-left: 8px;
      box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
      background-color: white;
      transition:all 0.5s;
    }
    #circle:before {
      content:"";
      position: absolute;
      top:0;
      left:80px;
      height: 100%;
      width: 100px;
      border-radius: 25px;
      box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
      background-color: white;
      transition:all 0.5s;
    }
    #circle:hover::before {
      left:0;
    }
    #circle:hover {
      box-shadow:none;
    }
    /*Added this to avoid glitchs*/
    #circle:after {
      content:"";
      position:absolute;
      top:0;
      bottom:0;
      left:0;
      width:200px;
    }
    
    @keyframes animation {
      0% {left: 80px;}
      100% {left: 0;}
    }
    <div id="circle"></div>