انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Web: home
0. Lab tour1
1. HTML: the page's content7
2. CSS: the page's look6
3. JavaScript: the actions7
4. The project: the to-do list3
5. Pro CSS: shadows and movement5
6. Arrays and building the page with code3
7. Events and forms3
8. Objects and time3
9. Canvas: drawing and games3
10. The final project: the memory game3

animation: movement that never stops

Section 5: Pro CSS: shadows and movementLesson 5.3 · ⏱ 10 min

In @keyframes we describe the movement step by step: where it starts, and where it ends. Then we give it to any element with animation.

  • spin 2s linear infinite: the movement's name, two seconds long, at a steady speed, and repeating for ever.
  • In jump we describe three moments: the start 0%, the middle 50% and the end 100%.
HTML
<div class="sky">
  <span class="star">⭐</span>
  <span class="ball">🏀</span>
</div>
CSS
body { font-family: Tahoma, sans-serif; }
.sky { display: flex; gap: 60px; justify-content: center; padding: 40px; font-size: 50px; }

@keyframes spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}
@keyframes jump {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-40px); }
}

.star { animation: spin 2s linear infinite; }
.ball { animation: jump 0.8s ease-in-out infinite; }
🖥️ The page in Robo Web:The page in Robo Web
🖥️ What happens on the stage?
In the lab the star spins and the ball jumps without stopping. The picture is one moment of it.
  1. Add a third movement grow that makes an element bigger, then smaller.
  2. Make the ball faster: 0.4s.

📌 Another example: a beating heart

We make the heart bigger and smaller without stopping.

🌐 A beating heart
HTML
<div class="heart">❤️</div>
CSS
body { text-align: center; padding-top: 40px; }
.heart {
  display: inline-block;
  font-size: 80px;
  animation: beat 0.8s ease-in-out infinite;
}
@keyframes beat {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.3); }
}
🖥️ The page in Robo Web:The page in Robo Web
✍️ Practise
Try the example here: copy the code into its tabs, change it, and watch the page change.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

A square whose colour changes from red to blue and back.

👀 Show the solution
🌐 Solution
HTML
<div class="box"></div>
CSS
.box {
  width: 100px;
  height: 100px;
  animation: colors 2s infinite alternate;
}
@keyframes colors {
  from { background: red; }
  to   { background: blue; }
}
🖥️ The page in Robo Web:The page in Robo Web

Result: a colour that keeps changing.

Medium

A car that moves from left to right and back.

👀 Show the solution
🌐 Solution
HTML
<div class="road"><div class="car">🚗</div></div>
CSS
.road { height: 80px; background: #ddd; position: relative; }
.car {
  position: absolute;
  font-size: 50px;
  animation: drive 3s linear infinite alternate;
}
@keyframes drive {
  from { left: 0; }
  to   { left: 80%; }
}
🖥️ The page in Robo Web:The page in Robo Web

Result: the car moves.

Hard

Three dots that jump one after the other (animation-delay).

👀 Show the solution
🌐 Solution
HTML
<div class="dots"><span>●</span><span>●</span><span>●</span></div>
CSS
.dots { font-size: 40px; padding: 30px; color: #5B3DF5; }
.dots span {
  display: inline-block;
  animation: hop 0.9s ease-in-out infinite;
}
.dots span:nth-child(2) { animation-delay: 0.15s; }
.dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes hop {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-20px); }
}
🖥️ The page in Robo Web:The page in Robo Web

Result: dots jumping in order.

Finished the lesson? Mark it so your progress counts.