Rupali Parik & Aditya Tyagi

1 POSTS 0 COMMENTS
Rupali Parik and Adtiya Tyagi lead the front-end development team at EngineerBabu. Aditya Tyagi is a big-time web development enthusiast and bibliophile by nature. He is striving to leverage the power of technology to build brilliantly designed products for EB. A brilliant front-end developer, Rupali Parik shares keen interest in UI/UX. She is endeavoring to become a full-stack developer.

A Detailed Guide to CSS Animations and Transitions

0
CSS3 Animations and Transitions

Every one of us gets awestruck when we look at the exquisitely designed websites of tech biggies, like Apple or Microsoft, or of automobile companies like Tesla or Mercedes.

The X-factor that makes these websites stands out, is oftentimes the appealing animations and effects that make us look again and again.

So, how do they make the websites so amazingly interactive?
Ever gaped about how the animations on these websites actually work?
Do you wish to design a similarly elegant and interactive website for your business too?

If yes, then look no further.

Read along as this is an extensive excerpt covering the basics of CSS animations and transitions that could immensely help you in achieving the same for your business website.
If you have just ventured into the domain of front-end development, or are looking to expand your understanding about the same, then continue reading this blog, because by the end of it you’ll get a comprehensive understanding of CSS.

CSS or Cascading Style Sheets is the pretty part of web-applications you get to notice.

While CSS styles the web application, HTML or Hypertext Markup Language structures it. 

Animations play a critical role in improving the experience of users as they assist in providing improved visual feedback and helps in making interactions with the website striking.

CSS 3 has an abundance of in-built properties that substantially help in animating elements, and that too across all the major browsers.

But – With great power, comes greater responsibility.

Animations need to be used wisely, or you could end up creating a page that is more distracting than interactive. The entire purpose of animations is to serve as an aid, rather than a hindrance while using web applications.

According to a Google report, almost 50% of the overall web traffic comes through mobile devices. Hence, any business needs to create mobile-friendly animations. 

It is also imperative to have a basic understanding of CSS (preferably CSS 3), HTML and somewhat of jQuery. jQuery is a Javascript library that assists developers in playing with elements of the website. The technical jargon for this is – DOM manipulation.

As your target audience might vary, it is possible that you might be using more than one kind of web browsers (Mozilla Firefox, Google Chrome, Safari, Opera, Internet Explorer), and thus need to cater to all of them.

You can also check for the browser support for animations here.

CSS animations have 3 significant aspects to it:

  • Transforms
  • Transitions
  • Keyframes
CSS Animations and Transitions Flowchart
A flowchart depicting the key elements of Animations and Transitions in CSS.

PART A: TRANSFORMS

Transforms helps to change your web elements in all kinds of wonderful ways – from moving the element to re-sizing it, from rotating the element to tilting it.
And the best part – You can alter anything and everything without changing the document flow.

There are four significant aspects of transforms:

  1. Translate
  2. Scale
  3. Rotate
  4. Skew
    Let’s dig a little deeper, shall we?

1. Translate

CSS Translate
Moving an object from one point to another is basically translate.
Source: 0fps.net

Translate changes the coordinates of the element. It is used to change the appearance of the component on a 2D plane.

Translate means to simply move the element from one position to another on the webpage. You can translate an object on X-axis, Y-axis or both.

Moves the element on the X-axis
Syntax: transform: translateX(200px); or transform: translateX(-200px);

Moves the element on the Y-axis
Syntax: transform: translateY(200px); or transform: translateY(-200px);

Using shorthand
Syntax: transform: translate(x-axis, y-axis)

Example: transform: translate(200px, 200px);

Alert!
transform: translate(200px); [will only translate the element along the X-axis]

2. Scale

Scale Transition
Source: camo.envatousercontent.com

One can play with the size of the image, along with both the axis – X and Y. Scaling distorts the shape of the element and can degrade the quality of the element. A number greater than 1 will increase the size and a decimal less than 1 will decrease the size.

Scaling along the X-axis - transform: scaleX(3);

Scaling along the Y-axis - transform: scaleY(0.5);

Using the shorthand - for scaling along X and Y axis together -
transform: scale(3 , 0.5); or transform: scale(0.5);

3. Rotate

You can rotate the element either clockwise or counter-clockwise. The unit of measurement used is degrees. A positive value will rotate the element clockwise, and vice versa for the negative element. This rotation would also happen along X, Y and Z axis.

CSS Rotate
Source: gamedev.stackexchange.com

Understanding the rotation in CSS animations is one of the trickiest parts, and thus a lot of people fails to utilize the real potential of this feature.

Along X-axis
Imagine a marshmallow getting roasted while being rotated on a bonfire. You have to visualize the elements to turn into the page along with the X-axis, and that’s what rotation will look like on X-axis. We won’t see the 3D rotation, all we would be able to see is the height of the element changing.

Syntax: transform: rotateX(45deg);

Along Y-axis
Imagine a dancer on a pole. The elements will be rotating into the page along Y-axis. All you would notice is the width of the component getting changed.

Syntax: transform: rotateY(45deg);

Along Z-axis
This is the best possible orientation that you would work with while using rotation, as you can see the element actually rotating. One can also modify the clockwise and counter-clockwise movement with positive and negative rotation values respectively. For this, try imagining an arrow going into the page and the element rotating with respect to that arrow.

transform: rotateZ(45deg) – clockwise rotation

transform: rotateZ(-45deg) – counterclockwise rotation

4. Skew

CSS skewSkewing an element means tilting the component. It has both positive and negative values, and like rotation, it is measured in degrees (deg) too.

Positive X value bends the element to the left, and vice versa for the negative X. Likewise, a positive Y value dips the element downwards, and vice versa. By default, if X or Y aren’t stated in the transform, it will shift the element with respect to the X-axis.

Along X-axis
transform: skewX(45deg) or skew(45deg);

Along Y-axis
transform: skewY(80deg);

5. Combining transforms

One can also apply multiple transforms into a single transform statement. The order does matter (sometimes) as the second transform will apply on the first transform and the third will apply on the result of the first two transforms.

CSS Transform
Source: commons.wikimedia.org
transform: translateX(200px) rotateX(45deg) scaleY(1.5) skewX(45deg);

All of these transforms will take effect on some user event like hover, click, focus, active, etc. To see the magic in action, you can add the transforms in these events.

.element-to-animate{

display: block
}

.element-to-animate:hover{

transform: translateX(200px) rotateX(45deg) scaleY(1.5) skewX(45deg);

}

PART B: TRANSITIONS

Transition from one point to another
Source: mozillademos.org

If you got a chance to go over the code above, you might have noticed a jerk during the state change on hover; that’s precisely what transitions are for. Transitions help in making the animation fluid.

One can control the transitions with the help of the following properties:

  • transition-property
  • transition-duration
  • transition-timing-function
  • transition-delay

1. transition-property

These are the CSS properties that one can use transitions on. This ranges from playing with margins and paddings to background and borders. You can either apply a transition to a specific feature or to a complete list. A comprehensive list of all the properties that come under this purview can be found here.

Applying transition property to a specific CSS property

transition-property: background-color;

Applying a transition to the entire list of CSS properties

transition-property: all;

2. transition-duration

The time duration for which the animation will be in play. This can easily be measured in seconds (s) or milliseconds (ms). It is advisable to use seconds as it makes them easily readable – even your fellow developers at the office won’t get annoyed by you!

transition-duration: 0.5s;

3. transition-timing-function

Who doesn’t like SPEED! 
Speed is something that can make or break the user experience with your animation; therefore it is advisable to control it. The way you can accomplish it is by using transition-timing-function.

Pupils at CSS 3 team were kind enough to provide us with some in-built values for speed, like ease, ease-in, ease-in-out 

And in case, you wish to possess total command over the speed – Use Bezier Curves.

transition-timing-function: ease;

transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);

4. transition-delay

This helps in creating a pause between the event getting fired that starts the animation and the actual start of the animation. Simply put, transition-delay – delays the animation. It is measured in seconds (s) or milliseconds (ms).

transition-delay: 0.5s;

Shorthand:

transition: (property) (duration) (transition-timing-function) (transition-delay);

The order for the two time-oriented functions, i.e. duration and transition-delay matter – So stay alert!

Using Transforms and Transitions Together

Transition Between States
Source: medium.com

STATE 1: NORMAL

.element-to-animate display: block; transition: transform 1s ease 0.2s, background 1s; }

STATE 2: HOVER

.element-to-animate:hover{ background: black; transform: translateX(200px); }

For transitioning between STATE 1 and STATE 2, one can apply the transition to all the properties separately with individual timings, or if you wish to apply transitions on all properties in STATE 2, then:

transition: all 1s ease 0.2s;

 


PART C: KEYFRAMES

Keyframes in CSS
Source: css-tricks.com

The keyframes help in changing the animation from one to another at certain times.
One can play with keyframes in two ways:

  1. From – To approach
  2. Percentage Approach

1. From – To Approach

In this approach, there are only 2 states during the animation – start state and end state.

Syntax:

@keyframes animation_name {
            from { }
            to { }
}

To animate an element (e.g., a Car) to move horizontally from its initial position:

@keyframes drive{
from {
transform: translateX(-200);
}
to {
transform: translateX(1000px);
 }
}
.car{
animation-name: drive;
animation-duration: 3s;
animation-fill-mode: forwards;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-timing-function: ease-in;
animation-direction: normal;
}

2. Percentage Approach

We cannot use from-to in @keyframes as there is more than 1 state now. The percentage approach breaks down the animation into various intermediate states including a start and end state. The start state is indicated by 0%, and the end state is indicated by 100%. There can be as many intermediate states you want, but it is advised to keep the segregation of states uniform throughout.

Example: 0% – 25% – 50% – 75% – 100%

@keyframes jump{

0% { transform: translateY(-50px) }

50% { transform: translateY(-100px) }

100% { transform: translateY(-50px) }

}

• Animation shorthand

One can also use this concise format to write the animations:

animation: (animation-name) (animation-duration) (animation-fill-mode) (animation-timing-function) (animation-iteration-count) (animation-direction) (animation-delay);

Note: The animation-delay will always come AFTER animation-duration. Apart from them – time-oriented functions and the order of other properties in shorthand doesn’t matter.

Example:

.car{
animation-name: drive;
animation-duration: 3s;
animation-fill-mode: forwards;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-timing-function: ease-in;
animation-direction: normal;
}
Can be re-written as:
.car{
animation: drive 3s ease-in infinite normal 2s;
}

We now have two animations – drive and jump, which we wish to add on the “car” element. We can do this through a single line code by using, “Chaining Animations.”

• What is Chaining Animations?

Playing with multiple animations is a breeze with CSS. Comma separation technique can be used to chain the animation and run one animation after the other. For example,

.car{
animation: drive 3s ease-in infinite normal 2s, jump 2s ease 4 alternate reverse;
}

Classifications of Animation Properties:

    • Which Animation to use?
      animation-name: Name of the animation which is indicated by the keyword after @keyframes
    • What to do when the animation ends?
      animation-fill-mode: Tells what to do with the element being animated outside the animation window.
    • If you wish to repeat the animation “n” number of times, use,
      animation-iteration-count: It helps to repeat the animation. It can either have a number value or if you want the animation to play continuously, then use “infinite.”
    • If you wish to reverse the direction of the animation, use,
      animation-direction: You don’t need to write a separate animation all together just to reverse the animation. You can apply this to play with the current animation, and it’s direction.
In From – To Approach:

reverse: to - from
alternate: from-to -> to-from -> from-to
alternate-reverse: to-from -> from-to -> to-from

In Percentage Approach

reverse: 100% - 0%
alternate: 0% - 100% -> 100% - 0% -> 0% - 100%
alternate-reverse: 100% - 0% ->0% - 100% ->100% - 0%
    • Like SPEED? Control it using,
      animation-timing-function helps to control the speed with which the animation starts, behaves and ends. There are some in-built values like ease, ease-in and you can write your own using the cubic-bezier(n,n,n,n). Play with it here.
    • Do you procrastinate? CSS animations can too!
      Using animation-delay will start the animation few seconds/milliseconds after the event gets fired.

Wrapping Up

It could be a bit complicated to get used to CSS jargons at first. But once you get used you’ll realize that CSS Animations and Transitions are simply marvelous.

Thanks for reading. We hope this article helped you. If it did, please give it a thumbs-up and feel free to drop your comments in case you have any queries. Also, if you have exciting uses of CSS transitions or animations that you’d like to share, we’d love to hear from you.


Handpicked Content for You: