Chapter 15: SVG image

SVG Image (the <image> tag) means embedding a raster picture (like JPG, PNG, or even another SVG file) inside your SVG document. It’s like putting a photo or logo inside your vector drawing so you can:

  • Mix vector shapes (circles, paths) with real photos
  • Apply SVG effects (filters, clips, masks, gradients, transforms) to the photo
  • Position, scale, and rotate the photo as part of your SVG
  • Keep everything vector-sharp around it, while the photo itself is raster (pixels)

Important: This is not the same as using <img src=”photo.jpg”> in HTML. Here, <image> lives inside <svg>…</svg> and becomes part of the SVG canvas.

1. Why Use <image> in SVG? (Real Reasons)

  • Add a photo background then draw vector shapes/text on top
  • Apply blur, shadow, or color filters to a photo using SVG only
  • Rotate/clip a photo inside a circle (like profile pic mask)
  • Mix vector icons with real product photos in ads/infographics
  • Embed small icons/photos without extra HTTP requests (if base64, but usually external)

Note: Browsers must support JPEG/PNG/SVG inside <image>. Almost all do in 2026.

2. Basic Syntax of <image>

XML
  • Self-closing tag (/>)
  • Must be inside <svg>…</svg>
  • href = modern way (was xlink:href in old code — use href now)
  • width & heightrequired — no size = invisible!
  • x/y default to 0 (top-left of SVG)

3. Your First SVG <image> Example (Copy-Paste Now!)

Save as svg-image.html:

HTML

What you see: The MDN logo photo appears inside the SVG canvas at position (50,50), sized 300×200.

Breakdown:

  • href=”…” → points to external image (can be relative path too, like “myphoto.jpg”)
  • x=”50″ y=”50″ → starts 50 units from left/top
  • width/height → forces display size (can stretch if not matching original ratio)

Zoom browser — photo becomes pixelated when big (raster), but SVG container stays sharp!

4. All Important Attributes (Full Notes Style)

Attribute Meaning Required? Default Example values Notes
href (or xlink:href) Source URL of image Yes “photo.png”, “https://…” Modern: href; old: xlink:href
x Horizontal position (left) No 0 20, 100, 10% From SVG left
y Vertical position (top) No 0 30, 80 From SVG top
width Display width Yes 200, 50%, 128px Must have!
height Display height Yes 150, 40% Must have!
preserveAspectRatio How to handle aspect ratio mismatch No xMidYMid meet “none”, “xMinYMin slice” Controls scaling/cropping
crossorigin CORS for external images No anonymous “anonymous”, “use-credentials” For tainted canvas/security

5. More Real & Fun Examples (Try These!)

Example 1: Photo with preserveAspectRatio (No Distortion)

XML

→ Centers photo, crops edges if ratio doesn’t match (like background-cover in CSS)

Example 2: Rotate + Clip Photo (Circle Mask)

XML

→ Photo clipped to circle (great for avatars!)

Example 3: Apply Filter (Blur on Photo)

XML

→ Blurs the embedded photo

Example 4: Nested SVG (Another SVG as Image)

XML

→ Embed vector SVG inside your main SVG (reusable icons!)

6. Teacher’s Tips (Hyderabad Student Style 😄)

  • Use external URLs or relative paths — avoid big base64 unless small icon (makes file huge)
  • preserveAspectRatio=”none” → stretch to fit (like object-fit: fill)
  • Common mistake: Forget width/height → photo invisible!
  • Performance: External images = extra HTTP request; inline SVG better for small ones
  • Accessibility: Add <title> or <desc> inside <image> for screen readers
  • Job/freelance: <image> great for product mockups, photo filters in ads, hybrid vector+raster designs

Got SVG <image> clear now? It’s how you bring real photos into the vector world! Want next: Base64 embedded image? More filter examples? Or <foreignObject> for HTML inside SVG? Just say — we’re almost done with core SVG elements! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *