|

CSS Transitions and Transformations

CSS Transitions and Transformations

As we have seen in the recent tutorials about CSS, many nice visual effects can be created on your web pages entirely in CSS with no Graphics or JavaScript required. In this tutorial we’ll take a look at the animation capabilities built right into CSS3 to make effects that were only possible with technologies like Flash in the past. We can begin by looking at CSS Transitions which make it easy to make changes to a style happen smoothly over time. Next up we’ll have a look at Transforms. CSS Transforms can do things like skew, rotate, scale, and position elements. When combining Transitions and Transforms, some interesting animation effects are possible.


Transitions In CSS3

The effect that transitions provide is very much like what you see with jQuery. For example, if you hover the mouse over a particular link on a web page and the background color changes gradually from blue to green all the while acting very slick with various shades appearing during the change – that can be done with a CSS Transition. Transitions make changes that would otherwise be very sudden and abrupt to be smooth and slick. When used sparingly, transitions can add a nice polish to web pages.


Transition Property and Transition Duration

Let’s see an example of putting the transition-property and transition-duration properties to use. The transition-property property takes the name of the CSS property that you want to transition, or add an effect to. In the example below, we are adding a transition to the background-color property.

The transition-duration property defines how long it will take the transition to complete. In the example below, we set this to 1 second.


Hover over the Super element to see the background-color transition in action.


Transition Timing Functions

Once you have a applied the transition-property and transition-duration properties to use on an element, you may further customize the behavior of the transition using the transition-timing-function property. This property is used to define how the intermediate values of the CSS properties being transitioned are calculated. The values that the transition-timing-function property accepts are as follows.

  • ease Applies a transition effect which starts slowly, speeds up, then ends slowly. It is the default value.
  • ease-out Applies a transition effect which has a slow ending.
  • ease-in-out Applies a transition effect which has a slow start and slow ending.
  • linear Has a consistent effect from beginning to end. Feels a bit robotic.
  • ease-in Starts slowly at first, then speeds up.
  • steps This interesting value divides the transition into a set number of steps.
  • step-start Changes the state in one big step, right at the beginning of the duration timer.
  • step-end Changes the state in one step, right at the end of the duration timer.

We can see these different values in action here. We have eight different boxes and each gets a different value for the transition-timing-function property.

Hover over the container to see the width transition with different transition-timing-function values in action.

step-start

ease

ease-out

ease-in-out

linear

ease-in

steps(3,end)

step-end


Adding More Transition Properties

You can add more than one CSS property as a value to the transition-property property, allowing you to create more sophisticated animations. For example, we are going to take our initial link which transitioned from blue to green, and we can add the border-radius property to our transition. In the snippet below, you can see we now have second CSS property added to transition-property. Note, that the transition-duration now also has a second time value. This means you can change the time it takes for each transition independently. For our purposes, we’ll have the background-color transition happen in one second and the border-radius transition happen in three seconds.

Hover over the Super element to see the background-color transition and border-radius transition in action.


That is pretty super! Now let’s take that idea little further. We can use the transition of a border-radius to turn a square into a circle. What’s more, we will also apply a transition to the letter-spacing and color of the font. We can create a perfect square with a horizontal and vertical center using Flexbox – then we add our transitions.

Hover over the square to change it into a circle.

Circleify


Maybe you’d rather see a circle morph into a square. To do that, just set the border-radius to start at 50% and then transition to no border radius.

Hover over the circle to turn it into a square.

Squareify


This fun effect can add an inner border to an element by way of a box-shadow.

Hover over the rectangle to add an inner border.

Inner Border Effect via Box Shadow


You can just keep stacking these transition properties if you like. In this one we’ll add a transition to the box-shadow, background-color, color, and letter-spacing of the element. It makes for a neat effect.

Hover over the element to see four properties transitioning.

Four Property Transitions!


Creating A Button Push Effect

We can use all of the techniques we have learned so far to create some cool push button effects on a flexbox menu.

html

css


Hover over a menu item, then click and hold to get the “button press” effect. The click and hold action gives the impression of a button actually depressing downwards as it is pushed.


CSS Transforms

The first section discussed CSS Transitions and the fact that they are the most simple way to add a smoothing effect on elements as they change between two states. Now we can have a look at CSS Transforms which are kind of the next level of power in CSS Animations technologies. CSS Transforms can transform your web page elements in all kinds of fun and interesting ways. We can do things like stretch an element, change the coordinates of an element, rotate elements, and more.

The transform CSS property is a doozy! It can accept a whole slew of values to modify the coordinate space of the CSS visual formatting model. Here are the list of values you can use with the transform property.

  • rotate() Rotates a page element around a fixed point on the 2D plane.
  • rotateX() Rotates a page element around the abscissa (horizontal axis).
  • rotateY() Rotates a page element around the ordinate (vertical axis).
  • rotateZ() Rotates a page element around the z-axis.
  • rotate3d() Rotates a page element around a fixed axis in 3D space.
  • translate() Changes the elements position in the horizontal and/or vertical directions.
  • translateX() Changes the position of an element horizontally on the 2D plane.
  • translateY() Changes the position of an element vertically on the 2D plane.
  • scale() Resizes the element.
  • scaleX() Resizes the element horizontally.
  • scaleY() Resizes the element vertically.
  • skew() Gives a tilt effect to the element.
  • skewX() Tilts the element horizontally.
  • skewY() Tilts the element vertically.

Now that we know what can be done with all of the different values for transform, let’s see a bunch of examples. We can start with a simple cloud.

just a pretty cloud


translateX()

The translateX() CSS function changes the position of an element horizontally on the 2D plane. That means we can move our cloud to the right just a bit like so.

transform translateX example


scaleX()

The scaleX() CSS function resizes an element along the x-axis which is horizontal. That means we can make our cloud twice as big here horizontally.

css transform scaleX


scale()

The scale() function can accept more than one value. The second value in this example scales on the y-axix as well. So this cloud will scale horizontally and vertically.

translate scale x and y values


rotate()

We can rotate elements on the page with the rotate() CSS function. This function rotates an element around a fixed point on the 2D plane. Let’s see a few examples.

Two images are used to give a nice effect with the rotate() function. The first image has a lesser opacity to show the original position versus the transformed position.

css transform rotate 2 image effect


transform-origin

When using the rotate() function with the transform property, we can also use transform-origin to change the position of transformed elements. In other words, we can specify the pivot location the element rotates on. The value for transform-origin can be length measurements, keywords, or percentage values. The first value given is the offset horizontally while the second is the offset vertically. If you specify only one value, then this value is used for both.

Notice how with the transform-origin: top center; set, the image uses that location to rotate from.
transform-origin top center example

Here we can set transform-origin: right bottom; to see how that changes the effect.

Now we get what we would expect. The image is rotating off of the bottom right area. Cool!
transform-origin right bottom example

In addition to describing the transform-origin, you can specify with exact coordinates.

css transform origin pixel based


translate()

The translate() CSS function changes the position of an element in the horizontal direction, and the vertical direction if you like. If you only needed to re position horizontally or vertically, then you could use translateX() or translateY().

What a beautiful butterfly!
css transform translate two values


The translate() function can also accept negative values for interesting effects.

css transform translate negative values


Here we again use the scale() function to make a neat picture in picture effect.

css transform scale 75


This use of scale() makes for a nice call out effect using an image.

transform scale awesome


Skews

The various skew values of skew(), skewX(), and skewY() are used to change the angle on both or either of the axis by a certain number of degrees. This makes for those slant style effects.

This first example of skewX() gives a tilt effect to the image.

css transform skewx 30 degrees


This use of skewY() creates what almost looks like an opening book effect.

css transform skewY 20 degrees


In the example below, skew() takes two values to skew on two planes.

css transform skew 15 and 20


The three images below use a combination of scale() and rotate() together which creates a really nice effect.

Hover over each picture for a nice effect.


3D Transformations

There are also ways to turn your transformations into effects that give a sense of three dimensions. To do this, one must make use of the perspective property. The perspective property can be assigned to a containing element which instructs the browser to treat child elements as if they are in a three dimensional space. The value given to this property is an integer which specifies the distance from the element’s origin on the z-axis. Recall X and Y axis are horizontal, and the Z-Axis is to and from. Imagine the distance between the letters on a web page and your eyes.

This first example uses these techniques to create some images flipping like cards.

3d transform example

A second example is making use of 3-D Transforms to create some images in a Star Wars like perspective. Quite interesting!

css star wars effect


CSS Transitions and Transformations Summary

CSS transitions and transforms are a great way to give a nice polish to user experiences. They are fully built into the CSS language, therefore the nice effects you can achieve do not require JavaScript. Transitions specify the duration of an element changing its state smoothly over time and without them, a visual state change is abrupt and immediate. To apply transitions you can use the transition-property (required), transition-duration (required), transition-timing-function, and transition-delay properties. Transforms allow designers to move or change the appearance of an element on a 2D plane. They are typically triggered with a hover state change. The four types of transforms are Rotate, Skew, Scale and Translate. It makes sense to use transitions when using transforms in order to produce a smooth and gradual effect.

|