Learn CSS transform animation | Zero to Hero

Control your website elements movement

Kamran Taghaddos
The Startup
9 min readJun 13, 2020

--

Photo by Mae Mu

In this article, we learn how to control the website elements (div, image, etc.). In other words, we are going to change the size, shape, and position of the web page elements, and they are all of the features that we can do with transform function.

Before starting to work with CSS animation with “transform” property, we should know the prerequisite, which is the CSS transition property. CSS transition property helps us to change CSS properties (background-color, font-size, etc.) smoothly at a particular time. If you are not familiar with CSS transition property, this article is short and straightforward, and also has fluent text for learning CSS transition property.

What is the CSS transform?

Control the website elements movement form one state to another.

Photo by Ahmad Odeh

Transform functions

Transform property has four functions that we can use to control how our elements would be displayed.

These are the functions:

  1. Translate: move the element and changing its position.
  2. Scale: for resizing elements.
  3. Rotate: rotate the element in a 2D environment, by any degree or any number of turns we want.
  4. Skew: skew the element along the x-axis or y-axis.

1. Translate

This function changes the position of the element like div, image, etc. along the browser x-axis or y-axis or both. We can use three different forms of usage:

Translate only in the x-axis:

Translate only in the y-axis:

Translate both in x-axis and y-axis:

For example, let’s look at these lines of codes below:

Let’s try them in the real world:

Note: If you look at the code snippet above, you understand that we used “transition-duration.” If you are not familiar with CSS transition property or you forget how to use it, then you can take a quick look at the article below to learn or remind it again.

2. Scale

This function allows us to resize the elements. In other words, changing the element’s width or height or both of those aspects. Like the translate function, we can use three different forms of usage:

Scale only in the x-axis:

Scale only in the y-axis:

Scale both in x-axis and y-axis:

For example, let’s look at these lines of codes below:

Let’s try them in the real world:

3. Rotate

This function rotates elements in a 2D environment. It accepts one value, which is the angle or the number of degrees.

The rotation value can be positive or negative. The positive value rotates the element clockwise, and the negative value rotates the desired web page element counterclockwise. Look at some examples below:

Also, we can use three different forms of value in the rotate function:

  • Degree: use to rotate the elements in certain degrees.
  • Turn: use to rotate the elements in a certain number of turns.
  • Gradient: use to rotate the elements at a certain angle.

For example, let’s look at these lines of codes below:

Note: In the real-world, the rotation function is used with degrees most of the time.

Let’s try some practice in the real world:

4. Skew

Skew functions, skew elements horizontally and vertically. It is like to translate and scale mentioned earlier. Meaning we have skew X to skew elements horizontally and skew Y to skew them vertically, or we can just use skew.

Skew is similar to rotate in terms of units. We use degrees to determine the amount of skew. It has a positive or negative value, like the rotate function. So let’s look at three different forms of skew function:

Skew horizontally:

Skew vertically:

Skew both horizontally and vertically:

For example, let’s look at these lines of codes below:

Let’s try them in the real world:

Transform origin

For finishing the transform function features, it is necessary to know about transform-origin. It is the point that our transform takes as a reference when it works. Transform origin accepts keywords as one value like top, bottom, right, and left. Also, we can combine these values. Let’s look at the real-world example:

Let’s try all of the possible ways:

Set the rotation pivot to top and bottom:

Note: the green circle in the images is only for guidance.

Set the rotation pivot to the right and left:

Set the rotation pivot to top-right and top-left:

Set the rotation pivot to bottom-right and bottom-left:

Last word

Indeed, CSS animation is your imagination and also your creativity. If you want to be an expert in this context, you should play with CSS animation properties and, of course, taking time to work with it. You can build all of your dreams animation if you want.

--

--