demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

Using SVG In Web Pages

Before I get to creation tools for SVG, I thought it would be useful to cover the ways in which SVG files may be used on web pages. In contrast to the limited utility of bitmap images, the ways in which SVG files can be used are diverse:

Inline, via the <img /> tag

Probably the easiest method, and the one most familiar to web developers. SVG files can be brought onto a web page using an <img /> tag, just as bitmap images are:

  1. <img src="assets/svg/twitter-icon.svg" alt="Twitter" title="Twitter"
  2. style="width: 300px; height: 300px;" />

TwitterNote that size can also be set in the SVG document itself; scaling doesn’t matter per se, as SVG is vector based. CSS is applied to the SVG image like any other element.

The only downside to this technique is the SVG features that you drop when doing so: inline SVG images may include animation, but links inside the file and any scripts and interactivity that are part of the SVG code will be ignored. (The entire SVG image can be linked using an <a> element, of course),

Browser support for this method is strong:

IEFirefoxChromeSafariOperaiOSAndroid
9.01.5 and laterAll versions8 and later3.2 and later3.0 and later

In CSS

SVG is also very effective in CSS: like bitmaps, SVG files can be applied to properties like background-image, and much more besides:

  1. header { background: url(assets/svg/columns.svg); }

Browser support is much the same as for inline SVG images:

IEFirefoxChromeSafariOperaiOSAndroid
9.01.5 and laterAll versions3.0 and later8 and later3.2 and later3.0 and later

Using SVG in CSS also drops any scripting, interactivity, and links.

Directly, as an SVG tag

HTML5 has support for writing SVG files directly into the page. This preserves of the features of SVG, and is perhaps the best current practice, so long as you’re prepared to drop some backwards browser compatibility. The code looks something like this:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>HTML5 page with SVG Content</title>
  6. </head>
  7. <body>
  8. <p>This is the markup of an ordinary HTML5 page.</p>
  9. <svg version="1.1" xmlns="http://www.w3.org/2000/svg"
  10. xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
  11. width="400px" height="400px">
  12. <path fill="#5DD7FC" d="M0.5… /> <!—SVG document
  13. </svg>
  14. </body>
  15. </html>

Browser support for this technique is, not surprisingly, somewhat more limited:

IEFirefoxChromeSafariOperaiOSAndroid
9.04.0 and later7.0 and later5.1 and later11.6 and later5.03.0 and later

All internal links, scripting and interactivity are preserved in this method.

Via the <object>, <iframe> or <embed> tags

The oldest methods, stemming from when native support for SVG in the browser was non-existent or limited. Commensurately also the best supported.

  1. <object type="image/svg+xml" data="twitter-icon.svg">
  2. Warning for older browsers, or alternative content
  3. </object>
  1. <embed type="image/svg+xml" src="twitter-icon.svg"
  2. pluginspage="http://www.adobe.com/svg/viewer/install/" />
  1. <iframe src="twitter-icon.svg">
  2. Warning for older browsers, or alternative content
  3. </object>

The methods above preserve all SVG features, and, as mentioned, have the greatest compatibility with IE (note that the Adobe SVG viewer linked to has not been updated for some time).

IEFirefoxChromeSafariOperaiOSAndroid
9.0 (6.0+ if using plugin)All versions3.0 and later

As a font, via @font-face

As most fonts are vector shapes, SVG can be used to describe letter forms and embed them into web pages, as an alternative to the .woof, .otf and .eof formats. The CSS3 code looks something like this:

  1. @font-face {
  2. font-family: ‘GabbaGrabba’;
  3. url(‘gabbagabba.otf’) format(‘opentype’),
  4. url(‘gabbagabba.svg#gabba’) format(‘svg’);
  5. }

Sadly, Firefox and IE do not yet support the method, despite the other major browsers having a long history of doing so:

IEFirefoxChromeSafariOperaiOSAndroid
NoAll versions3.0 and later

Note that these techniques only cover SVG when it is used, or seen, directly on a webpage: there are many other possibilities (SVG filters on content, using Raphael or JQuery to display SVG images) that must be addressed in a separate article.

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.