CSS Animation Get Certified
What is CSS Animation?
CSS Animation lets you change the style of an element gradually over time. You first describe the sequence of styles inside an @keyframes rule, then attach that animation to an element with the animation property (or its longhand versions). Animations run automatically and do not require JavaScript.
Why Use CSS Animation?
Animations add life to a page: loading spinners, bouncing elements, fading text, and smooth hover feedback. Because they are handled by the browser’s compositor, well-written CSS animations are usually smoother and more efficient than the same effects written in JavaScript.
Important Animation Properties
Step 1: Create Your First Animation
What this code does: Defines a named animation called moveBox that slides a blue square 200 pixels to the right over 3 seconds. The animation runs once when the page loads.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
margin: 0;
}
.demo-box {
width: 100px;
height: 100px;
background: #007BFF;
border-radius: 8px;
animation: moveBox 3s;
}
@keyframes moveBox {
from { transform: translateX(0); }
to { transform: translateX(200px); }
}
</style>
</head>
<body>
<div class="demo-box"></div>
</body>
</html>
Step 2: Change Background Color
What this code does: Creates a continuous color-cycle animation that moves smoothly from red → yellow → green and then repeats forever. Percentage keyframes give precise control over the timing of each color.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
margin: 0;
}
.demo-box {
width: 120px;
height: 120px;
border-radius: 8px;
animation: colors 4s infinite;
}
@keyframes colors {
0% { background: red; }
50% { background: yellow; }
100% { background: green; }
}
</style>
</head>
<body>
<div class="demo-box"></div>
</body>
</html>
Step 3: Rotate an Element
What this code does: Continuously spins an orange circle. The linear timing function keeps the speed constant, and infinite makes the rotation never stop — a classic loading-spinner technique.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
margin: 0;
}
.demo-circle {
width: 100px;
height: 100px;
background: #ff6600;
border-radius: 50%;
animation: spin 2s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="demo-circle"></div>
</body>
</html>
Step 4: Fade In an Element
What this code does: Fades a heading in from completely transparent (opacity: 0) to fully visible (opacity: 1) over 3 seconds. This is a classic entrance effect for titles and important content.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
margin: 0;
}
.demo-title {
animation: fade 3s;
}
@keyframes fade {
from { opacity: 0; }
to { opacity: 1; }
}
</style>
</head>
<body>
<h2 class="demo-title">Welcome to CSS Animation</h2>
</body>
</html>
Step 5: Create a Hover Animation
What this code does: Uses a CSS transition (not @keyframes) to scale a button up and darken its background when the mouse hovers over it. Transitions are ideal for simple interactive feedback that starts and stops with user action.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
margin: 0;
}
.demo-btn {
padding: 12px 25px;
background: #007BFF;
color: white;
border: none;
border-radius: 6px;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
}
.demo-btn:hover {
transform: scale(1.2);
background: #0056b3;
}
</style>
</head>
<body>
<button class="demo-btn">Hover Me</button>
</body>
</html>
Complete CSS Animation Example
What this code does: Creates a bouncing gradient box that moves up and down continuously. The alternate direction makes the animation reverse on every cycle, producing a natural bouncing effect without writing extra keyframes.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 60px 24px;
}
.demo-box {
width: 120px;
height: 120px;
margin: 20px auto;
background: linear-gradient(#00c6ff, #0072ff);
border-radius: 12px;
animation: bounce 2s infinite alternate;
}
@keyframes bounce {
from { transform: translateY(0); }
to { transform: translateY(-100px); }
}
</style>
</head>
<body>
<h2>Bouncing Box</h2>
<div class="demo-box"></div>
</body>
</html>
Advantages of CSS Animation
Best Practices
- Use animations to improve the user experience, not distract users.
- Keep animation durations natural (usually between 0.2 s and 2 s).
- Give
@keyframesmeaningful names so the code stays readable. - Prefer
transformandopacityfor the best performance — they are hardware-accelerated. - Use
infiniteonly when the animation should truly run forever (spinners, ambient effects). - Combine
animation-direction: alternatewith simple keyframes to create bouncing or pulsing effects easily. - Test animations on both desktop and mobile devices.
- Respect users who prefer reduced motion by providing a
prefers-reduced-motionmedia query when possible.
Web Resources on CSS Animation
1. MDN Web Docs : Using CSS animations
2. Animate.style : Just add water CSS animations
3. MDN Web Docs : animation CSS property
4. Web.dev : Animations
Click “Run Code” to see the result here.
CSS Syntax Example
The CSS syntax consists of a selector and a declaration block.
h1 { color: blue; font-size: 36px;}Selector: h1 – targets all <h1> elements
Property: color and font-size
Value: blue and 36px
Your CSS Applied Below:
This is an H1 heading with your CSS
This is an H2 heading (not affected)
This paragraph is not affected by the h1 selector.
Welcome to CSS
This page is styled using CSS.
The heading and paragraph use your CSS rules.
- List item with your styles
- Another list item
🧪 Test Your CSS Code
Edit the CSS code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.