CSS Animations Study Guide
CSS Animations Study Guide
The following study guide aims to practice and exercise the contents that
we have seen in class, in addition to delving into additional topics that complement
those seen in class.
_ 1
[Link]fi[Link]
Table of contents
Introduction 3
Transitions 3
Properties of a transition 4
Transition-Property 5
Timing-duration 6
Timing function: The time function 6
Types of time function 7
Activity 1 9
Delay 9
Summary of the properties 9
Transformations (Transform) 10
Activity 2 12
Transformations with transitions 12
Activity 3 13
Animations 13
Keyframes 16
Duration and delay 16
Address 17
Animations with multiple frames 18
Activity 4 19
Closing questions 20
Answers 21
_ 2
[Link]fi[Link]
Introduction
With CSS, we can bring life and dynamism to our HTML elements using different
techniques, including transitions, transformations, and animations.
● Transitions.
● Transformations (Transform).
● Animations
Transitions
La propiedad transition permite crear animaciones de transición suave entre dos estados de
an element; for example, by changing the background color of a button when hovering over it
It is possible to create a smooth transition that makes the color change more appealing.
for the user.
<!DOCTYPE html>
<html>
<head>
<title>Ejemplo de animación CSS</title>
<metacharset="utf-8">
<style>
button{
background-color:#0077FF
color: white;
padding:10px 20px;
font-size:16px;
border: none;
border-radius:5px;
transition: background-color0.3sease-out;
}
button:hover{
background-color:rgb(255, 140, 0);
_ 3
[Link]fi[Link]
}
</style>
</head>
<body>
Hover the mouse cursor over the button
</body>
</html>
In the HTML file, we create a simple button that has text inside it, while in the
CSS file, we apply styles to the button.
This includes the background color, the text color, the padding, the font size,
the border and the border radius. A transition property is also added, which
indicate to the browser how to animate the change in the background color of
button.
The secret of animation lies in the transition property, which takes four values:
Properties of a transition
In the previous example, when we used the following values for the transition:
background-color 0.3s ease-out we actually use multiple properties for
separated. These are:
● transition-property: background-color;
● transition-duration: 0.3s
● transition-timing-function: ease-out
● transition-delay: 0s
_ 4
[Link]fi[Link]
Transition-Property
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title>Ejemplo de transición con múltiples propiedades</title>
<style>
.box{
width:100px;
height:100px;
background-color: blue;
transition: all3s;
}
.box:hover{
width:200px;
height:200px;
background-color: red;
}
</style>
</head>
<body>
<divclass="box"></div>
</body>
</html>
Now let's modify the transition property so that it only applies to the width of the
box using:transition: width 3s;with this we will see that the properties color and
height changes immediately, but the width property takes 3 seconds to change.
_ 5
[Link]fi[Link]
Timing-duration
The time function is a property that allows you to control the speed curve of the
transition. There are different types of curves for the transition of animations, where two
Common examples are the linear curve and the ease curve.
The linear curve advances the same amount at each step, while the ease curve produces
a more natural and smooth transition, starting slowly, accelerating and decelerating
towards the end. The choice of the appropriate time function depends on the effect that is desired.
achieve in animation.
_ 6
[Link]
Types of time functions
ease-out Starts quickly and then slows down towards the end.
_ 7
[Link]fi[Link]
Let's try a similar example to the previous one by changing the time function to steps and giving it
we will pass the value 5.
<!DOCTYPE html>
<html>
<head>
<title>Ejemplo de animación CSS</title>
<metacharset="utf-8">
<style>
button{
background-color:#0077FF
color: white;
padding:10px 20px;
font-size:16px;
border: none;
border-radius:5px;
transition: background-color1ssteps(5);
}
button:hover{
background-color:rgb(255, 140, 0);
}
</style>
</head>
<body>
Hover the mouse cursor over the button
</body>
</html>
If we now try moving the mouse cursor, we will see that the color change is
paused by intervals, where in a total of 5 intervals it changes color.
_ 8
[Link]fi[Link]
Activity 1
● Try changing the value of step to a different value, what effect does it have?
● Try changing the timer function for the following functions.
○ ease
○ ease-in
○ ease-out
○ linear
It can be difficult to detect changes, try increasing the duration of the transition to
make it simpler.
Delay
With the transition-delay property, we can control how long the transition takes to delay.
start. Like the duration, it can be in seconds or milliseconds.
_ 9
[Link]fi[Link]
Transformations
Transformation Description
_ 10
[Link]fi[Link]
Let's see an example with rotation and scale:
<!DOCTYPE html>
<html>
<head>
<title>Ejemplo de transformación en CSS</title>
<style>
.box{
width:100px;
height:100px;
background-color: blue;
transformrotate(45deg)scale(1.5);
The transformation breaks and scales the box
}
</style>
</head>
<body>
<div class="box"></div>
The box transforms according to the properties of the style
</body>
</html>
When we open the page, we will see a box rotated 45 degrees, we will also see that it is
50% larger due to scale(1.5).
Seeing this at first glance may be complex, but let's review an exercise where
let's add 2 boxes, one normal and another with transformation.
Image 3. Exercise
Source: Desafío Latam.
_ 11
[Link]fi[Link]
Activity 2
● Move a box to the right and down: Create a rectangular box and move it.
50 pixels to the right and 50 pixels down using the transform property
with the function translate().
● Scale an image to twice its original size: Create a div element with a
background image and scale the image to double its original size using the
transform property with the scale() function.
● Rotate a text 180 degrees: Create a p element with text and rotate the text 180
degrees using the transform property with the rotate() function.
We can combine transformations and transitions to achieve interesting effects, let's see
an example:
<!DOCTYPE html>
<html>
<head>
<title>Ejemplo de transición con transformación en CSS</title>
<style>
.box{
width:100px;
height:100px;
background-color: blue;
transform:rotate(0deg);
transition: transform1s;
The transition will apply to the transformation
}
.box:hover{
transformrotate(45deg);
When hovering over it, the box rotates.
}
</style>
</head>
_ 12
[Link]fi[Link]
<body>
<divclass="box"></div>
The box will rotate smoothly when the mouse hovers over it.
</body>
</html>
Activity 3
1. Scale an image to double its original size when hovering over it:
❖ Create a div element with a background image and scale the image to double
its original size when the user hovers the mouse over the element.
To achieve this, the transform property must be used with the scale() function.
and the transition property to make the transition smooth and gradual.
Rotate a box 360 degrees when the mouse moves over it:
❖ Create a rectangular box and make it rotate 360 degrees when the user does
click on it. To achieve this, the transform property must be used with the
function rotate() and the transition property to make the rotation smooth
and gradual.
3. Make a text move to the right when hovering the mouse over it:
❖ Create a p element with text and make it move to the right
when the user hovers the mouse over the element. To achieve this, it is
you must use the transform property with the translate() function and the property
transition to make the scrolling smooth and gradual.
Animations
The CSS animation property allows us to create advanced and customized animations.
on our websites.
One of the most useful features of this property is the ability to define
keyframes, which are key points in animation where values are set
específicos de las propiedades CSS. Gracias a los keyframes, podemos tener un control
I need to know how the animation behaves at each moment, which allows us to create
complex effects.
_ 13
[Link]fi[Link]
On this occasion, we will focus on understanding how keyframes work and how
we can use them to create dynamic and surprising animations. Let's see a
example:
<!DOCTYPE html>
<html>
<head>
<title>Ejemplo de animaciones en CSS</title>
<style>
.box{
width:100px;
height:100px;
background-color: blue;
position: relative;
animation-name: example;
animation-duration:2s;
animation-iteration-count: infinite;
}
@keyframesexample {
from{left:0px; }
to{left:200px;
The animation will move the box from 0px to 200px to the left
}
</style>
</head>
<body>
<div class="box"></div>
The box is animated with the defined animation property.
</body>
</html>
_ 14
[Link]fi[Link]
In animations, there are many key concepts, and we summarize them in this table.
Concept Description
_ 15
[Link]
Keyframes
Keyframes are key points in an animation that allow us to set the values
of the CSS properties at specific moments, where when using several of these
keyframes in an animation create the illusion of smooth movement between each point.
@keyframes example {
from{left:0px; }
to{left:200px; }
}
In the first example we saw, the keyframes were from (desde) and to (hasta) and the animation
It consisted of the movement from the first point to the second.
For animations, the concepts of duration and delay are the same as for
transitions. Withanimation-duration we control how long it takes to
complete the animation and withanimation-delayHow long does it take to start.
El código anterior retrasa el comienzo de la animación 1 segundo y haría que cada iteración
the animation will last 2 seconds.
_ 16
[Link]
Address
Let's look at an example of an animation without this property and then add it to see the
difference.
/* CSS */
.container{
width:100%;
display: flex;
justify-content: center;
align-items: center;
}
.text{
font-size:3em;
animation: move2sinfinite;
}
@keyframesmove {
from{
transformtranslateX(0);
}
to{
transform:translateX(200px);
}
}
In each iteration of the animation, we see that the box moves automatically,
resulting in an animation that looks strange, we can easily improve this
adding the propertyanimation-direction: alternate .
_ 17
[Link]fi[Link]
Our resulting CSS would look like this:
/* CSS */
.container{
width:100%;
display: flex;
justify-content: center;
align-items: center;
}
.text{
font-size:3em;
animation: move2sinfinite;
animation-direction: alternate;
}
@keyframes move {
from{
transformtranslateX(0);
}
to{
transformtranslateX(200px);
}
}
/* CSS */
.container{
width:100%;
height:100vh;
display: flex;
justify-content: center;
align-items: center;
}
_ 18
[Link]fi[Link]
.text{
position: absolute;
font-size:48px;
animation: move4sease infinite;
}
@keyframesmove {
0% {
transformtranslateX(0);
}
25%
transformtranslateX(100px);
}
50%
transformtranslateX(0);
transformrotate(180deg)
;
} 75% {
transform:translateX(-100px);
}
100%
transformtranslateX(0);
}
}
Activity 4
_ 19
[Link]fi[Link]
Closing questions
What CSS property is used to create a transformation that scales or increases?
does it decrease the size of an element?
a) transform-scale
b) transform-rotate
c) transform-translate
d) transform-skew
_ 20
[Link]fi[Link]
Answers
1 A
2 B
3 A
4 C
5 A
6 B
_ 21
[Link]fi[Link]