Home Web Front-end CSS Tutorial CSS Animations – Bringing Elements to Life

CSS Animations – Bringing Elements to Life

Sep 13, 2024 am 06:26 AM

CSS Animations – Bringing Elements to Life

Lecture 13: CSS Animations – Bringing Elements to Life

Welcome to Lecture 13 of the "Basic to Brilliance" course! In this post, we will explore CSS Animations—a powerful way to add life to your web elements by animating them over time. With CSS animations, you can create smooth, dynamic effects that enhance user experience and engagement.


1. CSS Animations Overview

CSS animations allow elements to transition between different styles over a defined period. You can control how the animation works using two key properties:

  • @keyframes: Defines the animation’s behavior at specific moments in time.
  • animation: Applies the animation to an element and controls its timing and duration.

2. The @keyframes Rule

The @keyframes rule specifies the styles the element should have at different points during the animation. You define keyframes at various percentages (0% being the start, 100% being the end).

  • Example: Basic keyframe animation to change the background color.
  @keyframes changeColor {
    0% {
      background-color: red;
    }
    100% {
      background-color: blue;
    }
  }
Copy after login

In this example:

  • The background color will gradually change from red to blue as the animation progresses.

3. Applying Animations with the animation Property

To apply the animation to an element, you use the animation property. This property requires a few key values:

  • Name: The name of the keyframes animation (e.g., changeColor).
  • Duration: How long the animation should run (e.g., 2s for 2 seconds).
  • Timing Function: The speed curve of the animation (e.g., ease, linear).
  • Delay: How long to wait before starting the animation.

  • Example: Applying an animation to an element.

  .box {
    animation: changeColor 2s ease-in-out infinite;
  }
Copy after login

In this case:

  • The .box element will change color from red to blue over 2 seconds, following an ease-in-out curve, and the animation will repeat infinitely.

4. Animation Timing Functions

The timing function controls how the animation progresses over time. Some common timing functions include:

  • linear: The animation progresses at a constant speed.
  • ease: The animation starts slow, speeds up, then slows down.
  • ease-in: The animation starts slow and speeds up.
  • ease-out: The animation starts fast and slows down.

  • Example: Applying a different timing function.

  .box {
    animation: changeColor 3s linear;
  }
Copy after login

Here:

  • The animation will progress at a constant speed, taking 3 seconds to complete.

5. Animation Iteration and Delay

You can control how many times an animation repeats using the animation-iteration-count property. You can also delay the start of the animation using animation-delay.

  • Example: An animation that repeats 3 times and starts after a 1-second delay.
  .box {
    animation: changeColor 2s ease-in 3;
    animation-delay: 1s;
  }
Copy after login

In this case:

  • The animation will begin 1 second after the page loads and repeat 3 times.

6. Animation Fill Mode

The animation-fill-mode property defines how an element should look before and after the animation. Common values include:

  • none: The element returns to its original state after the animation.
  • forwards: The element retains the final state of the animation.
  • backwards: The element takes on the initial state before the animation starts.

  • Example: Keeping the final state after the animation.

  .box {
    animation: changeColor 2s ease forwards;
  }
Copy after login

Here:

  • The element will remain blue after the animation completes, instead of reverting to red.

7. Multiple Animations

You can apply multiple animations to an element by separating them with commas.

  • Example: Animating both color and position.
  @keyframes moveBox {
    0% {
      transform: translateX(0);
    }
    100% {
      transform: translateX(100px);
    }
  }

  .box {
    animation: changeColor 2s ease, moveBox 2s ease-in-out;
  }
Copy after login

In this case:

  • The .box will change color and move 100px to the right at the same time.

8. Browser Support and Prefixed Properties

While modern browsers support CSS animations, it’s always a good idea to add vendor prefixes for older versions of browsers.

  • Example: Adding vendor prefixes.
  .box {
    -webkit-animation: changeColor 2s ease;
    -moz-animation: changeColor 2s ease;
    animation: changeColor 2s ease;
  }
Copy after login

This ensures compatibility across different browsers.


Practice Exercise

  1. Create a bouncing ball animation using @keyframes that makes the ball move up and down smoothly.
  2. Add an animation to an image that rotates it 360 degrees on hover.

Next Up: In the next lecture, we’ll explore CSS Transitions, which allow you to animate changes in CSS properties smoothly. You’ll learn how to create engaging hover effects and other interactions that enhance user experience.


follow me on LinkedIn

Ridoy Hasan

The above is the detailed content of CSS Animations – Bringing Elements to Life. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Adding Box Shadows to WordPress Blocks and Elements Adding Box Shadows to WordPress Blocks and Elements Mar 09, 2025 pm 12:53 PM

Adding Box Shadows to WordPress Blocks and Elements

Create a JavaScript Contact Form With the Smart Forms Framework Create a JavaScript Contact Form With the Smart Forms Framework Mar 07, 2025 am 11:33 AM

Create a JavaScript Contact Form With the Smart Forms Framework

Create an Inline Text Editor With the contentEditable Attribute Create an Inline Text Editor With the contentEditable Attribute Mar 02, 2025 am 09:03 AM

Create an Inline Text Editor With the contentEditable Attribute

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

Making Your First Custom Svelte Transition

File Upload With Multer in Node.js and Express File Upload With Multer in Node.js and Express Mar 02, 2025 am 09:15 AM

File Upload With Multer in Node.js and Express

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Mar 04, 2025 am 10:22 AM

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts)

Best CSS Animations and Effects on CodeCanyon 2025 (Paid   Free) Best CSS Animations and Effects on CodeCanyon 2025 (Paid Free) Mar 01, 2025 am 09:32 AM

Best CSS Animations and Effects on CodeCanyon 2025 (Paid Free)

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

Working With GraphQL Caching

See all articles