Create Dynamic Spinners only using CSS
Tags: css, html, webdev, ux
Published: 2021-03-14 03:27:00

In this blog we will be creating the **Pizza Spinner** depicted in the Cover Image.

# Getting Started

We would be using the following image of a **Pizza Slice** to generate the entire pizza. 

![Pizza Slice](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr7ormd6xl1ymkb501sv.png)

The shell of the **HTML** would be

```html
<div class="spinner">
  <img src="<link to image>" class="pizza-part pizza-part-1" />
  <img src="<link to image>" class="pizza-part pizza-part-2" />
  <img src="<link to image>" class="pizza-part pizza-part-3" />
  <img src="<link to image>" class="pizza-part pizza-part-4" />
</div>
```

# Breaking Down the Animation

Any complex animation can be broken down into a series of simple animations. Let's breaking down the animation into the corresponding components:
- Each of the pizza slice uses `opacity` to make the impression of fading in & out
- Each of the pizza slice also uses `transform` to `rotate` (to the required position to generate the appearance of a complete pizza) and `translate` (to generate the animation of picking the slice off the pizza)
- The main container spins about its center making it look even more dynamic

The **CSS** for the spinner would be

```css
.spinner {
  width: 160px;
  height: 160px;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  animation: spin 4s linear infinite;
}

.pizza-part {
  width: 80px;
  height: 80px;
}

.pizza-part-1 {
  animation: pizza1 2s ease-in-out infinite;
}

.pizza-part-2 {
  animation: pizza2 2s ease-in-out infinite;
}

.pizza-part-3 {
  animation: pizza4 2s ease-in-out infinite;
}

.pizza-part-4 {
  animation: pizza3 2s ease-in-out infinite;
}

/* animations */
@keyframes spin {
  0% {
    transform: rotateZ(0deg);
  }
  100% {
    transform: rotateZ(360deg);
  }
}

@keyframes pizza1 {
  0% {
    transform: translate(-20%, -20%);
    opacity: 0;
  }
  10% {
    transform: translate(0%, 0%);
    opacity: 1;
  }
  50% {
    transform: translate(0%, 0%);
    opacity: 1;
  }
  60% {
    transform: translate(-20%, -20%);
    opacity: 0;
  }
  100% {
    transform: translate(-20%, -20%);
    opacity: 0;
  }
}

@keyframes pizza2 {
  0% {
    transform: translate(20%, -20%) rotateZ(90deg);
    opacity: 0;
  }
  10% {
    transform: translate(20%, -20%) rotateZ(90deg);
    opacity: 0;
  }
  20% {
    transform: translate(0%, 0%) rotateZ(90deg);
    opacity: 1;
  }
  60% {
    transform: translate(0%, 0%) rotateZ(90deg);
    opacity: 1;
  }
  70% {
    transform: translate(20%, -20%) rotateZ(90deg);
    opacity: 0;
  }
  100% {
    transform: translate(20%, -20%) rotateZ(90deg);
    opacity: 0;
  }
}

@keyframes pizza3 {
  0% {
    transform: translate(20%, 20%) rotateZ(180deg);
    opacity: 0;
  }
  20% {
    transform: translate(20%, 20%) rotateZ(180deg);
    opacity: 0;
  }
  30% {
    transform: translate(0%, 0%) rotateZ(180deg);
    opacity: 1;
  }
  70% {
    transform: translate(0%, 0%) rotateZ(180deg);
    opacity: 1;
  }
  80% {
    transform: translate(20%, 20%) rotateZ(180deg);
    opacity: 0;
  }
  100% {
    transform: translate(20%, 20%) rotateZ(180deg);
    opacity: 0;
  }
}

@keyframes pizza4 {
  0% {
    transform: translate(-20%, 20%) rotateZ(-90deg);
    opacity: 0;
  }
  30% {
    transform: translate(-20%, 20%) rotateZ(-90deg);
    opacity: 0;
  }
  40% {
    transform: translate(0%, 0%) rotateZ(-90deg);
    opacity: 1;
  }
  80% {
    transform: translate(0%, 0%) rotateZ(-90deg);
    opacity: 1;
  }
  90% {
    transform: translate(-20%, 20%) rotateZ(-90deg);
    opacity: 0;
  }
  100% {
    transform: translate(-20%, 20%) rotateZ(-90deg);
    opacity: 0;
  }
}
```

# Demo

{% codepen https://codepen.io/ruppysuppy/pen/YzpBLBM %}

# Project using this Implementation

**Pizza Man**

Web-app: [http://pizza-man-61510.firebaseapp.com/](http://pizza-man-61510.firebaseapp.com/)

{% github https://github.com/ruppysuppy/Pizza-Man %}

Finding **personal finance** too intimidating? Checkout my **Instagram** to become a [**Dollar Ninja**](https://www.instagram.com/the.dollar.ninja/)

# Thanks for reading

Reach out to me on:

-   [Portfolio](https://tapajyoti-bose.web.app)
-   [GitHub](https://github.com/ruppysuppy)
-   [LinkedIn](https://www.linkedin.com/in/tapajyoti-bose-429a601a0)