Lesson 40 / الدرس 40

Transforms & animations / التحويلات والحركات

Move, rotate and scale an element with transform without disturbing the layout around it, then build a looping animation with @keyframes.

حرّك عنصرًا ودوّره وكبّره أو صغّره باستخدام transform دون أن تُخل بالتخطيط من حوله، ثم ابنِ حركة متكررة بواسطة @keyframes.

Properties like margin or top move an element by pushing everything around it too — the box model shifts, and neighboring elements react. transform is different: it moves, rotates or resizes an element visually, on top of the page, without changing the space it reserves in the layout at all. Combined with the transition you just learned, it is how a hovered card lifts or a button grows slightly without anything around it jumping out of the way.

Three transform functions: translate, rotate, scale

.card {
    transition: transform 0.2s ease-out;
}

.card:hover {
    transform: translateY(-8px);
}
translateY(-8px) shifts the element 8 pixels up. translateX() moves it sideways, and translate(x, y) does both at once. Hover the card — it lifts, and nothing else on the page moves to make room, because as far as layout is concerned it never left.
.icon {
    transition: transform 0.3s ease-in-out;
}

.icon:hover {
    transform: rotate(20deg) scale(1.2);
}
rotate(20deg) turns the element, and scale(1.2) grows it to 120% of its size — scale(0.8) would shrink it instead. Listing more than one function inside transform, separated by spaces, applies all of them together.

A repeating animation with @keyframes

A transition only ever runs in response to something changing, like :hover. An animation plays on its own, with no trigger needed, and can repeat forever. First you describe the stages inside a named @keyframes rule using percentages — 0% is the start, 100% is the end, and you can add stops in between. Then you attach it to an element with animation-name plus at least animation-duration.

@keyframes pulse {
    0%   { transform: scale(1); }
    50%  { transform: scale(1.15); }
    100% { transform: scale(1); }
}

.badge {
    animation-name: pulse;
    animation-duration: 1.2s;
    animation-iteration-count: infinite;
}
The badge grows a little and shrinks back, forever, with no pointer or click involved. animation-name must match the @keyframes name exactly, and animation-iteration-count: infinite is what makes it loop instead of running once.
PropertyControls
animation-nameWhich @keyframes rule to play
animation-durationHow long one cycle takes
animation-timing-functionHow the speed changes during a cycle
animation-iteration-countHow many times it repeats — a number, or infinite

The animation properties used in this lesson. They can also be combined into an animation shorthand, in this same order.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. Which function moves an element visually without affecting the layout of elements around it?

2. What connects a @keyframes rule to the element that should play it?

3. Why is animating transform usually smoother than animating top or width?

Your task / مهمتك

Build a small notification badge that pulses continuously using @keyframes and animation, and a button that lifts and grows slightly on :hover using transform with a transition.

ابنِ شارة إشعار صغيرة تنبض باستمرار باستخدام @keyframes و animation، وزرًا يرتفع ويكبر قليلًا عند :hover باستخدام transform مع transition.

  • Defines an @keyframes rule and applies it with a matching animation-name تُعرّف قاعدة @keyframes وتُطبّقها بـ animation-name مطابق
  • The badge repeats forever with animation-iteration-count: infinite الشارة تتكرر إلى الأبد بـ animation-iteration-count: infinite
  • The button uses transform on :hover, paired with a transition on its base selector الزر يستخدم transform عند :hover، مع transition على مُحدِّده الأساسي
How do you want to submit? / كيف تريد التسليم؟