Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create DRY CSS when using transform & keyframes
    primarykey
    data
    text
    <p>I'm trying to achieve <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow">DRY</a> CSS code when using <code>transform</code> in combination with <code>keyframes</code>. Let's say I've got the following:</p> <p><strong>HTML</strong></p> <pre><code>&lt;div id="box"&gt;&lt;/div&gt; </code></pre> <p><strong>CSS</strong></p> <pre><code>#box { width:300px; height:300px; background-color:red; animation: animate 5s; animation-iteration-count: infinite; animation-direction: alternate; animation-timing-function: ease-in-out; } @keyframes animate { from { transform: scale(0.8) translateY(100px) rotate(10deg); } to { transform: scale(0.8) translateY(100px) rotate(-10deg); } } </code></pre> <p><a href="http://jsfiddle.net/gmn3L/" rel="nofollow">JSFiddle example</a>.</p> <p>How can I prevent doing the <code>scale(0.8)</code> and <code>translateY(100px)</code> inside the animation? I only want it to be rotated back and forth, without having to apply these properties inside transform for each step. Here, only two steps are used (<code>from</code> and <code>to</code>), but if multiple steps are used (for example with <code>0%</code>, <code>20%</code>, <code>40%</code>, <code>60%</code>, <code>80%</code>, <code>100%</code>) it would mean a lot of repeated code. As you can imagine, this isn't very good when changes will appear later.</p> <p>Ultimately, I'm looking for something like this (which isn't possible, since the <code>transform</code> property will get overridden):</p> <pre><code>#box { transform: scale(0.8) translateY(100px); } @keyframes animate { from { transform: rotate(10deg); } to { transform: rotate(-10deg); } } </code></pre> <p>Is this even possible?</p> <p>Ps. I'm not looking for an answer where you would change the <code>width</code>/<code>height</code> to <code>scale</code> and/or change the <code>margin</code>/<code>top</code> property to <code>translate</code>. Also nothing with LESS/SASS to make the values easy to change, since it would still result in duplicated code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload