Suggestions

TLDR; Not Your Typical Privacy Agreement

Powered by Cohere

Samsung Galaxy S10e

Specifications

  • Dimensions: 142.2 x 69.9 x 7.9 mm (5.60 x 2.75 x 0.31 in)
  • Weight: 150 g (5.29 oz)
  • Display: Dynamic AMOLED, HDR10+
  • Resolution: 1080 x 2280 pixels, 19:9 ratio (~438 ppi density)
  • OS: Android 9.0 (Pie), upgradable to Android 12, One UI 4.1
  • CPU: Octa-core (2x2.73 GHz Mongoose M4 & 2x2.31 GHz Cortex-A75 & 4x1.95 GHz Cortex-A55) - EMEA/LATAM
  • Main Camera: 12 MP, f/1.5-2.4, 26mm (wide)
  • Selfie Camera: 10 MP, f/1.9, 26mm (wide), 1/3", 1.22ยตm, dual pixel PDAF
  • Battery: Li-Ion 3100 mAh, non-removable
All Notes

Animation With CSS

Tuesday, March 14, 2023
Author:
Share to Reddit
Share to Facebook
Share to X
Share to LinkedIn
Share to WhatsApp
Share by email
Describing the associated blog post


Why Animate?

Animation adds an extra level of interaction to a web page. With CSS, you can apply animation properties to any element on a web page and make it do what you want. In this article, I'll create a basketball and make it move to simulate a bouncing object using CSS.

First Step

To start things off, we need to create our markup in HTML. The content of the body tag will contain the following:

    <div class="area">
        <div class="ball">
        </div>
    </div>

The first division tag with a class of area contains all of the parts that make up our basketball. If you don't know what basketball is, I suggest you watch this YouTube video๐Ÿ˜. The ball class is meant to simulate a round object, I've used a background image of a basketball that has been cropped perfectly to fit inside the circle.

Here is the CSS code applied to the ball class:

.ball{
    border-radius: 50%;
    background: url('images/basketball.png');
    background-size: contain;
    padding: 0;
    position: relative;
    width: 150px;
    height: 150px;  
}

Second Step

In order to animate something with CSS, we use the keyframes keyword which looks like this:

    @keyframes animation-name{
        0%{

        }
        50%{

        }
        100%{

        }
    }

The animation-name can be defined to be anything that is suitable. The percentages inside the curly brackets represent the different times that something happens within one movement. The movements are executed in the order of percentage rate from 0 to 100.

For this particular ball, the keyframe looks like this:

@keyframes bounce{
    0%{
        margin-top: 0px;
    }
    50%{
        margin-top: 50px;
        transform: scaleX(0.8);
    }
    75%{
        margin-top: 150px;
    }
    100%{
        margin-top: 350px;
        transform: scaleY(0.6);
        box-shadow: 0px 50px 20px black;
    }
}

Applying Animations

The keyframes object only defines the sequence of the animation. In order to make it functional, there is a property called animation that can be placed inside any CSS class. For this particular example, placing the following code inside the ball class will activate the animation movements:

    animation-name: bounce;
    animation-duration: 0.9s;
    animation-timing-function: ease-in-out;
    animation-delay: 0s;
    animation-iteration-count: infinite;
    animation-direction: alternate-reverse;
    animation-fill-mode: none;

Alternatively, you can use the animation shorthand property which addresses each of the 7 properties. The syntax of the animation shorthand property looks like this: animation: name duration timing-function delay iteration-count direction fill-mode;. For this particular example, we'd write it like this:

    animation: bounce 0.9s ease-in-out 0s infinite alternate-reverse none;

Animation Properties

For a more thorough understanding of the properties that are accessible for animating, we can refer to the list below:

Property Values Definition
animation-name keyframe object Defines a list of animations that apply. Each name is used to select the keyframe at-rule that provides the property values for the animation
animation-duration Time (e.g. 1s or 20ms) Defines the length of time that an animation takes to complete one cycle
animation-timing-function ease-in-out, ease-in, ease-out, ease Describes how the animation will progress over one cycle of its duration
animation-delay Time (e.g. 1s) Defines when the animation will start
animation-iteration-count e.g. infinite Defines the number of times an animation cycle is played. The default value is one, meaning the animation will play from beginning to end once.
animation-direction e.g. alternate Defines whether or not the animation should play in reverse on alternate cycles.
animation-fill-mode e.g. forwards Defines what values are applied by the animation outside the time it is executing.

Wrapping Up

I recently discovered that it is also possible to apply animations using JavaScript. The syntax looks like this:

    var element = document.getElementById('element');

    element.animate([
        {marginTop: 0px},
        {marginTop: 50px, transform: scaleX(0.8)},
        {marginTop: 150px},
        {marginTop: 350px, transform: scaleY(0.6), boxShadow: 0px 50px 20px black},
    ], {
        duration: 0.9, //milliseconds
        easing: 'ease-in-out', // linear, a bezier curve etc.
        delay: 0, // milliseconds
        iterations: Infinity, // or any number
        direction: 'alternate-reverse', // normal, reverse, etc.
        fill: 'none' // backwards, both, forwards, auto
    });

Applying this JavaScript method will play the same animation that we defined earlier with CSS or override any existing animations. Note that there is no need to use the animation-name property when using JavaScript.

I found myself recently dependent on CSS animations for a project which is why I decided to write this article. I hope it helps someone out there to increase their knowledge of CSS animations or even get comfortable using them. You're welcome to read my other article about HTML. Feel free to give me feedback through this link.

~ Thank You For Reading

Tawanda Andrew Msengezi

Tawanda Andrew Msengezi is a Software Engineer and Technical Writer who writes all the articles on this blog. He has a Bachelor of Science in Computer Information Systems from Near East University. He is an expert in all things web development with a specific focus on frontend development. This blog contains articles about HTML, CSS, JavaScript and various other tech related content.

User Notice

Dear Visitor,

This website stores your color theme preference, you can toggle your theme preference using the lightbulb icon in the top right of the webpage.

Clicking on the robot icon that says "Chat" in the bottom-left corner will open a chat with an AI assistant. Click the button below to close this message.