Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Create a Simple Keyframe Animation with CSS
📖 Scenario: You want to make a box on a webpage move smoothly from left to right and back again. This will catch the visitor's eye and make your page more lively.
🎯 Goal: Build a CSS animation using @keyframes that moves a square box horizontally across the screen and back continuously.
📋 What You'll Learn
Create a CSS @keyframes rule named slide that moves an element from left 0 to left 200px and back.
Create a CSS class called box that sets width, height, background color, and position relative.
Apply the slide animation to the box class with a duration of 4 seconds and infinite repetition.
Use semantic CSS properties and ensure the animation runs smoothly.
💡 Why This Matters
🌍 Real World
Keyframe animations are used on websites to create smooth movements and transitions that attract user attention and improve user experience.
💼 Career
Web developers often use CSS animations to enhance the look and feel of websites, making them more interactive and visually appealing.
Progress0 / 4 steps
1
Create the box style
Write CSS to create a class called box that sets width to 100px, height to 100px, background-color to blue, and position to relative.
CSS
Hint
Use the CSS class selector .box and set the properties exactly as described.
2
Define the keyframes animation
Write a CSS @keyframes rule named slide that moves the element's left property from 0px at 0% to 200px at 50%, then back to 0px at 100%.
CSS
Hint
Use @keyframes slide and define the left property at 0%, 50%, and 100% as described.
3
Apply the animation to the box
Add to the .box class the CSS property animation with the value slide 4s infinite to run the slide animation continuously every 4 seconds.
CSS
Hint
Use the animation property inside .box with the exact value slide 4s infinite.
4
Add HTML to show the animated box
Write the HTML code to create a <div> element with the class box so the animation is visible on the page.
CSS
Hint
Use a <div> tag with the class attribute set to box.
Practice
(1/5)
1. What does the @keyframes rule do in CSS animations?
easy
A. Stops the animation immediately
B. Applies the animation to an HTML element
C. Sets the animation duration and delay
D. Defines the stages of an animation with style changes over time
Solution
Step 1: Understand the role of @keyframes
The @keyframes rule defines how styles change at different points during the animation timeline.
Step 2: Differentiate from animation properties
Properties like animation-duration or animation-name apply the animation, but @keyframes sets the actual style changes.
Final Answer:
Defines the stages of an animation with style changes over time -> Option D
A. Missing semicolon after width: 200px in keyframes
B. Incorrect animation property name; should be animation
C. Animation duration must be in milliseconds, not seconds
D. Missing @keyframes keyword
Solution
Step 1: Check syntax inside @keyframes
Each CSS declaration must end with a semicolon. The width: 200px line is missing a semicolon.
Step 2: Verify animation properties
The properties animation-name, animation-duration, and animation-iteration-count are correct and properly used.
Final Answer:
Missing semicolon after width: 200px in keyframes -> Option A
Quick Check:
CSS declarations need semicolons [OK]
Hint: Always end CSS declarations with semicolons [OK]
Common Mistakes:
Omitting semicolons inside keyframes
Confusing animation property names
Using wrong units for duration
5. You want to create a bouncing ball effect using keyframe animations. Which keyframe sequence best simulates a ball dropping and bouncing back up smoothly?