This page is Almost Ready

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

SVG deployment

By Mike Sierra


This brief guide shows different ways to deploy SVG, either within HTML or as standalone files, with various options to reference CSS and JavaScript.

Until recently, SVG was fairly difficult to incorporate with other web content, and there are still many challenges. The HTML5 standard provides a way to insert inline regions of SVG into HTML files.

scr svg html.png

It translates to this basic HTML markup structure, where CSS and JavaScript may affect both HTML and SVG content:

<!DOCTYPE html>
    <link href="styles/html_and_svg.css" rel="stylesheet" />
    <script defer type="text/javascript" src="js/html_and_svg.js">
      <!-- define graphics here -->

There are also other ways to do it. You can reference external SVG files, and render them interactively within the HTML using either iframe, embed, or object tags:

<!DOCTYPE html>
    <link href="styles/html_and_svg.css" rel="stylesheet" />
    <script defer type="text/javascript" src="js/html_and_svg.js"></script>
    <iframe src=’graphics.svg’></iframe>
    <embed src=’graphics.svg’></embed>
    <object data=’graphics.svg’ type=’image/svg+xml’></object>

It’s also common to reference external SVG files to present static SVG graphics via CSS. The example below shows how you might place a right-aligned navigation arrow within a mobile interface, rendering as crisply as possible on high-resolution handsets:

scr svg css.png

 a[href] {
     background-image    : url(img/nav_arrow.svg);
     background-position : center 0 right 10px;
     background-size     : contain;
     display             : block;
     min-height          : 2em;
     border-radius       : 0.5em;
     padding             : 0.5em;

As you will see, you can also use URL anchors to reference individual component graphics that are collected within an SVG file. This assigns a custom bullet shape:

 ul > li {
    list-style-image     : url(img/components.svg#bullet);

Less common in practice, SVG files can be viewed as standalone files, perhaps as the target of a navigation. Any SVG file or svg tag region can reference its own set of CSS and JavaScript, which only applies within the SVG:

scr svg svg.png

This example shows how to embed or reference either CSS or JavaScript from within an SVG file:

<?xml version="1.0" standalone="no"?>
  <!-- reference CSS here: -->
<?xml-stylesheet href="css/styles.css" type="text/css"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "">
<svg xmlns="" version="1.1" width="700px" height="500px" viewBox="0 0 1000 500">
    <style type="text/css">
        <![CDATA[            /* embed CSS here */        ]]>
  <script type="application/ecmascript">
          // embed JavaScript here
  <!-- or reference JavaScript here: -->
  <script type="application/ecmascript" xlink:href="js/app.js">
  <!-- define graphics here -->

From within an SVG, you can also reference components from other SVG files. The example that follows shows how you might import a graphic of a cat from another SVG file, then style it calico based on referenced CSS:

scr svg svg2svg.png

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet href="css/svg_styles.css" type="text/css"?>
<svg xmlns="" xmlns:xlink="" width="1000" height="1000">
    <use xlink:href="components.svg#cat" class="calico"/>

You may also be able to use the foreignObject tag to render live HTML content within an SVG graphic environment. This example uses the switch tag to test whether the feature works, as recognized by the tag’s requiredExtensions attribute. If not, it uses fallback text:

scr svg svg2html.png

<?xml version="1.0" standalone="yes"?>
<svg width="4in" height="3in" version="1.1" xmlns=''>
  <desc>use 'switch' to test if foreignObject works, otherwise render fallback 'text' content</desc>
    <foreignObject width="100" height="50" requiredExtensions="">
      <!-- XHTML content goes here -->
      <body xmlns="">
        <p>Here is a paragraph that requires word wrap</p>
        <iframe src="external.html">...or an iframe...</iframe>
    <text font-size="10" font-family="Verdana">
      <tspan x="10" y="10">Here is a paragraph that</tspan>
      <tspan x="10" y="20">requires word wrap.</tspan>

See also

Related articles