title: Using CSS Values in a D3 Transition
body: <p>I'd like to make d3 transition to a style defined in CSS. I can successfully transition to a style value which I explicitly apply in code. But, I'd like the target values of the animation to be defined in a style sheet.</p>
  
  <p>Here's an example of a red div which transitions to a blue one:</p>
  
  <pre><code>   &lt;html&gt;
      &lt;head&gt;
          &lt;script src="http://d3js.org/d3.v3.js"&gt;&lt;/script&gt;
          &lt;style&gt;            
              div {
                  background: red
              }
          &lt;/style&gt;
  
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div&gt;Hello There&lt;/div&gt;
  
          &lt;script&gt;
              d3.select('body div')
                  .transition()
                  .duration(5000)
                  .style("background", "cornflowerblue");
          &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here's a my (incorrect) attempt at "storing" these values in CSS:</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;script src="http://d3js.org/d3.v3.js"&gt;&lt;/script&gt;
          &lt;style&gt;            
              div {
                  background: red
              }
  
              div.myDiv {
                  background: cornflowerblue
              }
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div&gt;Hello There&lt;/div&gt;
  
          &lt;script&gt;
              d3.select('body div')
                  .transition()
                  .duration(5000)
                  .attr("class", "myDiv");
          &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The former animates the background from red to blue. The latter jumps from red to blue (no tweening is applied to background value defined in the CSS).</p>
  
  <p>I think I understand why it doesn't work. Can I animate to a set of values in a CSS style in d3, and how would I do it?</p>