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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With