18 top CSS animation examples

You've probably noticed the number of CSS animation examples featuring on websites has been on the rise lately. Animation is already making a splash online, and is set to become one of the key web design trends of 2018. All over the web, designers are getting creative and using CSS animations to bring personality to their sites, explain complex ideas quickly and easily, and guide their users' actions.

These animations don't need to be overblown – even a subtle movement can have a big impact (most still have their roots in Disney's classic 12 principles of animation). 

In this article, we've pulled together a selection of CSS animation examples from some of the biggest websites around, and dug into the code to show you how to achieve these effects yourself. This page features in-depth tutorials, or click through to page 2 for inspiring effects (and links to their code) for you to dig into yourself.

01. Blowing bubbles

  • As seen on: 7UP 
  • View the code 

Click to see the animation in action

The CSS bubble animation that features on 7UP is a beautiful example of carrying a brand theme through into the website design.The animation consists of a few elements: the SVG ‘drawing’ of the bubbles and then two animations applied to each bubble. 

The first animation changes the opacity of the bubble and moves it vertically in the view box; the second creates the wobbling effect for added realism. The offsets are handled by targeting each bubble and applying a different animation duration and delay.

In order to create our bubbles we’ll be using SVG. In our SVG we create two layers of bubbles: one for the larger bubbles and one for the smaller bubbles. Inside the SVG we position all of our bubbles at the bottom of the view box.

In order to apply two separate animations to our SVGs, both utilising the transform property, we need to apply the animations to separate elements. The <g> element in SVG can be used much like a div in HTML; we need to wrap each of our bubbles (which are already in a group) in a group tag.

CSS has a powerful animation engine and really simple code in order to produce complex animations. We’ll start with moving the bubbles up the screen and changing their opacity in order to fade them in and out at the beginning and end of the animation.

In order to create a wobbling effect, we simply need to move (or translate) the bubble left and right, by just the right amount – too much will cause the animation to look too jaunting and disconnected, while too little will go mostly unnoticed. Experimentation is key with when working with animation.

In order to apply the animation to our bubbles, we’ll be using the groups we used earlier and the help of nth-of-type to identify each bubble group individually. We start by applying an opacity value to the bubbles and the will-change property in order to utilise hardware acceleration.

We want to keep all the animation times and delays within a couple of seconds of each other and set them to repeat infinitely. Lastly, we apply the ease-in-out timing function to our wobble animation to make it look a little more natural.

02. Scrolling mouse

  • As seen on: Baltic Training
  • View the code

Click to see the animation in action

A subtle scrolling mouse animation can give direction to the user when they first land on a website. Although this can be accomplished using HTML elements and properties, we're going to use SVG as this is more suited to drawing.

Inside our SVG we need a rectangle with rounded corners and a circle for the element we’re going to animate, by using SVG we can scale the icon to any size we need.

Now we’ve created our SVG, we need to apply some simple styles in order to control the size and position of the icon within our container. We’ve wrapped a link around the mouse SVG and positioned it to the bottom of the screen.

Next we’ll create our animation. At 0 and 20 per cent of the way through our animation, we want to set the state of our element as it begins. By setting it to 20% of the way through, it will stay still for part of the time when repeated infinitely.

We need to add in the opacity start point and then transform both the Y position and the vertical scale at the 100% mark, the end of our animation. The last thing we need to do is drop the opacity in order to fade out our circle.

Lastly we apply the animation to the circle, along with the ‘transform-origin’ property and the will-change property to allow hardware acceleration. The animation properties are fairly self-explanatory. The cubic-bezier timing function is used to first pull the circle back before dropping it to the bottom of our mouse shape; this adds a playful feel to the animation.

03. Playful pop

  • As seen on: Adidas
  • View the code

Click to see the animation in action

The circular buttons on the Adidas website – when interacted with – have a playful popping animation whereby another circle covers the button growing from the centre. This animated transition is overlapped by a fading circle around the edge, the same colour as the button’s original state. This adds a sense of fun and engagement.

To achieve this we use both the ::before and ::after pseudo elements – one for each of our animated circles. We absolutely position both of the elements, fixing them to the original button. In order to animate from the centre, we’re also setting the transform origin to the centre of the element.

We’ve set the initial scale of our ::before element to 0 so we can transition the value when interacted with. The transition property performs the animation. When we interact with our element, we simply increase the scale. 

However, since we’ve also changed the translation (another value of the transform property), we need to carry that value to the new transform property’s value. The outer circle is achieved using a very simple keyframe animation – transitioning the opacity.

04. Animated writing

  • As seen on: Garden Eight
  • View the code for the paths and text

Click to see the animation in action

The Garden Eight website uses a common animation technique whereby text appears to be written out. To achieve the effect, we turn to SVG. To begin with, we’ll create the SVG. There are two approaches here: convert the text to paths in order to animate them or use SVG text. Both approaches have their pros and cons.

Start by creating our keyframe animation. The only function we need it to perform is to change the stroke-dashoffset. Now we’ve created our animation, we need to apply the values we want to animate from. We set the stroke-dasharray, which will create gaps in the stroke. We want to set our stroke to be a large enough value to cover the entire element, finally offsetting the dash by the length of the stroke.

The magic happens when we apply our animation. By animating the offset, we’re bringing the stroke into view – creating a drawing effect. We want the elements to draw one at a time, with some overlap between the end of drawing one element and beginning to draw the next. To achieve this we turn to Sass/SCSS and nth-of-type to delay each letter by half the length of the animation, multiplied by the position of that particular letter.

05. Mesmerising mandalas

  • As seen on: God of War
  • View the code here and here 

Click to see the animation in action

Once again, to achieve this we turn to SVG to create our shapes, patterns and masks. Once we have our SVG, we target the masked darker circle with CSS and prepare it for animation.

Next, we create our keyframe animation. We only need to change two properties here: the opacity and the scale.

06. Flying birds

  • As seen on: Fournier Père et Fils
  • View the code for a single bird or the whole flock

Click to see the animation in action

We start with completely straight vector lines, drawing each frame of our animation, depicting the bird in a different state of flight. We then manipulate the vector points and round the lines and edges. Finally, we put each frame into an equally sized box and place them side-by-side. Export the file as an SVG.

The HTML setup is really simple. We just need to wrap each bird in a container in order to apply multiple animations – one to make the bird fly and the other to move it across the screen.

We apply our bird SVG as the background to our bird div and choose the size we want each frame to be. We use the width to roughly calculate the new background position. The SVG has 10 cells, so we multiply our width by 10 and then alter the number slightly until it looks correct.

CSS animation has a couple of tricks you may not be aware of. We can use the animation-timing-function to show the image in steps – much like flicking through pages in a notebook to allude to animation.

Now we’ve created our fly cycle, our bird is currently flapping her wings but isn’t going anywhere. In order to move her across the screen, we create another keyframe animation. This animation will move the bird across the screen horizontally while also changing the vertical position and the scale to allow the bird to meander across more realistically.

Once we’ve created our animations, we simply need to apply them. We can create multiple copies of our bird and apply different animation times and delays. 

07. Cross my hamburger

  • As seen on: Better Brand Agency
  • View the code for the spans and SVG

Click to see the animation in action

This animation is used all over the web, turning three lines into a cross or close icon. Until fairly recently, the majority of implementations have been achieved using HTML elements, but actually SVG is much more suited to this kind of animation – there’s no longer a need to bloat your buttons code with multiple spans. 

Due to the animatable nature and SVG and its navigable DOM, the code to accomplish the animation or transition changes very little – the technique is the same. 

We start by creating four elements, be it spans inside of a div or paths inside of an SVG. If we’re using spans, we need to use CSS to position them inside the div; if we’re using SVG, this is already taken care of. We want to position lines 2 and 3 in the centre – one on top of another – while spacing lines 1 and 4 evenly above and below, making sure to centre the transform origin.

We’re going to rely on transitioning two properties: opacity and rotation. First of all, we want to fade out lines 1 and 4, which we can target using the :nth-child selector.

The only thing left to do is target the two middle lines and rotate them 45 degrees in opposite directions.

08. Chasing circles

  • As seen on: Better Brand Agency
  • View the code

Click to see the animation in action

The animated loading icon is made up of four circles. The circles have no fill, but have alternating stroke-colours.

In our CSS, we can set some basic properties to all of our circles and then use the :nth-of-type selector to apply a different stroke-dasharray to each circle.

Next, we need to create our keyframe animation. Our animation is really simple: all we need to do is to rotate the circle by 360 degrees. By placing our transformation at the 50% mark of the animation, the circle will also rotate back to its original position.

With our animation created, we now just need to apply it to our circles. We set the animation name; duration; iteration count and timing function. The ‘ease-in-out’ will give the animation a more playful feel. 

At the moment, we have our loader, but all of the elements are rotating together at the same time. To fix this, we’ll apply some delays. We’ll create our delays using a Sass for loop.

Due to the delays, our circle now animates in turn, creating the illusion of the circles chasing each other. The only problem with this is that when the page first loads, the circles are static, then they start to move, one at a time. We can achieve the same offset effect, but stop the unwanted pause in our animation by simply setting the delays to a negative value.

Next page: More inspiring CSS animation examples to explore

09. Falling snow

  • As seen on: USA Today
  • View the code

Click to see the animation in action

The snow is created using an SVG and the technique is very similar to the way we created the bubbles earlier. To start, we create two layers of circles inside an SVG, then we animate those two layers by translating the Y value with a keyframe animation. 

We apply the animation to each layer instead of individual elements and reuse the same animation for both layers. By simply giving them different durations, we can add some depth to our scene.

10. Moving background

  • As seen on: A Violent Act
  • View the code

Click to see the animation in action

The website A Violent Act uses masking and subtle movement to grab the attention of the user. The majority of the work here is in the setup and creating the SVG.

11. Colourful transitions

  • As seen on: DaInk
  • View the code

Click to see the CSS animation in action

The Da-Ink website uses a really effective technique to transition between pages. The transition is simple and consists of an SVG containing a number of different-sized rectangles of different colours positioned on top of one another. 

The animation consists of transforming the X position by the width of the SVG. Then, using nth-of-type, we apply delays, offsetting each by 75ms from the last to create a smooth transition.

12. Pulsing circles

  • As seen on: PeekaBeat
  • View the code

Click to see the CSS animation in action

The pulse animation used on the Peek-a-Beat website is simple yet effective and not difficult to reproduce – consisting of three circles inside an SVG in which we animate their scale and opacity. 

13. Expanding highlight

  • As seen on: Heartbeat
  • View the code

Click to see the animation in action

This is a very simple, yet really effective technique. The transition is accomplished using the ::before pseudo element. To begin with, the pseudo element is placed at the bottom while spanning the full width, but only a few pixels in height. 

When the element is interacted with, the width and height of the pseudo element are both transitioned to 105% of the parent’s size (the change is much more dramatic vertically), as well as transitioning the colour of the text. 

14. Elevated title

  • As seen on: Ensemble Correspondances
  • View the code

Click to see the CSS animation in action

Ensemble Correspondances uses simple animation to convey movement in music. The design loosely represents sheet music. 

15. Spinning menu icon

  • As seen on: Coulee Creative 
  • View the code

Click to see the animation in action

The animated menu button is created using an SVG. The animation occurs when the user interacts with the menu button. Two transitions take place: the circular group around the menu spins 360 degrees and the menu icon in the centre changes colour. 

The most complicated part is the timing-function. Utilising cubic-bezier to gain complete control, we’re able to start the animation slowly, race through the middle part and slow it down again at the end.

16. Underline from the centre

  • As seen on: God of War
  • View the code

Click to see the animation in action

The animation consists of positioning the ::after pseudo element to the bottom and then scaling it when the button is interacted with.

17. Expanding corners

  • As seen on: Princess Alexandra Auditorium
  • View the code

Click to see the animation in action

The Princess Alexandra Auditorium website has a visual way to show the categories of its shows. Each of the show cards has a triangular corner set in a colour which represents the category and then, on hover, the name of the category is displayed. 

The effect is accomplished using the ::before and ::after pseudo elements, transitioning the size of the triangle and fading the name in when the element is interacted with.

18. Sliding arrow

  • As seen on: Greenwich Library
  • View the code

Click to see the animation in action

The Greenwich Library has a really interesting transition on its buttons. When interacting with the button, two things happen: the text part of the button is covered and the arrow is then animated off the right-hand side of the button and back in from the left. 

The colour transition is accomplished with the transition property and the arrow using a simple keyframe animation. Both the transition and the animation use the same duration in order to synchronise the movements.

This article originally appeared in issue 268 of Web Designer, the creative web design magazine – offering expert tutorials, cutting-edge trends and free resources. Buy issue 268 here or Subscribe to Web Designer here.

Read more:

  • Get started with GSAP
  • How to debug web animations
  • Create and animate SVG Polygons