Quick CSS Transitions and Animations Guide
Quick CSS Transitions and Animations Guide
1
Example 1: Button Hover Transition 3
Example 2: Simple Animation 3
CSS Transition and Animation Properties 4
Transition Properties: 4
Animation Properties: 4
Advanced Examples 4
Example 3: Bouncing Ball Animation 5
Example 4: Rotating and Scaling Animation 5
Exercises 6
Exercise 1: Create a Hover Effect 6
Exercise 2: Build a Loading Animation 6
Multiple-Choice Questions 7
Question 1: 7
Question 2: 7
Question 3: 8
CSS Transitions and Animations are powerful tools to enhance the user experience by adding
smooth visual effects. This guide covers the basics, advanced features, and practical examples
of CSS Transitions and Animations.
CSS Transitions allow changes in CSS properties to occur smoothly over a specified duration
rather than happening instantly.
Syntax:
selector {
transition: property duration timing-function delay;
}
CSS Animations
CSS Animations allow you to animate changes in CSS properties using keyframes.
Syntax:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
@keyframes animation-name {
0% { /* Initial styles */ }
100% { /* Final styles */ }
}
selector {
animation: name duration timing-function delay iteration-count
direction;
}
Examples
Explanation:
3
animation: move 2s infinite alternate;
}
@keyframes move {
0% {
transform: translateX(0);
}
100% {
transform: translateX(200px);
}
}
</style>
Explanation:
● @keyframes move: Defines the animation's starting (0%) and ending (100%) states.
● animation: move 2s infinite alternate;: Applies the animation, making it
repeat infinitely and reverse direction alternately.
Transition Properties:
Animation Properties:
Advanced Examples
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
4
Example 3: Bouncing Ball Animation
<div class="ball"></div>
<style>
.ball {
width: 50px;
height: 50px;
background: red;
border-radius: 50%;
position: relative;
animation: bounce 1.5s infinite ease-in-out;
}
@keyframes bounce {
0%, 100% {
top: 0;
}
50% {
top: 200px;
}
}
</style>
Explanation:
● The @keyframes bounce makes the ball move up and down by modifying the top
position.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
}
50% {
transform: rotate(180deg) scale(1.5);
}
100% {
transform: rotate(360deg) scale(1);
}
}
</style>
Exercises
1. Create a button that changes its text color and background color with a smooth transition
over 1 second.
Solution:
Solution:
<div class="spinner"></div>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
<style>
.spinner {
width: 50px;
height: 50px;
border: 5px solid lightgray;
border-top: 5px solid blue;
border-radius: 50%;
animation: spin 1s infinite linear;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
Multiple-Choice Questions
Question 1:
1. animation-duration
2. transition-time
3. transition-duration
4. duration
Answer: 3. transition-duration
Explanation: transition-duration specifies the time a transition takes to complete.
Question 2:
7
4. None of the above.
Question 3:
1. background-color
2. transform
3. display
4. opacity
Answer: 3. display
Explanation: The display property cannot be transitioned as it does not have intermediate
states.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis