Animations, transitions and transformations
Making a website captivating and interactive is key in this modern digital world, where millions of websites are in existance at any one time; animations, transitions and transformations can help make this possible.
Animations
CSS3 has native support for animations, which allow for the animating of transitions between CSS styles. CSS animations are made up of two key components:
- CSS styles which describe the animation
- Keyframes which indicate the start, intermediate and end states of the animation
Key benefits of CSS animations are the bypassing of JavaScript, which in most instances will have performance deficits compared to the built-in rendering engine of the browser, enabling a web designer who does not know JavaScript to create animations for web pages.
Animation properties
CSS provides a multitude of properties for configuring an animation, and just like other properties, it provides a shorthand animation
property.
animation-delay
- Used to specify the delay between when an element loads and the start of the animation.
animation-direction
- Specifies the direction of an animations first run, forward or backward, and whether subsequent runs will alternate direction or reset.
animation-duration
- Used to specify an animations length.
animation-fill-mode
- Used to specify the way styles applied to an animation before and after it executes.
animation-iteration-count
- Specifies how many times an animation should repeat.
animation-name
- Specifies the name of animation for targeting by the keyframe rules.
animation-play-state
- Specifies whether an animation should be running or stopped.
animation-timing-function
- Used to specify the rate of transitions through keyframes.
Specifying keyframes
Animation keyframes are specified using the @keyframes
at-rule, an animation-name
must be specified, a from
rule and a
to
rule at a minimum. The following example demonstrates an animation sliding in a paragraph from off the left of the page to right:
- Press the Render button or reload the page to see the animation.
Intermediary keyframes can be added to make the transition smoother or add effects by specifying a percentage in the keyframes:
This example increases the size of the font during the animation and makes the change permanent by applying the animation-fill-mode: forwards
property.
Let's create an animation that infinitely repeats sliding-in text from the right instead:
To prevent an overflow from occuring when sliding-in the content, the animated element was placed inside a div
with overflow: hidden
applied. To
prevent the text from starting as a huge gobbled mess, we also apply a larger than normal width and reduce it to the size of the container (100%
) during the
course of the animation. Setting the animation-iteration-count
to infinite
repeats the animation over and over.
The animation
shorthand
The animation
shorthand property can be used to specify all of the properties of an animation:
animation: duration timing-function delay iteration-count direction fill-mode play-state name
The prior slide-in from the right animation, without infinitely looping but instead alternating in direction for 3 iterations, could then be specified as follows:
Transitions
Transitions are useful when there is some kind of animation whose speed you want to control, changes occur through automatic calculations that follow a specified acceleration curve.
Transition properties
We can define transitions using a selection of CSS properties, these transitions can be applied to most CSS properties, a list of properties can be found here.
Just like the animation
property, a shorthand transition
property is available. First, lets go through the available properties that make it up though:
transition-property
- Used to specify which CSS properties a transition should be applied to.
transition-duration
- Used to specify the duration of a transition, a single value can be specified or multiple to allow properties to transition over different time periods.
transition-timing-function
- Used to specify how intermediate values for properties are computed, done by way of an easing function.
transition-delay
- Used to specify the time between a property changing and the transition beginning.
The short-hand property follows the form:
transition: property duration timing-function delay;
Example transitions
The first example demonstrates a box that rotates 360 degrees, shrinks in size and changes colour on hover:
Hello world!
The transition
property was applied to the box
class rather than the psuedo-class hover
selector. This enables the animation
to occur on hover and when the cursor is removed from the hover.
CSS transforms
CSS transformations allow for changing the shape and position of content without disrupting nromal flow, this is done by modifying the coordinates of the element
in the document. Transformations are only applicable to block
-level elements.
Transform properties
There are two major properties for defining CSS transformations:
transform
- Used to specify a space-separated list of transformations to apply to an element, one after the other.
transform-origin
- Used to specify the origin position of an element, by default it is at the centre of an element.
Transform functions
To use the transform properties, we supply transform functions to manipulate an elements shape and position.
Functions for rotation
Rotation functions rotate an element around a specific point or axis:
rotate()
- Rotates an element around a fixed point on the 2D plane.
rotate3d()
- Rotates an element around a fixed axis on the 3D plane.
rotateX()
- Rotates an element around the horizontal axis.
rotateY()
- Rotates an element around the vertical axis.
rotateZ()
- Rotates an element around the z-axis (forwards and backwards).
Functions for skewing
Skew functions will change the angle of an element along a specific point or axis:
skew()
- Skews an element on the 2D plane.
skewX()
- Skews an element on the horizontal axis.
skewY()
- Skews an element on the vertical axis.
Functions for scaling
Scaling functions modify an elements size from a specific point or axis:
scale()
- Scales an element up or down on the 2D plane.
scale3d()
- Scales an element up or down on the 3D plane.
scaleX()
- Scales an element up or down on the horizontal axis.
scaleY()
- Scales an element up or down on the vertical axis.
scaleZ()
- Scales an element up or down on the z-axis.
Functions for translations
Translation functions modify the position of an element from a specific point or axis:
translate()
- Translates an element around a fixed point on the 2D plane.
translate3d()
- Translates an element around a fixed axis on the 3D plane.
translateX()
- Translates an element around the horizontal axis.
translateY()
- Translates an element around the vertical axis.
translateZ()
- Translates an element around the z-axis (forwards and backwards).
Example transforms
Horizontally and vertically centred text
The first transformation we will demonstrate is the centering of text both horizontally and vertically, which before CSS transforms and Flexbox was a tricky thing to do with just the standard text stylings, requiring clever and careful attention to the padding of the text.
To centre text with a transform, we will need to make use of the relative
and absolute
positioning systems. Let's see what we mean by this:
Hello world!
In the above example, we absolutely positioned the text and applied a 50%
value to top
and left
. This hasn't quite centred the
text though, it is slightly off. This is where a translation becomes helpful:
Hello world!
So what has happened here? Well, when we applied a 50%
value to top
and left
, it placed the top-left corner (origin) of the element
in the middle of the div
. To centre the actual text, we then used translate(-50%, -50%)
to move the text half of the width of the element to the left
and half the height upwards. This trick can also be used with non textual elements. For example, the following centres a green square div
:
Rotating and scaling an element on hover
The rotate()
function can be used to rotate at an element clockwise or counter-clockwise given some degrees:
Hello world!
To ensure the element remained fully visible after rotating and scaling it up in size, the translate()
function was supplied
to adjust the position of the element on hover. Be careful how much you adjust its position by though, adjusting it too much will result in the element rapidly
switching between a hover and non-hover state as the element moves from out of the cursor and back in. Here is an example of this:
Hello world!
This would be quite disconcerting for a viewer of your website to come across.
Combining transformations with animations
The following example demonstrates mixing transformations with animations to create movement effects:
Here is a handy article by CSS Tricks on transforms.