CSS Animation – calc() Function & Transform: Create a Dynamic Dot Animation - Part 1

Make three dots bounce in a wave, then orbit into a colorful triangle with pure CSS. Simple steps, full code, beginner friendly

CSS Animation – calc() Function & Transform: Create a Dynamic Dot Animation - Part 1
Photo by Milad Fakurian / Unsplash

Introduction

Want to create complex animations without hard-coding dozens of values? In this beginner-friendly article series, you’ll build a playful CSS loading animation where bouncing orange dots morph into a spinning colorful triangle!

In this three-part article series, you'll learn how to use the CSS calc() function to build a fun and dynamic dot animation. Along the way, you'll also gain hands-on experience with CSS Custom Properties, positioning, transforms, and animation techniques that can be reused in your own projects.

  • In Part 1 (the article you're reading now), you'll build the foundation of the animation by creating and styling the dots, setting up the layout, and using CSS Custom Properties (variables) to make the code flexible and easy to maintain.
  • In Part 2, you'll bring the dots to life by creating the orange wave animation, learning how calc(), timing offsets, and translate motion effects work together to produce a smooth ripple-like movement.
  • Finally, in Part 3, you'll complete the project by building the blue-green triangle animation. You'll use transforms, rotation, and color transitions to turn the bouncing dots into a rotating triangular formation, bringing the entire animation sequence together.

CSS properties and functions you’ll be learning in this article:

  • CSS Custom Properties (variables)
  • cubic-bezier()
  • calc()
  • will-change

Preview

A quick preview of what you'll be creating throughout this three-part article series.

Three Dots - Animation Preview
Three Dots - Animation Preview

By the end of the series, you'll have created a dynamic CSS dot animation while gaining practical experience with modern animation and positioning techniques.

Prerequisites

Essential CSS and HTML knowledge will help you understand the concepts and techniques introduced in this article. Jump over to this article if you require an HTML and CSS primer.

We assume you have configured tools to modify CSS. If not, this article will guide you through the setup process.

Please read this article if you’re unfamiliar with CSS animation and the @keyframes at-rule property.

HTML Structure

<div class="container">
  <div class="dots-stage">
    <div class="dot" style="--i: 1; --c: #085bb5"></div>
    <div class="dot" style="--i: 2; --c: #08b95b"></div>
    <div class="dot" style="--i: 3; --c: #2708b5"></div>
  </div>
</div>

container is the outermost enclosure. It enables the content to be centered and draws a light gray border. The rest of the <div>s represent each animation sequence.

Keep the HTML structure as is for the animation to display correctly.

Body and Container <div> CSS

CSS code for the body and container <div>.

/* Body and Container Settings */
/* Center shapes */
body {
  margin: 0;
  padding: 0;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
}

/* Set background and border color */
.container {
  width: 500px;
  height: 500px;
  border: 5px solid lightgray;
  background: #ffffff;
  position: relative;
  margin: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

Custom Properties

Let's explore the :root custom properties block and see how these global variables control the animation's size, spacing, movement, and timing.

/* Custom Properties */
:root {
  --dot-size: 90px;
  --dot-gap: 26px;
  --orbit-radius: 89px;
  --wave-height: 84px;
  --anticipation-dip: 18px;
  --orange-color: #f99f3e;
  --animation-duration: 6.5s;
  --animation-easing: cubic-bezier(0.45, 0, 0.55, 1);
}

The :root selector acts as the animation's central control hub. By storing values as CSS Custom Properties (variables) at the highest level of the document, every selector and @keyframes animation can access and reuse the same settings.

1.Geometry and Layout Variables: The first group of variables controls the size and positioning of the dots.

  • The --dot-size property sets both the width and height of each circle, ensuring they remain perfectly round.
  • The --dot-gap variable defines the empty space between neighboring dots in the starting horizontal layout. When combined with --dot-size, it creates a consistent distance between each dot, helping the animation look balanced and symmetrical.
  • Meanwhile, --orbit-radius determines how far the dots travel away from the center when they transform into the rotating triangle. Increasing this value makes the triangle larger, while decreasing it keeps the movement closer to the center.

2.Motion and Bounce Controls: The next set of variables influences how the wave animation behaves.

  • The --wave-height property controls the maximum height reached during each bounce. Because the animation uses a negative value inside the keyframes, the dots move upward on the screen as they reach their peak.
  • The --anticipation-dip variable adds a small downward movement before and after a jump. This brief "crouch" creates a more natural motion, making the dots feel as though they are gathering energy before launching upward. Combined with the scaling effects in the keyframes, it helps create a convincing squash-and-stretch animation.

3.Color and Timing Settings: The remaining variables control the dot color and animation timing behavior.

  • The --orange-color property stores the shared starting color used by all three dots during the wave animation. Later, each dot transitions to its own color using the --c custom property defined in the HTML.
  • The --animation-duration variable sets the length of one complete animation cycle. Because both animations use the same duration, their movements remain perfectly synchronized throughout the loop.
  • Finally, --animation-easing applies a custom cubic-bezier() curve that creates a smooth ease-in-out effect, allowing the dots to accelerate and decelerate naturally instead of moving at a constant speed.

Now that you understand how CSS Custom Properties store and manage the animation's reusable values, let's take a closer look at the Dot Stage element in the next section.

Dot Stage

The Dot Stage (.dots-stage) element acts as the animation stage where all three dots perform their movements. Think of it as an invisible wrapper that provides a positioning reference for every animated element inside it. Before we examine the CSS rules, let's first look at the HTML structure and understand how the stage fits into the overall layout.

<div class="container">
  <div class="dots-stage">
    <div class="dot" style="--i: 1; --c: #085bb5"></div>
    <div class="dot" style="--i: 2; --c: #08b95b"></div>
    <div class="dot" style="--i: 3; --c: #2708b5"></div>
  </div>
</div>

Inside the container sits .dots-stage, which serves as the animation's positioning reference point.

The three .dot elements are the stars of the animation. Since they all share the same class="dot", a single CSS rule can define their size, shape, color, and animation behavior. This follows the DRY (Don't Repeat Yourself) principle, making the code easier to maintain and update.

Each dot also includes two inline CSS Custom Properties:

  • --i (Index Number) assigns a unique value (1, 2, or 3) to each dot. CSS uses this number inside calc() expressions to determine the dot's position, rotation angle, and animation delay automatically.
  • --c (Identity Color) stores the dot's secondary color. During the triangle animation, the dots change from the shared orange color to their individual colors: blue (#085bb5), green (#08b95b), and indigo (#2708b5).

This approach is both clean and scalable. Instead of creating separate classes such as .dot-1, .dot-2, and .dot-3, the animation uses CSS Custom Properties to give each dot its own identity while allowing all three elements to share the same styling and animation rules.

/* Stage wrapper for centering and coordinate origin */
.dots-stage {
  position: relative;
  width: 100%;
  height: 100%;
}

The most important property here is position: relative;. This turns .dots-stage into the positioning container for every .dot element inside it. Because the dots use position: absolute;, their top and left values are calculated relative to this stage rather than the browser window.

The width: 100%; and height: 100%; declarations allow .dots-stage to expand and fill the entire .container. As a result, the stage becomes the same size as the 500×500px viewport, giving the animation plenty of room for the dots to bounce, orbit, and rotate.

Although .dots-stage does not have a visible background, border, or decoration, it plays a critical role. It establishes the animation's coordinate system, making the center of the stage the starting point from which all dot movements are calculated.

Next, let's focus on the Dot Styling, where the circles receive their size, shape, position, and animation behavior.

Dot Styling

While the HTML creates the three dots, the CSS transforms them into animated elements that can bounce, stretch, rotate, and change color throughout the sequence.

The .dot rule is where the animation truly comes to life. This single CSS block is responsible for positioning the dots, calculating their individual movement paths, and starting both animations.

Orange Dot Wave - Styling
Orange Dot Wave - Styling

Even though there are three dots on the screen, they all share the same CSS rule. By combining CSS Custom Properties, calc(), and multiple animations, each dot can behave differently without requiring separate classes.

1.Centering the Dots and Creating Circles

The first part of the rule places every dot in the exact center of .dots-stage.

/* Individual Dot Styling */
.dot {
  position: absolute;
  top: 50%;
  left: 50%;
  width: var(--dot-size);
  height: var(--dot-size);

  margin-top: calc(var(--dot-size) / -2);
  margin-left: calc(var(--dot-size) / -2);

  border-radius: 50%;
  background-color: var(--orange-color);
}
  • Setting top: 50%; and left: 50%; moves the dot's top-left corner to the center of the stage. However, that doesn't perfectly center the circle.
  • To align the dot itself, margin-top: calc(var(--dot-size) / -2); and margin-left: calc(var(--dot-size) / -2); shift it upward and left by half of its width and height.
  • The width and height properties use var(--dot-size) so every circle stays consistent in size.
  • Finally, border-radius: 50%; transforms the square element into a perfect circle, while background-color: var(--orange-color); gives each dot its starting orange color.

2.Using calc() to Create Unique Values
Although all three dots share the same CSS rule, they don't behave identically.

.dot {
  --linear-x: calc((var(--i) - 2) * (var(--dot-size) + var(--dot-gap)));

  --base-angle: calc(var(--i) * 120deg - 158deg);

  --wave-delay: calc((var(--i) - 1) * 0.14s);
}
  • Each dot receives a unique --i value in the HTML, and that value is used to calculate its own position, angle, and timing.
  • The --linear-x variable determines where each dot sits in the horizontal row. Using calc(), Dot 1 is placed to the left, Dot 2 remains in the center, and Dot 3 moves to the right.
  • The --base-angle variable determines each dot's starting angle when the triangle animation begins. By multiplying --i by 120deg, the dots are evenly distributed around a circle, creating the three corners of an equilateral triangle. Subtracting 158deg rotates the completed triangle into the desired starting orientation. Together, they ensure the dots automatically form a balanced triangular shape without manually assigning separate angles to each dot.
  • The --wave-delay variable creates the ripple effect. The formula uses each dot's index value (--i) to calculate a unique delay.

    Dot 1: calc((1 - 1) * 0.14s) → 0s
    Dot 2: calc((2 - 1) * 0.14s) → 0.14s
    Dot 3: calc((3 - 1) * 0.14s) → 0.28s

    Instead of bouncing together, each dot waits slightly longer than the previous one, producing a smooth wave motion that travels across the row.

3.Performance and Animation Control

The will-change property tells the browser which properties will animate most often, including translate, scale, transform, and background-color. This helps the browser optimize rendering and keep the motion smooth.

.dot {
  will-change: translate, scale, transform, background-color;

  animation:
    orangeDotWave var(--animation-duration) var(--animation-easing) infinite,
    
    blueGreenTriangle var(--animation-duration) var(--animation-easing) infinite;

  animation-delay: var(--wave-delay), 0s;
}

The animation property launches two separate animation sequences at the same time:

  • orangeDotWave controls the bouncing wave motion.
  • blueGreenTriangle controls the color change and rotating triangle formation.

Finally, animation-delay: var(--wave-delay), 0s; applies the staggered delay only to the wave animation. This allows the dots to bounce one after another while the triangle animation remains synchronized across all three dots.

Below is the complete Dot Styling CSS block.

/* Individual Dot Styling */
.dot {
  position: absolute;
  top: 50%;
  left: 50%;
  width: var(--dot-size);
  height: var(--dot-size);

  margin-top: calc(var(--dot-size) / -2);
  margin-left: calc(var(--dot-size) / -2);

  border-radius: 50%;
  background-color: var(--orange-color);

  --linear-x: calc((var(--i) - 2) * (var(--dot-size) + var(--dot-gap)));

  --base-angle: calc(var(--i) * 120deg - 158deg);

  --wave-delay: calc((var(--i) - 1) * 0.14s);

  will-change: translate, scale, transform, background-color;

  animation:
    orangeDotWave var(--animation-duration) var(--animation-easing) infinite,
    blueGreenTriangle var(--animation-duration) var(--animation-easing) infinite;
  animation-delay: var(--wave-delay), 0s;
}

By combining positioning, custom calculations, and multiple animations in a single rule, the .dot selector gives each circle its own personality while keeping the CSS clean, flexible, and easy to maintain.

You can see and play with the complete code on Pyxofy’s CodePen page.

See the Pen CSS Animation – calc() Function & Transform: Create a Dynamic Dot Animation by Pyxofy (@pyxofy) on CodePen.

Conclusion

In Part 1 of this three-part series, you focused on building the foundation of the animation.

You created and styled the three dots, set up the animation stage, and used CSS Custom Properties (variables) and the calc() function to calculate positions, angles, and timing values dynamically. By establishing this flexible structure, you've prepared everything needed for the animation's movement and transformations in the next two parts.

Now that the groundwork is complete, you're ready to bring the dots to life with the animated wave effect in Part 2.

What animation will you make using the calc() function? Share your masterpiece with us on LinkedIn, Threads, Bluesky, Mastodon, X (Twitter) @pyxofy, or Facebook.

We hope you liked this article. Kindly share it with your network. We appreciate it.

calc() Function & Gradients: Create an Isometric Tornado - Part 1
Learn to build an animated 3D isometric tornado using only CSS—no images or JavaScript required!
CSS Animation – 3D Transforms Using translate3d()
2D? Nope. We live in a three-dimensional (3D) world! Learn 3D transforms using the CSS translate3d() and many more in this step-by-step article.
JavaScript プログラミングを始めよう
JavaScript(ジャバスクリプト)は、私たちが日常的に利用しているウェブサイトやウェブアプリなどで使われているプログラミング言語です。プログラミング初心者の方へ向けて、JavaScript について簡単に紹介します。
スクラッチプログラミング - 「おんがく」ブロックでピアノをつくろう
拡張機能(かくちょうきのう)の中から「おんがく」をえらんで、あたらしいブロックを追加(ついか)してみましょう。いろいろな音の楽器(がっき)をえんそうしたり、曲(きょく)をつくれるようになりますよ。