Transform CSS clip-path with mixed (fixed and percentage) values to SVG clip-path
Asked Answered
M

3

5

I have a card image with gradient with clipped corner using clip-path:

.card {
  width: 200px;
  height: 200px;
  background: linear-gradient(to bottom, blue, green);
  clip-path: polygon(20px 0, 100% 0, 100% 100%, 0 100%, 0 20px);
}
<div class="card"></div>

Clipped corner must have fixed size regardless of card size so I'm using pixels for clipping corner.

But clip-path has not the best browser support at the moment, so I've tried converting this HTML and CSS to SVG.

.container {
  width: 200px;
  height: 200px;
}
<div class="container">
  <svg viewBox="0 0 100 100" clip-path="url(#myClip)">
    <defs>
      <linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
        <stop offset="0%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
        <stop offset="100%" style="stop-color:rgb(0,255,0);stop-opacity:1" />
      </linearGradient>
    </defs>
  
    <polygon points="20,0 100,0 100,100 0,100 0,20" fill="url(#grad1)" />
 </svg>
</div>

But the issue is that I can't make this clipped corner to have fixed size regardless of card size.

Memorable answered 12/12, 2018 at 12:24 Comment(3)
the gradient will be random or defined one?Faina
@TemaniAfif It would be great if I will get solution that can work with arbitrary gradient or image. Even if it's not SVG solution but will work in every modern browser.Memorable
added a generic solutionFaina
M
1

With the help from Stack Overflow in Russian using SVG mask my solution is this

.container {
  width: 200px;
  height: 200px;
}

svg {
  width: 100%;
  height: 100%;
}
<div class="container">
  <svg>
    <defs>
      <mask id="triangle-clip">
        <rect x="0" y="0" width="100%" height="100%" fill="#fff" />
        <path d="M0,20 v-20 h20 z" fill="#000" />
      </mask>

    <linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
      <stop offset="0%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(0,255,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <rect width="100%" height="100%" fill="url(#grad1)" mask="url(#triangle-clip)" />
</svg>
</div>
Memorable answered 13/12, 2018 at 16:18 Comment(0)
A
3

To keep it fixed size, you can't use a viewBox on your SVG. Just clip the corner you need and have the other corners extend a long way so that it covers any size you might need. In the example below, I've made the clippath extend out to (10000,10000).

Here we are applying the gradient to a 100% x 100% <rect>. This is so that the gradient always scales to fit the screen. Then we apply the clippath to the rect to get the notch.

html, body {
  height: 100%;
}

.container {
  width: 50%;
  height: 50%;
}
<div class="container">
  <svg width="100%" height="100%">
    <defs>
      <linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
        <stop offset="0%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
        <stop offset="100%" style="stop-color:rgb(0,255,0);stop-opacity:1" />
      </linearGradient>
      <clipPath id="clip1">
        <polygon points="20,0 10000,0 10000,10000 0,10000 0,20"/>
      </clipPath>
    </defs>
  
    <rect width="100%" height="100%" fill="url(#grad1)" clip-path="url(#clip1)"/>
 </svg>
</div>
Antelope answered 13/12, 2018 at 0:56 Comment(2)
I've found solution using SVG mask but you have my upvote because your solution also works although looks hacky with this 10000 value. So thanks anyway!Memorable
@Paul LeBeau Very witty answer! Works in all modern browsers + EdgeHazardous
F
1

In case the gradient will always have bottom or top direction you can consider a trick using skew transform and pseudo element like below:

.card {
  width: 200px;
  height: 200px;
  padding-top: 20px;
  background-image: linear-gradient(to bottom, blue,red,yellow,green); 
  background-clip:content-box;
  background-size:100% 200px; /*same as height*/
  position: relative;
  z-index:0;
  overflow:hidden;
  box-sizing: border-box;
  display:inline-block;
}

.card:before {
  content: "";
  position: absolute;
  z-index:-1;
  top: 0;
  padding: inherit;
  left: 0;
  right: 0;
  background-image: inherit;
  background-size:inherit;
  transform: skewX(-45deg);
  transform-origin: left bottom;
}

body {
  background:pink;
}
<div class="card"></div>
<div class="card" style="background-image:linear-gradient(to top,white,purple,green ,red 90%, blue"></div>

For any gradient or any image you may add extra element to rectify the skew:

.card {
  width: 200px;
  height: 200px;
  padding-top: 20px;
  background-image: linear-gradient(to bottom, blue,red,yellow,green); 
  background-clip:content-box;
  background-size:auto 200px; /*same as height*/
  position: relative;
  z-index:0;
  overflow:hidden;
  box-sizing: border-box;
  display:inline-block;
}
.image {
  background-size:cover; /*less restriction when it comes to image*/
}


.card span,
.card span::before {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  background-image: inherit;
  background-size:inherit;
  transform-origin: left bottom;
}

.card span {
  z-index:-1;
  padding: inherit;
  transform: skewX(-45deg);
  overflow:hidden;
}
.card span:before {
   content:"";
   bottom:0;
   transform: skewX(45deg);
}

body {
  background:pink;
}
<div class="card">
<span></span>
</div>
<div class="card" style="background-image:linear-gradient(60deg,white,purple,green ,red 90%, blue)">
<span></span>
</div>

<div class="card image" style="background-image:url(https://picsum.photos/400/400?image=0)">
<span></span>
</div>

<div class="card image" style="background-image:url(https://picsum.photos/600/600?image=15)">
<span></span>
</div>
Faina answered 12/12, 2018 at 12:44 Comment(2)
If you want my feedback, then I can say that I like that it works great, so you have my upvote, but I don't like that it needs so much code so I'll wait if other options exist.Memorable
@VadimOvchinnikov yes sure, you need to wait because I am pretty sure that there is a way with SVG. Simply wait for the wizards ;) but I like hacking with CSS :pFaina
M
1

With the help from Stack Overflow in Russian using SVG mask my solution is this

.container {
  width: 200px;
  height: 200px;
}

svg {
  width: 100%;
  height: 100%;
}
<div class="container">
  <svg>
    <defs>
      <mask id="triangle-clip">
        <rect x="0" y="0" width="100%" height="100%" fill="#fff" />
        <path d="M0,20 v-20 h20 z" fill="#000" />
      </mask>

    <linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
      <stop offset="0%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(0,255,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <rect width="100%" height="100%" fill="url(#grad1)" mask="url(#triangle-clip)" />
</svg>
</div>
Memorable answered 13/12, 2018 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.