How to make an SVG mask for an image

There are some cases where you may need to “mask” an image with a shape.
There is the clip method of CSS but it is not supported by all browsers yet.

So If you have a square or round shape you can actually use an html element and place the image as a background.

But what happens when we need an irregular shape? In this case we can use an SVG.

So first step is to create your SVG shape as you would normally do. In my case the shape is as follows:

<svg xmlns="" width="232.5" preserveAspectRatio="xMinYMin meet" viewBox="0 0 232.5 277.75" class="svg"> 
 <path d="M150.3 277.75H0V0h232.5" fill="#000"/>

Note that i am using the preserveAspectRatio="xMinYMin meet" in order to have the SVG resizable.

Now all we have to do is create a pattern with the image and use that one as a fill on the path:

<defs><pattern id="svg-bg-img" patternUnits="userSpaceOnUse" width="100%" height="100%"><image xlink:href="" x="0" y="0" width="100%" height="100%"/></pattern></defs>

You may see the complete code on the example below:

See the Pen SVG mask by Lab21 (@lab21) on CodePen.