filling svg background with an image

Abi Saunders

I have created a svg heart with an image as a pattern inside. I am trying to make it so the image fits the whole heart but I am not having much luck.

Any help would be great and hugely appreciated.

svg {
  width: 300px;
  border: 1px solid grey;
  margin: 1em auto;
  display: block;
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 315 345">

    <!-- START SVG RULES -->
    <defs>
        <!-- DEFINE IMAGE INSIDE PATTERN -->
        <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
            <image xlink:href="https://images.pexels.com/photos/325185/pexels-photo-325185.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"
                   x="0" y="0" width="100" height="100" />
        </pattern>

        <!-- SVG SHAPE CREATION -->
        <g id="heart">
            <path d="M0 200 v-200 h200
                     a100,100 90 0,1 0,200
                     a100,100 90 0,1 -200,0
                     z" />
        </g>
    </defs>

    <use xlink:href="#heart" class="outline" fill="url(#img1)"  />
</svg>

Paul LeBeau

The simplest solution is to use patternContentUnits="objectBoundingBox" and set the image width and height to "1".

Then to make the image fill the pattern, set preserveAspectRatio="xMidYMid slice". This is equivalent to the CSS's background-size: cover

svg {
  width: 300px;
  border: 1px solid grey;
  margin: 1em auto;
  display: block;
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 315 345">
        
     <!-- START SVG RULES -->
    <defs>
        <!-- DEFINE IMAGE INSIDE PATTERN -->
        <pattern id="img1" patternContentUnits="objectBoundingBox" width="100%" height="100%">
            <image xlink:href="https://images.pexels.com/photos/325185/pexels-photo-325185.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"
                   x="0" y="0" width="1" height="1" preserveAspectRatio="xMidYMid slice" />
        </pattern>
        
        <!-- SVG SHAPE CREATION -->
        <g id="heart">
            <path
                d="M0 200 v-200 h200
                  a100,100 90 0,1 0,200
                  a100,100 90 0,1 -200,0
                  z" />
        </g>
    </defs>
    <use xlink:href="#heart" class="outline" fill="url(#img1)"  />
</svg>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SVG path drawing and filling with image

From Dev

SVG path drawing and filling with image

From Dev

Background image not completely filling div

From Dev

SVG as background image

From Dev

SVG as background image

From Dev

Filling a round SVG element with an image in javascript

From Dev

Filling a round SVG element with an image in javascript

From Dev

Filling a rotating circular SVG with a pattern image

From Dev

Background Image filling and resizing to browser window

From Dev

Appending svg into a background-image

From Dev

PHP Generated SVG as background image

From Dev

SVG background image not appearing on iPad

From Dev

Rotating stripes in SVG background image

From Dev

SVG triangle separator with image background

From Dev

Background SVG image inline not displayed

From Dev

SVG triangle separator with image background

From Dev

Background image not zooming and panning with SVG

From Dev

Responsive svg clipPath with background image

From Dev

Using WebP as a background image in SVG

From Dev

Bootstrap background image not filling up space when mobile

From Dev

Cover background not filling screen when screen is bigger than image

From Dev

SVG background image isn't displaying

From Dev

Apply SVG Gradient filter to background image

From Dev

SVG as inline background image of a Div is not working

From Dev

SVG Stripe Pattern as Element Background-Image

From Dev

Polymer SVG as CSS background-image not working

From Dev

Setting a css background image to an inline SVG symbol?

From Dev

setting background image to dynamic svg data?

From Dev

Make css background image (svg) responsive