How to embed SVG in HTML with example Code

How to embed SVG in HTML with example Code

Three ways to embed SVG in HTML

Scalable Vector Graphics (SVG) are XML-based images that stay sharp at any resolution. You can add them to your web pages using standard image tags, external objects, or by pasting the code directly into your markup. I usually prefer inlining them when I need to change their colors with CSS, but using the image tag is cleaner for static icons.

  1. Use the img tag for simple icons

    The simplest way to embed an SVG is the <img> tag. This treats the SVG like a standard JPEG or PNG. It is the best choice for performance because browsers can cache the external file.

    <!DOCTYPE html>
    <html>
    <body>
      <!-- Using a standard image tag -->
      <img src="icon.svg" alt="Description of SVG" width="100" height="100">
    </body>
    </html>
  2. Embed inline SVG for CSS control

    When you paste the <svg> code directly into your HTML, you gain the ability to style individual paths with CSS. This is how you create hover effects or change icon colors dynamically. A common mistake is forgetting the viewBox attribute; without it, your SVG might not scale correctly when you change the width or height in your stylesheet.

    <!DOCTYPE html>
    <html>
    <head>
    <style>
      .logo-path {
        fill: blue;
        transition: fill 0.3s;
      }
      .logo-path:hover {
        fill: red;
      }
    </style>
    </head>
    <body>
      <!-- Inline SVG allows CSS styling -->
      <svg width="100" height="100" viewBox="0 0 100 100">
        <circle class="logo-path" cx="50" cy="50" r="40" />
      </svg>
    </body>
    </html>
  3. Use the object tag for interactive scripts

    If your SVG contains its own internal scripts or complex interactions, the <object> tag is the right tool. It maintains the SVG as a separate document but allows it to be part of your page layout. If the SVG fails to load, the text inside the tag provides a fallback.

    <!DOCTYPE html>
    <html>
    <body>
      <!-- Using the object tag with fallback text -->
      <object data="graphic.svg" type="image/svg+xml">
        Your browser does not support SVG.
      </object>
    </body>
    </html>

A complete working example

This example combines the inline method with CSS to show how a single SVG element can be styled and scaled. I am using a simple square path here. You can copy this entire block into an index.html file to see it work. It demonstrates the use of viewBox to ensure the shape stays proportional.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>SVG Embedding Example</title>
  <style>
    .container {
      display: flex;
      gap: 20px;
      padding: 50px;
    }
    .box {
      fill: #4CAF50;
      stroke: #333;
      stroke-width: 2;
    }
    .box:hover {
      fill: #FF5722;
    }
  </style>
</head>
<body>

  <div class="container">
    <!-- The SVG is embedded directly -->
    <svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
      <rect class="box" x="10" y="10" width="80" height="80" />
    </svg>
    
    <p>Hover over the square to see the CSS transition in action.</p>
  </div>

</body>
</html>
<!-- Expected output: A green square that turns orange when hovered. -->

When you use the inline method, make sure you remove the <?xml version="1.0"?> declaration if you are copying code from a file generated by design software like Illustrator or Figma. That declaration is only needed for standalone files and can sometimes cause parsing issues in older browsers when kept inside HTML5 documents.

Posted in: HTML

Comments 0

No comments yet. Be the first to comment.