title: How to use SVG Sprite Sheet as CSS background-image while maintaining aspect ratio and scalability
body: <p>TL;DR: I want to use several icons tiled in an SVG sprite sheet as CSS background-images, which maintain their aspect ratio and automatically scale to fill the parent element, using nothing but SVG and CSS. No JavaScript please.</p>
  
  <hr>
  
  <p>So I have a spritesheet in SVG format, which I made with a combination of <a href="http://svg-edit.googlecode.com" rel="noreferrer">SVG-Edit</a> and some hand-coding in Notepad++. Here's the source code:</p>
  
  <pre><code>&lt;svg version="1.1"
    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns="http://www.w3.org/2000/svg"
    width="600"
    height="400"
    viewBox="0 0 600 400"&gt;
    &lt;!-- Created with SVG-edit - http://svg-edit.googlecode.com/ --&gt;
    &lt;title&gt;chosen_sprite&lt;/title&gt;
    &lt;g&gt;
      &lt;title&gt;Add&lt;/title&gt;
      &lt;rect fill="none" stroke-width="10" stroke-dasharray="null" stroke-linejoin="null" stroke-linecap="null" x="5" y="5" width="90" height="90" id="svg_1" stroke="#dcdcdc"/&gt;
      &lt;line id="svg_2" y2="50" x2="70" y1="50" x1="30" stroke-linecap="round" stroke-linejoin="null" stroke-dasharray="null" stroke-width="12" stroke="#00a00c" fill="none"/&gt;
      &lt;line id="svg_3" y2="30" x2="50" y1="70" x1="50" stroke-linecap="round" stroke-linejoin="null" stroke-dasharray="null" stroke-width="12" stroke="#00a00c" fill="none"/&gt;
    &lt;/g&gt;
    &lt;g&gt;
      &lt;title&gt;Delete&lt;/title&gt;
      &lt;rect fill="none" stroke-width="10" stroke-dasharray="null" stroke-linejoin="null" stroke-linecap="null" x="105" y="5" width="90" height="90" id="svg_1" stroke="#dcdcdc"/&gt;
      &lt;line id="svg_2" y2="70" x2="170" y1="30" x1="130" stroke-linecap="round" stroke-linejoin="null" stroke-dasharray="null" stroke-width="12" stroke="#ff0000" fill="none"/&gt;
      &lt;line id="svg_3" y2="30" x2="170" y1="70" x1="130" stroke-linecap="round" stroke-linejoin="null" stroke-dasharray="null" stroke-width="12" stroke="#ff0000" fill="none"/&gt;
    &lt;/g&gt;
    &lt;g&gt;
      &lt;title&gt;Expand Dark&lt;/title&gt;
      &lt;rect stroke="#505050" id="svg_1" height="90" width="90" y="5" x="205" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="10" fill="none"/&gt;
      &lt;line fill="none" stroke="#000000" stroke-width="12" stroke-dasharray="null" stroke-linejoin="null" stroke-linecap="round" x1="250" y1="65" x2="280" y2="35" id="svg_2"/&gt;
      &lt;line fill="none" stroke="#000000" stroke-width="12" stroke-dasharray="null" stroke-linejoin="null" stroke-linecap="round" x1="220" y1="35" x2="250" y2="65" id="svg_3"/&gt;
    &lt;/g&gt;
    &lt;g&gt;
      &lt;title&gt;Collapse Dark&lt;/title&gt;
      &lt;rect stroke="#505050" height="90" width="90" y="5" x="305" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="10" fill="none" id="svg_4"/&gt;
      &lt;line fill="none" stroke="#000000" stroke-width="12" stroke-dasharray="null" stroke-linejoin="null" stroke-linecap="round" x1="350" y1="35" x2="380" y2="65" id="svg_5"/&gt;
      &lt;line fill="none" stroke="#000000" stroke-width="12" stroke-dasharray="null" stroke-linejoin="null" stroke-linecap="round" x1="320" y1="65" x2="350" y2="35" id="svg_6"/&gt;
    &lt;/g&gt;
    &lt;g&gt;
      &lt;title&gt;Expand Green&lt;/title&gt;
      &lt;rect fill="none" stroke-width="10" stroke-dasharray="null" stroke-linejoin="null" stroke-linecap="null" x="405" y="5" width="90" height="90" id="svg_1" stroke="#dcdcdc"/&gt;
      &lt;line id="svg_2" y2="35" x2="480" y1="65" x1="450" stroke-linecap="round" stroke-linejoin="null" stroke-dasharray="null" stroke-width="12" stroke="#00a00c" fill="none"/&gt;
      &lt;line id="svg_3" y2="65" x2="450" y1="35" x1="420" stroke-linecap="round" stroke-linejoin="null" stroke-dasharray="null" stroke-width="12" stroke="#00a00c" fill="none"/&gt;
    &lt;/g&gt;
    &lt;g&gt;
      &lt;title&gt;Collapse Green&lt;/title&gt;
      &lt;rect fill="none" stroke-width="10" stroke-dasharray="null" stroke-linejoin="null" stroke-linecap="null" x="505" y="5" width="90" height="90" id="svg_1" stroke="#dcdcdc"/&gt;
      &lt;line id="svg_2" y2="65" x2="580" y1="35" x1="550" stroke-linecap="round" stroke-linejoin="null" stroke-dasharray="null" stroke-width="12" stroke="#00a00c" fill="none"/&gt;
      &lt;line id="svg_3" y2="35" x2="550" y1="65" x1="520" stroke-linecap="round" stroke-linejoin="null" stroke-dasharray="null" stroke-width="12" stroke="#00a00c" fill="none"/&gt;
    &lt;/g&gt;
    &lt;g&gt;
      &lt;title&gt;Search&lt;/title&gt;
      &lt;circle id="svg_9" r="32" cy="140" cx="60" stroke-width="8" stroke="#000000" fill="none"/&gt;
      &lt;line id="svg_11" y2="167.5" x2="32.5" y1="190" x1="10" stroke-linecap="round" stroke-linejoin="null" stroke-dasharray="null" stroke-width="12" stroke="#000000" fill="none"/&gt;
    &lt;/g&gt;
    &lt;g&gt;
      &lt;title&gt;Search 2&lt;/title&gt;
      &lt;rect id="svg_10" stroke="#505050" height="90" width="90" y="105" x="105" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="10" fill="none"/&gt;
      &lt;circle r="25" cy="142.5" cx="157.5" stroke-width="8" stroke="#000000" fill="none" id="svg_7"/&gt;
      &lt;line y2="165" x2="135" y1="180" x1="120" stroke-linecap="round" stroke-linejoin="null" stroke-dasharray="null" stroke-width="12" stroke="#000000" fill="none" id="svg_8"/&gt;
    &lt;/g&gt;
  &lt;/svg&gt;
  </code></pre>
  
  <p>It works fine and looks the way I want it to.</p>
  
  <p>The problem is the CSS. Defining the cells in the spritesheet is a little bit messier than I would like it to be.  Here's the page I'm displaying these icons in:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;style&gt;
  
  * {padding: 0px; margin: 0px; outline: 1px solid rgba(0,0,0,0.1);}
  
  html {width: 100%; height: 100%;}
  
  body {width: 100%; height: 100%;}
  
  .svgSprite {
      background-image: url('./svgicons/form_icons_sprite.svg');
      background-repeat: no-repeat;
      background-size: 600%;
  }
  
  .svgSprite.add {
      background-position: 0px 0px;
      width: 12px;
      height: 12px;
  }
  
  .svgSprite.delete {
      background-position: -16px 0px;
      width: 16px;
      height: 16px;
  }
  
  .svgSprite.expandDark {
      background-position: -24px 0px;
      width: 12px;
      height: 12px;
  }
  
  .svgSprite.collapseDark {
      background-position: -36px 0px;
      width: 12px;
      height: 12px;
  }
  
  .svgSprite.expandGreen {
      background-position: -48px 0px;
      width: 12px;
      height: 12px;
  }
  
  .svgSprite.collapseGreen {
      background-position: -60px 0px;
      width: 12px;
      height: 12px;
  }
  
  .svgSprite.search {
      background-position: 0px -12px;
      width: 12px;
      height: 12px;
  }
  
  .svgSprite.search2 {
      background-position: -16px -16px;
      width: 16px;
      height: 16px;
  }
  
  &lt;/style&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;div class="svgSprite add"&gt;&lt;/div&gt;
  &lt;div class="svgSprite delete"&gt;&lt;/div&gt;
  &lt;div class="svgSprite expandDark"&gt;&lt;/div&gt;
  &lt;div class="svgSprite collapseDark"&gt;&lt;/div&gt;
  &lt;div class="svgSprite expandGreen"&gt;&lt;/div&gt;
  &lt;div class="svgSprite collapseGreen"&gt;&lt;/div&gt;
  &lt;div class="svgSprite search"&gt;&lt;/div&gt;
  &lt;div class="svgSprite search2"&gt;&lt;/div&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Basically, I want to know if there's an easier way to define the cells in the spritesheet and simplify the CSS I use to tell each div which icon to display from the spritesheet.</p>
  
  <p>I would prefer that this solution be strictly SVG and CSS; I am not interested in using JavaScript libraries. I am aiming to get it to a point where I can simply define the cells and have the particular icon I'm aiming for automatically scale to fit its container, while maintaining its aspect ratio.  Currently, in order to make the icon fit its parent container, its width and height need to be explicitly defined, and match the width and height of the parent container. If I change the width and height of the parent container, I need to change the background-position sizes as well.</p>
  
  <p>Then, there's the problem of scaling.  With this setup, the SVG scales to the appropriate size to be drawn on-screen, but if I decide to zoom using my browser's zoom, it pixelates.  This is not how SVG is supposed to work.</p>
  
  <p>I suppose I could just put each icon in its own file, because that seems to work wonderfully, but I just really like using sprites; it not only saves me several server requests, it's just cool.</p>
  
  <p>I am aware of <a href="http://code.google.com/p/svg-icon-loader/" rel="noreferrer">SVG Icon Loader</a>. It's pretty cool, but it's one more JavaScript file that I would rather not rely on.</p>
  
  <p>I've already read the w3 SVG docs, the MDN SVG docs, and the following threads on SO:</p>
  
  <p><a href="https://stackoverflow.com/questions/9414207/svg-spritesheets">SVG &amp; Spritesheets</a></p>
  
  <p><a href="https://stackoverflow.com/questions/4737243/fit-svg-to-the-size-of-object-container">Fit &lt;svg&gt; to the size of &lt;object&gt; container</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9185434/using-svg-as-background-image">Using SVG as background image</a></p>
  
  <p>...but even after all that, I haven't managed to find a solution.</p>
  
  <p>EDIT: I forgot to mention, this needs to work in IE9. That's a bit of an issue, I'm sure, but IE9's SVG support is decent, which is why I chose SVG for this project.</p>