Add a background image (.png) to a SVG circle shape
An image fill for an svg element is achieved through SVG Patterns...
<svg width="700" height="660">
<defs>
<pattern id="image" x="0" y="0" patternUnits="userSpaceOnUse" height="1" width="1">
<image x="0" y="0" xlink:href="url.png"></image>
</pattern>
</defs>
<circle id='top' cx="180" cy="120" r="80" fill="url(#image)"/>
</svg>
How to add different background image (.png) to a SVG circle shape , and set stroke?
Use objectBoundingBox in patternUnits
attribute value instead of userSpaceOnUse. Also you need to declare xlink
namespace. Thus, try to use following:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="700" height="660"> <defs> <pattern id="image" x="0" y="0" patternUnits="objectBoundingBox" height="1" width="1"> <image x="0" y="0" xlink:href="https://www.gravatar.com/avatar/31497e3c546c438c4eea4b68d6f9f027?s=32&d=identicon&r=PG&f=1"></image> </pattern> <pattern id="image2" x="0" y="0" patternUnits="objectBoundingBox" height="1" width="1"> <image x="0" y="0" xlink:href="https://lh5.googleusercontent.com/-x_BhuHcC8Ww/AAAAAAAAAAI/AAAAAAAAABg/hiPWIjRXbhI/photo.jpg?sz=64"></image> </pattern> </defs> <circle cx = "20%" cy = "20%" r = "20" fill = "url(#image)" stroke-width ="2px" stroke="red"/> <circle cx = "40%" cy = "20%" r = "20" fill = "url(#image2)" stroke-width ="2px" stroke="red"/></svg>
SVG image inside circle
A pattern will work. You just have to give the <image>
a size. Unlike HTML, SVG images default to width and height of zero.
Also, if you want the image to scale with the circle, then you should specify a viewBox
for the pattern.
<svg id="graph" width="100%" height="400px">
<!-- pattern --> <defs> <pattern id="image" x="0%" y="0%" height="100%" width="100%" viewBox="0 0 512 512"> <image x="0%" y="0%" width="512" height="512" xlink:href="https://cdn3.iconfinder.com/data/icons/people-professions/512/Baby-512.png"></image> </pattern> </defs> <circle id="sd" class="medium" cx="5%" cy="40%" r="5%" fill="url(#image)" stroke="lightblue" stroke-width="0.5%" /></svg>
How to add a border to an image inside an SVG circle
You could change the radius on your clipPath circle to be a little bit less than the other circle and use a square image so it fits perfectly.
render() {
return (
<svg id='svg1' viewBox='0 0 48 48'>
<defs>
<clipPath id='circleView'>
<circle cx='24' cy='24' r='18' fill='none' stroke='#FF62E1' strokeWidth='2' />
</clipPath>
</defs>
<image
x="0"
y="0"
width='48'
height='48'
xlinkHref={'https://source.unsplash.com/random/1500x1500/'}
clipPath='url(#circleView)'
/>
<circle
cx='24'
cy='24'
r='22'
fill='none'
stroke='#FF62E1'
strokeWidth='2'
// @ts-ignore
ref={(circle) => { this.circle = circle }} >
</circle>
</svg>
)
}
}
Codepen
This option leaves a transparent gap where the background can be seen, so may or may not be exactly what you want. The alternative is to create another circle with a stroke but no fill on top with a different radius again.
Alternative Codepen
Centered background image within SVG
Using a pattern fill is the way to go - if your image aspect ratio is square it will work nicely. Notice the patterns width and height are the same as your rectangle. It's important in this case to set the patternUnits
to userSpaceOnUse
which for simplicity keeps the pattern fill from scaling to the objects bounding box.
.body { width: 100%; height: 100%; background: #1f1b33; padding: 2rem;}
svg { width: 120px;}
<div class="body"> <svg viewBox="0 0 121.375 125.397"> <defs> <pattern id="patt" patternUnits="userSpaceOnUse" width="97" height="97"> <image xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/17496/flace.jpg" width="97" height="97"></image> </pattern> </defs> <g id="blank" transform="translate(-1460.94 -927.887)" > <path id="wrapper" data-name="Caminho 2257" d="M80.617,78.013C67.787,91.6,11.606,90.985-8.689,69.872s-20.527-73.915,0-90.659S66.834-47.3,87.13-26.188,93.447,64.424,80.617,78.013Z" transform="translate(1484.938 966.119)" fill="#fff" opacity="0.2" /> <rect id="within" data-name="Retângulo 365" width="97" height="97" rx="37" transform="translate(1475 945)" fill="url(#patt)" /> </g> </svg></div>
No display of image as a pattern in SVG circle
<svg width="260" height="120"> <defs> <pattern id="Triangle" width="10" height="10" patternUnits="userSpaceOnUse"> <polygon points="5,0 10,10 0,10" /> </pattern> <pattern id="Img" width="100" height="100" patternUnits="userSpaceOnUse"> <image x="0" y="0" width="100%" height="100%" xlink:href="https://upload.wikimedia.org/wikipedia/commons/4/46/A_Meat_Stall_with_the_Holy_Family_Giving_Alms_-_Pieter_Aertsen_-_Google_Cultural_Institute.jpg"></image> </pattern> </defs>
<circle cx="60" cy="60" r="60" fill="url(#Img)" stroke="red" /> <circle cx="200" cy="60" r="60" fill="url(#Triangle)" stroke="red" /></svg>
Fill SVG path element with a background image without tiling or scaling
Just make the x
, y
, width
and height
of the pattern match the bounding box of your path. You can just use "0","0","1" & "1" respectively here because the patternUnits
defaults to objectBoundingBox
, so the units are expressed relative to the bounding box. Then make the image in your pattern have the width
and height
of the path bounding box also. This time, you'll need to use "real" sizes though.
The image will be centred in the pattern automatically because the default value of preserveAspectRatio
for <image>
does exactly what you want.
<svg width="600" height="600"> <defs> <pattern id="imgpattern" x="0" y="0" width="1" height="1"> <image width="120" height="250" xlink:href="http://lorempixel.com/animals/120/250/"/> </pattern> </defs> <path fill="url(#imgpattern)" stroke="black" stroke-width="4" d="M 100,50 L 120,110 150,90 170,220 70,300 50,250 50,200 70,100 50,70 Z" />
</svg>
Fill SVG path element with a background-image
You can do it by making the background into a pattern:
<defs>
<pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
<image href="wall.jpg" x="0" y="0" width="100" height="100" />
</pattern>
</defs>
Adjust the width and height according to your image, then reference it from the path like this:
<path d="M5,50
l0,100 l100,0 l0,-100 l-100,0
M215,100
a50,50 0 1 1 -100,0 50,50 0 1 1 100,0
M265,50
l50,100 l-100,0 l50,-100
z"
fill="url(#img1)" />
Working example
Related Topics
Twitter Bootstrap 3 Sticky Footer
How to Assign Multiple Classes to an HTML Container
Border Length Smaller Than Div Width
Two Inline-Block Elements, Each 50% Wide, Do Not Fit Side by Side in a Single Row
Twitter Bootstrap 3, Vertically Center Content
What Characters Are Valid in a Url
Is It Bad to Use !Important in a CSS Property
How to Use CSS to Surround a Number With a Circle
How to Remove Underline from a Link in Html
Is Autocomplete="Off" Compatible With All Modern Browsers