SVG inline in (X)HTML

01 Feb 2009 by David Corvoysier
SVG is now supported by all web browsers except IE. Opera has the best SVG support, but Safari and Firefox are not far behind. I think it is now time to take advantage of the powerful SVG features. What does SVG add to the web developer toolkit ? Well, as its names indicates, SVG first provides a simple syntax to draw shapes that scale: it is thus particularly handy to draw screen layouts and buttons. I see some people starting to get interested ... But wait: SVG also includes a wide range of gradient, opacity and geometric transformations that can be applied not only to native SVG shapes, but also to images. This give you access to many fancy effects very hard to achieve with HTML alone, like for instance image reflection or fading. Want more ? Check this out: SVG markup can be modified dynamically using javascript, allowing you to actually animate it. Who said cover-flow at the back of the class ? Animation through javascript sucks on low-end devices ? Well, SVG also includes native animation capabilities (unfortunately only supported today by Opera). Fair enough, but the whole web is built on top of HTML, and switching to full SVG is definitely an option. Well, the icing on the cake is that you don't have to: SVG can be inserted inline into XHTML. How powerful that is ? You will find detailed information on why and how it works on the SVG wiki. A method to have inline SVG supported by IE using the Adobe SVG viewer is also provided. Here is an overview of what you need to do.
  • First, you need to use XHTML, because plain old HTML doesn't support being extended by another markup language, and your SVG markup will be ignored by the browsers.
  • Second, you need to make sure that the SVG-aware browsers know this is XHTML, by delivering your content to them as application/xhtml+xml. You probably nevertheless still want to deliver your content to IE as text/html, so what you need is conditional mime-types based on the Accept header of the user agent.
    • it can be done at the HTTP server level. See example below for apache (example taken from the SVG wiki):
      AddType text/html .xhtml
      RewriteEngine on
      RewriteBase /
      RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml
      RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml\s*;\s*q=0
      RewriteCond %{REQUEST_URI} \.xhtml$
      RewriteCond %{THE_REQUEST} HTTP/1\.1
      RewriteRule .* - [T=application/xhtml+xml]
    • If you don't have access to your web server configuration, you can still:
      • modify headers on the fly. See an example for PHP below:
          header("Content-Type: application/xhtml+xml");
      • or insert meta tags in the HTML head to specify the proper mime-type. See another example for PHP:
          echo "<meta http-equiv='Content-Type' content='application/xhtml+xml' />" ;
      • If you use WordPress, the simpliest way is to use the Content-Negotiation plugin
  • Third, put your SVG under an SVG tag
  • <div id="myDIV">
        <svg xmlns="" version="1.1"
    	 viewBox="0 0 400 50"
    	      <linearGradient id="myGrad">
    		<stop style="stop-color:#ff5b06;" 
                            offset="0" />
    		<stop style="stop-color:#ffb11a;" 
                            offset="1" />
    	    <rect style="fill:url(#myGrad);"
    	       width="400" height="50" 
                   x="0" y="0" rx="10" ry="10" />
        <span>HTML Text with SVG background</span>
  • Fourth, style your SVG with CSS
  • #myDIV {
    #myDIV svg {
    #myDIV svg:hover rect {
        fill: red !important;
    #myDIV span {
        font-size: 16px;
        font-weight: bold;
The result should look like this:
HTML Text with SVG background

Please note in particular how the CSS styling allows the SVG to be perfectly integrated in the page:

  • the absolute positioning of the svg root element allows the next-coming HTML span to appear on top of the SVG background
  • the :hover behaviour allows a dynamic update of the SVG rect element fill attribute when the element is moused over
You will find more examples below:

UPDATE(2010): You can now inline SVG directly in HTML in most modern browsers.

comments powered by Disqus