finefoot
finefoot

Reputation: 11234

How to show a PNG image inside a <svg> tag?

I have a <svg> tag with viewBox="0 0 1000 1000" where I paint a polygon which draws a triangle over some parts of the 1000x1000 box. This SVG then gets scaled to width="100".

I want to add a PNG image, for example https://www.w3schools.com/html/pic_trulli.jpg, in the background of the SVG, behind the triangle, which will also get scaled in the same way.

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Hello, world!</title>
</head>

<body>

<svg viewBox="0 0 1000 1000" width="100">
<polygon points="100 100, 900 100, 900 900" />
</svg>

</body>
</html>

Upvotes: 2

Views: 3365

Answers (1)

finefoot
finefoot

Reputation: 11234

It's simply <image>: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/image

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Hello, world!</title>
</head>

<body>

<svg viewBox="0 0 1000 1000" width="100">
<image x="0" y="0" width="1000" height="1000" preserveAspectRatio="none" xlink:href="https://www.w3schools.com/html/pic_trulli.jpg" />
<polygon points="100 100, 900 100, 900 900" />
</svg>

</body>
</html>

Upvotes: 3

Related Questions