Search code examples
htmlcssimagesvgbackground-image

fill SVG path with pattern without stretching


I'm trying to use edit this example and fill the wave with a pattern

<svg id="shape-overlays" class="shape-overlays" viewBox="0 0 100 100" preserveAspectRatio="none">

                <defs>
                    <pattern id="img4" patternUnits="userSpaceOnUse" width="180" height="180">
                        <image xlink:href="./pattern/menu-3.png" x="0" y="0" width="180" height="180" />
                    </pattern>
                </defs>


                <path class="shape-overlays__path"></path>
                <path class="shape-overlays__path"></path>
                <path class="shape-overlays__path"></path>
                <path class="shape-overlays__path"></path>
            </svg>

CSS

.shape-overlays__path:nth-of-type(4) {
 fill: url(#img4);
 stroke: red;
 stroke-width: 1;
}

the SVG path is filled with pattern image but isn't repeated and is also stretched

I would like to have it repeated as a pattern, if is not possible I would like to use a big image as cover but keeping proportion


Solution

  • to solve the issue I changed preserveAspectRatio to "xMidYMid slice" instead of "none"

    <svg id="shape-overlays" class="shape-overlays" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" >