cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <section class="header">
  <h1>CSS Sprite Sheet Animations - Explained</h1>
  <div class="credits">
    <p>Sprite sheet by <a href="https://www.mozilla.org/en-US/firefox/partners/">Mozilla</a></p>
    <p>Demonstration by <a href="https://codepen.io/birjolaxew">Birjolaxew</a></p>
  </div>
</section>

<div class="content">
  <div class="viewport anim steps speed no-border no-shadow"></div>
  <p>Animated sprite sheets are quite simple. Here's how they're done:</p>
  
  <p>You start off with a "viewport". This is simply the element that you want the image to be seen in.</p>
  <div class="viewport no-bg"></div>
  
  <p>Next thing you do is set a <code>background-image</code> for the viewport (translucent image is not visible) to the <a href="https://www.mozilla.org/media/img/firefox/partners/foxtail.png">sprite sheet you want to use</a>.</p>
  <div class="no-shadow-left viewport"></div>
  
  <p>Now to the nifty stuff!</p>
  <p>Let <code>[anim-width]</code> be image width minus the width of your starting frame.</p>
  <p>Set the <code>background-position</code> of the viewport to animate from <code>0 0</code> to <code>-[anim-width] 0</code>.</p>
  <p>This animates from the left side of the first frame, to the left side of the last frame.</p>
  <div class="anim viewport"></div>
    
  <p>Change the <code>animation-timing-function</code> to <code>steps([number-of-frames])</code>.</p>
  <p>This moves the background image one frame at a time.</p>
  <div class="viewport anim steps"></div>
  
  <p>Ramp down <code>animation-duration</code> to speed up the animation.</p>
  <div class="viewport anim steps speed"></div>
  
  <p>And you're done!</p>
  <div class="example container"></div>
  
  <p>This was done with a horizontal sprite sheet - if you're using a vertical sprite sheet, simply animate the <code>y</code> position of the background instead of the <code>x</code> position.</p>
</div>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700);

/* Actual content stuff! */
.viewport {
  margin:0 auto 50px;
  height:156px;
  width:156px;
  background:#262729 url(https://www.mozilla.org/media/img/firefox/partners/foxtail.png);
  position:relative;
  border: 4px solid #da4e48;
}
.viewport::before, .viewport::after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 7020px;
  background: inherit;
  z-index: -1;
  opacity: .2;
}
.viewport::after { left: initial; right: 0; }

.no-bg { background-image: none; }
.no-shadow::before, .no-shadow::after, .no-shadow-left::after { content: initial; }
.no-border { border: none; }
.anim { animation: animate 44s linear infinite; }
.steps { animation-timing-function: steps(44); }
.speed { animation-duration: 3.75s; }

@keyframes animate {  
    0% {background-position: 0 0; }
    100% {background-position: -6864px 0;}
}

/* STYLE */
html, body {
  margin:0;
  padding:0;
  height:100%;
  background:#262729;
  overflow-x:hidden;
}
.header {
  height:50px;
  margin-bottom:40px;
  font-family:"Ubuntu", sans-serif;
  font-weight:500;
  color:#b4b4b4;
}
.header h1 {
  width:480px;
  height:100%;
  z-index:13;
  
  margin:0 auto;
  padding:20px 0 0;
  
  font-weight:500;
  text-align:center;
  
  box-shadow:inset 0 -4px #da4e48;
}
.header .credits {
  width:220px;
  
  margin:0;
  position:fixed;
  top:15px;
  right:35px;
  
  font-weight:700;
  color:#676767;

  z-index:15;
}
.credits p {
  margin:0;
}
a {
  color:#88d;
}
.content {
  font-family:"Ubuntu", sans-serif;
  font-weight:400;
  color:#ddd;
  z-index:5;
  position:relative;
}
.content p {
  text-align:center;
}
code {
  font-style:italic;
}
            
          
!
999px
Loading ..................

Console