Search code examples
cssclip-pathborder-radius

How to keep border-radius when using clip-path?


I have a project where I need to create a certain type of card. These cards contain a gradient background when hovered over. I have added a before item that is shown when the item is hovered over. In order to contain the background gradient within the card I have added a clip path attribute to the card.

But the problem I am facing is that the clip path doesn't clip the rounded border border-radius: 0.5rem;. I have searched and found that it is possible by using a polygon to clip path.

I have found a link where you can generate poly items to clip to: Clip poly generator.

But i cannot find a perfect polygon that fits. The circle is to rounded and the bevel item only has sharp borders.

body{
  background-color: black;
  }

.card-oval-gradient {
  position: relative;
  width: 10rem;
  height: 6rem;
  background-color: blue;
  border-radius: 0.5rem;
  clip-path: inset(0);
}

.card-oval-gradient > * {
  position: relative;
  z-index: 2;
}

.card-oval-gradient:hover::before {
  content: '';

  position: absolute;
  left: -7rem;
  top: -10rem;
  width: 18rem;
  height: 28rem;
  -moz-border-radius: 15rem / 20rem;
  -webkit-border-radius: 15rem / 20rem;
  border-radius: 15rem / 20rem;
  opacity: 0.6;
  transform: scaleX(-1) rotate(61deg);
  background-image: linear-gradient(
    138deg,
    #4d3d8f 0%,
    #df67ed 23%,
    #e24c26 65%,
    #f18823 84%,
    #3aa6c2 100%
  );

  filter: blur(50px);
}
<div class="card-oval-gradient">
</div>


Solution

  • Use mask not clip-path:

    body{
      background-color: black;
      }
    
    .card-oval-gradient {
      position: relative;
      width: 10rem;
      height: 6rem;
      background-color: blue;
      border-radius: 0.5rem;
      -webkit-mask: linear-gradient(#fff 0 0);
      mask: linear-gradient(#fff 0 0);
    }
    
    .card-oval-gradient > * {
      position: relative;
      z-index: 2;
    }
    
    .card-oval-gradient:hover::before {
      content: '';
    
      position: absolute;
      left: -7rem;
      top: -10rem;
      width: 18rem;
      height: 28rem;
      -moz-border-radius: 15rem / 20rem;
      -webkit-border-radius: 15rem / 20rem;
      border-radius: 15rem / 20rem;
      opacity: 0.6;
      transform: scaleX(-1) rotate(61deg);
      background-image: linear-gradient(
        138deg,
        #4d3d8f 0%,
        #df67ed 23%,
        #e24c26 65%,
        #f18823 84%,
        #3aa6c2 100%
      );
    
      filter: blur(50px);
    }
    <div class="card-oval-gradient">
    </div>