Introduction to CSS Animation: Bringing Websites to Life
Tags: html, css, webdev, tutorial
Published: 2024-07-09 08:51:25

In the field of web development, a website’s aesthetic appeal is essential for engaging users and improving their experience. CSS animation is an effective tools for developer. CSS animation allows developers to breathe life into static web elements, resulting in visually appealing and dynamic user interfaces that capture and delight visitors.

**Understanding CSS Animation**
[CSS animation](https://skillivo.in/introduction-to-css-animation-1/) enables developers to animate HTML elements without requiring JavaScript or any other tools. Keyframes, transitions, and other CSS attributes allow developers to control many aspects of an element’s appearance and behaviour over time.

**Key Concepts**

**1. keyframes**
A keyframe in CSS defines the styles that an element should have at certain moments during an animation sequence. Consider establishing waypoints along a path that an element will follow as it animates. These waypoints, given as percentages ranging from 0% to 100%, specify how the element should appear at each stage of the animation. ([Read more](https://skillivo.in/introduction-to-css-animation-1/))

**Syntax and Usage**
To define keyframes in CSS, use the @keyframes rule followed by a name for the animation sequence. Within the @keyframes block, you specify the percentage values (or keywords like from and to), as well as the CSS attributes and values that should be applied at certain points.

Here’s a basic example

```
@keyframes slide-in {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0);
  }
}

```

In this example, the slide-in animation slides an element from left to right, starting from outside the viewport (-100% of its width) and ending at its original position (0%).

**Applying Keyframes**
Once you’ve set your keyframes, use CSS’s animation property to apply them to elements. This property lets you provide the animation’s name, duration, timing function, delay, and other characteristics.

```
.element {
  animation: slide-in 1s ease-out;
}

```
The slide-in animation is assigned to the.element class, which lasts for one second and uses an easing function for smooth acceleration and deceleration..

Let’s see an example
This animation makes text shake horizontally.

output:

![text shake horizontally](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rh86ihigypqi4lxnvqb.gif)
 

```
<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Page Title</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
  <style>
    .shaker {
  font-size: 24px;
  animation: shake 0.5s cubic-bezier(0.36, 0.07, 0.19, 0.97) infinite;
}
div
{
    width:500px;
    margin: 0 auto;
    margin-top:300px;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  50% { transform: translateX(5px); }
  75% { transform: translateX(-3px); }
  100% { transform: translateX(3px); }
}


  </style>
     
</head>
<body>
    <div class="shaker">Shaking Text</div>
</body>
</html>
```
These HTML examples show how to include CSS keyframe animations into your web pages and create engaging visual effects.


**Animation Properties of CSS Animation**
[CSS animation](https://skillivo.in/introduction-to-css-animation-1/) properties allow developers to precisely control how items move, transform, and transition on a web page. These features allow you to create appealing animations without using JavaScript or external libraries. ([click Here](https://skillivo.in/introduction-to-css-animation-1/) to read more examples of css animation)
Syntax:

```
animation: name duration timing-function delay iteration-count direction fill-mode play-state;
```

let’s look at another example

output:

![bouncing ball](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9d2dqoz4uwslnadaujpg.gif)

```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cubic Bezier Bouncing Animation Example</title>

<style>

.ball {
    width: 100px;
    height: 100px;
    background-color: #df6412;
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    animation: bounce 1.5s cubic-bezier(0.68, -0.55, 0.265, 1.55) infinite;
  }

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

</style>
</head>
<body>
<div class="ball"></div>
</body>
</html>

```

In this example

1. The animation property adds a bouncing effect to the ball element by using keyframe animations.
2. The cubic Bézier timing function used in the animation is defined by the cubic-bezier(0.68, -0.55, 0.265, 1.55) function, with specific control points.
3. The bounce keyframes animation create the bounce effect, starting from the initial state (0%) to halfway (50%) where the ball is translated upward by 100px (translateY(-100px)) and then returning to the initial state (100%).
4. By adjusting the control points of the cubic Bézier curve, you may customise the timing and behaviour of the bouncing animation to produce desired results.


**Conclusion**
CSS animation features give developers a variety of tools to create visually beautiful and interactive web experiences. By mastering these features and integrating them in new ways, developers can bring their designs to life and increase user engagement. CSS animations are best learned through experimentation and practice. ([Read more about CSS Animation](https://skillivo.in/introduction-to-css-animation-1/))