Chapter 14: SVG image

What does image actually do?

It references an external image file and draws it inside the SVG coordinate system — just like placing a sticker on your drawing.

Important facts (2025–2026 reality):

  • Browsers must support JPEG, PNG, and SVG files inside <image>
  • They may support others (WebP, GIF, etc.), but don’t rely on it — stick to JPG/PNG/SVG for safety
  • The embedded image is not turned into vectors — it stays raster (can pixelate when zoomed too much)
  • But because it’s inside SVG, you can still transform, clip, mask, filter, and animate it with SVG power

Core attributes — the ones you almost always need

Attribute Meaning Required? Default Common values / notes
href The URL/source of the image Yes “photo.jpg”, “icons/logo.svg”, “#local-def-id”
x Left position (top-left corner of image) No 0 number, %, etc.
y Top position No 0 number, %, etc.
width Displayed width Yes 0 (→ invisible) number, %, “auto” (SVG 2, spotty support)
height Displayed height Yes 0 (→ invisible) number, %, “auto”
preserveAspectRatio How to handle different aspect ratios No “xMidYMid meet” “xMidYMid slice”, “none”, etc. (like object-fit)
crossorigin CORS setting (for tainted canvas / filters) No “anonymous”, “use-credentials”

Modern note: Use href (SVG 2). Older code still uses xlink:href — both work in 2026, but prefer href.

If you forget width or height → image usually disappears (size = 0 × 0).

Here are some clear diagrams showing how x, y, width, height position the image rectangle:

Example 1 – Basic photo inside SVG

HTML

Result: A nice photo placed 50 units from left/top, sized to 400×300.

(Use your own image URL or local path like “my-photo.jpg”.)

Example 2 – Preserve aspect ratio + cropping (like object-fit: cover)

HTML

Try changing to “xMidYMid meet” (letterbox / contain) vs “none” (stretch / ignore ratio).

Here are visual examples of preserveAspectRatio values:

Example 3 – Embedding another SVG file (very powerful)

HTML

→ The embedded SVG stays vector — sharp at any zoom!

Example 4 – Clipping / masking an image with SVG shapes

HTML

→ Image is clipped to a perfect circle — very common for avatars.

Quick comparison: Ways to show images involving SVG

Method When to use Pros Cons
<img src=”file.svg”> Simple standalone SVG Easy, cacheable, lazy-load Limited interactivity / CSS control
Inline <svg> with shapes Pure vector drawings Full control, style with CSS, small file Code longer for complex art
<image> inside SVG Raster photo + vector overlays / effects Filters, clip, mask, animate raster Raster can pixelate, bigger file if photo
<foreignObject> Real HTML/CSS inside SVG Full HTML content Heavy, accessibility issues sometimes

Common beginner mistakes

  • Forget width & height → image invisible
  • Wrong path in href → blank space
  • Using very large photos without sizing → huge SVG file / slow load
  • Expecting raster image to stay sharp forever → no, only vector parts do
  • CORS issues with filters/masks → add crossorigin=”anonymous” on external images

Mini practice for you

  1. Place any photo of a person and clip it to a heart shape (use <clipPath> with heart <path>)
  2. Embed a small SVG icon (like a logo) in the corner of a larger photo
  3. Make a polaroid-style frame: <rect> border + shadow filter + centered photo

Paste your code if you want feedback — I’ll review it like we’re sitting together 😄

Any part confusing? The preserveAspectRatio options? Clipping? Embedding SVG vs raster? Just tell me — we’ll zoom in with more examples until it clicks! 🚀

You may also like...

Leave a Reply

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