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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

See all articles