Reputation: 139
I have an SVG functioning properly as a clipping path masking an image. The SVG shape has preserveAspectRatio="none" and is behaving how I want it to (full-width but maintaining a static height).
The problem is that the image I am clipping is stretching and squishing along with the SVG whereas I'd like it to behave the same as a background image with background-size: cover; (always filling the SVG, cropping if necessary, but maintaing aspect ratio).
Is it possible to do this with cross-browser compatibility (the latest Chrome, Firefox, Safari, and Internat Explorer)?
HTML:
<div class="outer-wrapper">
<div class="svg-wrapper">
<!-- hidden SVG -->
<svg class="shadow-svg">
<!-- create drop shadow -->
<filter id="drop-shadow">
<feGaussianBlur in="SourceAlpha" stdDeviation="5"/>
<feOffset dx="0" dy="0" result="offsetblur"/>
<feFlood flood-color="rgb(0,0,0)" flood-opacity="0.75"/>
<feComposite in2="offsetblur" operator="in"/>
<feMerge>
<feMergeNode/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
<!-- create shape-->
<symbol id="shadow" viewBox="0 0 100 100" style="enable-background:new 0 0 100 100;" xml:space="preserve">
<defs>
<polygon id="clipShape" points="0 0 0 260 20 260 20 210 1350 210 1350 260 1370 260 1370 0 0 0" style="filter:url(#dropshadow)"/>
</defs>
</symbol>
<!-- set shape as clipping path -->
<clipPath id="clipPath">
<use xlink:href="#clipShape" />
</clipPath>
</svg>
<!-- visible SVG -->
<svg class="shape-svg" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1370 260" enable-background="new 0 0 1370 260" xml:space="preserve" preserveAspectRatio="none">
<!-- add drop shadow -->
<use xlink:href="#shadow" />
<!-- add clipping path shape -->
<g transform="matrix(1 0 0 1 0 0)" clip-path="url(#clipPath)" >
<!-- add image inside clipping path shape -->
<image width="1370" height="260" xlink:href="https://www.unsplash.it/1370/260" transform="matrix(1 0 0 1 0 0)"></image>
</g>
</svg>
</div><!-- .svg-wrapper -->
<!-- text block -->
<div class="header-text">
<h2>Test Header Placeholder</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Velit exercitationem quibusdam sed natus blanditiis quia assumenda, magni tenetur veritatis itaque iure explicabo veniam maiores magnam architecto et corporis possimus.</p>
</div>
</div><!-- .outer-wrapper -->
SCSS:
* {
margin: 0;
padding: 0;
}
body {
height: 100vh;
background: repeating-linear-gradient(-45deg, palegreen, palegreen 3px, lightgreen 3px, lightgreen 40px);
}
.outer-wrapper {
display: block;
width: 100%;
margin: 0 auto;
}
.svg-wrapper {
width: 96%;
height: 150px;
margin: 0 auto;
}
.shadow-svg {
width: 0;
height: 0;
position: absolute;
}
.shape-svg {
width: 100%;
height: 150px;
filter: url(#drop-shadow);
}
.header-text {
width: 90%;
margin: -20px auto 0;
font-family: monaco, courier;
}
Note: it's currently not working in Firefox or IE11. I'd appreciate any assistance in getting all this working across those browsers as well.
Thank you.
Upvotes: 1
Views: 1042
Reputation: 321
instead of using tag, you can use mask properties in CSS. It acts similarly like background properties.
I tried this one and solves the problem. https://clipping-masking.webflow.io/
.masked {
mask-image: url(domain.com/image.sv ), none;
mask-size: contain;
mask-repeat: no-repeat;
mask-position: center;
}
Upvotes: 0
Reputation: 5144
About the aspect ratio
You could just set the svg as a background-image of a div. And style it with css; background-size: cover;
and background-position: center bottom;
will probably do the trick.
Here is an answer of the question "why it doesn't it work in MS-Edge?".
<defs>
element. <svg>
element always works. Source: https://www.w3.org/TR/SVG/struct.html#DefsElement<clipPath>
element.
The <defs>
element ended up looking like this for me and is a child of the <svg>
element.<defs>
<!-- set shape as clipping path -->
<clipPath id="clipPath">
<polygon points="0 0 0 260 20 260 20 210 1350 210 1350 260 1370 260 1370 0 0 0"/>
</clipPath>
</defs>
Upvotes: 0