Advanced CSS Animations and Examples

Advanced CSS Animations and Examples

Keyframes, transforms, and transitions. How they can be used to create dynamic and engaging animations.

ยท

6 min read

Introduction

In the realm of web design, animations play a pivotal role in captivating user attention and creating dynamic, immersive experiences. In this article, I'll show you some advanced CSS animations with examples. We're going to discuss keyframes, transforms, and transitions.

Keyframes: Bringing Motion to Life

Keyframes are the backbone of CSS animations, allowing for the creation of complex and multi-step animations. Let's delve into an example that incorporates multiple keyframes:

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

.element {
  animation: pulse 2s infinite;
}

Here, the pulse animation causes an element to pulsate by scaling it up by 20% and then back to its original size repeatedly.

Keyframes in CSS animations provide granular control over the animation process, allowing for the creation of intricate motion sequences. Let's explore further examples:

Sequential Animation with Multiple Keyframes

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-30px); }
}

.ball {
  animation: bounce 1s infinite;
}

In this instance, the bounce animation causes a ball to bounce up and down by altering its translateY property at different keyframe percentages.

Complex Animation Sequences

@keyframes spinAndColor {
  0% { transform: rotate(0); background-color: #3498db; }
  50% { transform: rotate(180deg); background-color: #e74c3c; }
  100% { transform: rotate(360deg); background-color: #2ecc71; }
}

.spinner {
  animation: spinAndColor 3s infinite;
}

This spinAndColor animation combines rotation and background color changes to create a spinning effect that transitions the color of an element throughout its rotation cycle.

Animating Opacity and Scale

@keyframes fadeAndGrow {
  0% { opacity: 0; transform: scale(0); }
  50% { opacity: 0.7; transform: scale(1.2); }
  100% { opacity: 1; transform: scale(1); }
}

.box {
  animation: fadeAndGrow 2s ease-in-out alternate infinite;
}

Here, the fadeAndGrow animation gradually fades in an element while simultaneously enlarging it, creating a smooth and engaging effect.

Combining Keyframe Animations

@keyframes move {
  0% { transform: translateX(0); }
  50% { transform: translateX(200px); }
  100% { transform: translateX(0); }
}

@keyframes rotate {
  0% { transform: rotate(0); }
  100% { transform: rotate(360deg); }
}

.element {
  animation: move 4s infinite, rotate 2s infinite alternate;
}

This example showcases two separate keyframe animations (move and rotate) applied simultaneously to an element, enabling it to move horizontally and rotate concurrently.

Keyframes in CSS animations are versatile, allowing developers to orchestrate complex motion behaviors by defining various states at different intervals during an animation sequence.

Transforms: Reshaping Elements

CSS transforms offer a range of possibilities, enabling elements to move, rotate, scale, or skew. Let's explore an example that combines multiple transformations:

.box {
  transform-origin: center;
  transition: transform 0.5s ease-in-out;
}

.box:hover {
  transform: rotate(180deg) scale(1.5);
}

In this instance, hovering over the box element rotates it 180 degrees clockwise around its center while simultaneously scaling it to 1.5 times its original size.

Combining Multiple Transform Functions

.element {
  transform: translate(20px, 20px) rotate(45deg) scale(1.5) skew(20deg, 10deg);
}

Here, the .element undergoes multiple transformations simultaneously. It translates 20 pixels horizontally and 20 pixels vertically, rotates by 45 degrees, scales to 150% of its original size, and skews horizontally by 20 degrees and vertically by 10 degrees.

3D Transformations

.cube {
  transform-style: preserve-3d;
  transform: rotateY(45deg) rotateX(45deg);
}

By using preserve-3d in conjunction with rotateY and rotateX, this example creates a 3D cube effect by rotating it around both the Y and X axes.

Transform Origin and Transition

.square {
  width: 100px;
  height: 100px;
  background-color: #3498db;
  transition: transform 0.3s ease-in-out;
}

.square:hover {
  transform: rotate(180deg);
  transform-origin: top left;
}

When hovering over the square element, it smoothly rotates 180 degrees around its top-left corner due to the specified transform-origin, creating an interesting effect.

Using Transform with Keyframes

@keyframes growAndRotate {
  0% { transform: scale(1) rotate(0); }
  50% { transform: scale(1.5) rotate(180deg); }
  100% { transform: scale(1) rotate(360deg); }
}

.shape {
  animation: growAndRotate 3s infinite;
}

The growAndRotate keyframe animation gradually scales and rotates the .shape element, resulting in a sequence where it grows, flips 180 degrees, and completes a full 360-degree rotation.

CSS transforms, when combined and applied creatively, can produce impressive visual effects and enhance user interactions on websites/apps. By understanding the nuances of each transform function and exploring their combinations, you can elevate the aesthetics and interactivity of web elements significantly.

Transitions

Transitions complement CSS animations by providing smooth changes in CSS property values over a specified duration. Let's see a more complex transition example:

Seamless Element Changes

.button {
  transition: all 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

.button:hover {
  transform: rotateY(180deg);
  color: #fff;
  background-color: #3498db;
  border-color: #3498db;
}

Hovering over the button triggers a transformation (flip effect), color change, and border adjustment, all smoothly transitioning over 0.3 seconds using a custom cubic-bezier timing function.

Custom Timing Functions

.element {
  transition: transform 0.5s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

.element:hover {
  transform: scale(1.5);
}

By incorporating a custom cubic-bezier timing function, the .element smoothly scales up when hovered over, providing a unique animation curve for the transition.

Transitioning Multiple Properties

.button {
  transition: background-color 0.3s ease, color 0.3s ease;
}

.button:hover {
  background-color: #3498db;
  color: #fff;
}

Here, the transition effect is applied to both the background-color and color properties of the button element, smoothly altering both when hovered over.

Delaying Transitions

.box {
  transition: transform 0.3s ease-in-out 0.2s;
}

.box:hover {
  transform: rotate(180deg);
}

Adding a delay of 0.2 seconds to the transition ensures that the transformation (rotation in this case) begins with a slight delay after the hover event.

Transitioning Height and Width

.box {
  width: 100px;
  height: 100px;
  background-color: #3498db;
  transition: width 0.3s ease-in-out, height 0.3s ease-in-out;
}

.box:hover {
  width: 150px;
  height: 150px;
}

Hovering over the box smoothly transitions its width and height, enlarging it from 100x100px to 150x150px with a smooth animation effect.

By combining different properties, timing functions, and delays, transitions can transform static elements into interactive components, making web interactions more intuitive and engaging!

Quicky: Advanced Animation Properties

Beyond the basics, let's explore some lesser-known advanced animation properties:

  • animation-timing-function: Using custom cubic-bezier functions to create unique animation acceleration and deceleration effects.

  • animation-play-state: Pausing and resuming animations dynamically using JavaScript to control animation states.

  • will-change: Optimizing animations by hinting to the browser which properties are likely to change, improving performance.

Question: Should I write an article explaining these properties?! Let me know if you'd be interested in that!

Conclusion

By harnessing the power of CSS animations and diving into advanced techniques, you can create compelling and interactive web experiences. These techniques open doors to creativity and innovation, enabling you to craft memorable and engaging digital content that captivates users' attention and elevates the overall user experience.

With a solid understanding of keyframes, transforms, transitions, and advanced animation properties, you can confidently venture into the realm of sophisticated web animations, transforming static designs into captivating, dynamic masterpieces. Experimentation and exploration are key; dive in and push the boundaries to create exceptional web animations.


๐Ÿ‘‹ Hello, I'm Eleftheria, Community Manager, developer, public speaker, and content creator.

๐Ÿฅฐ If you liked this article consider sharing it.

๐ŸŒˆ All links | X | LinkedIn

Did you find this article valuable?

Support Eleftheria Batsou by becoming a sponsor. Any amount is appreciated!

ย