Lesson 39 / الدرس 39

Transitions / الانتقالات

Make a property change smoothly instead of snapping instantly, with transition-property, -duration, -timing-function, -delay, and the shorthand that combines them.

اجعل تغيّر خاصية ما تدريجيًا بدل أن يقفز فجأة، عبر transition-property و -duration و -timing-function و -delay، والاختزال الذي يجمعها معًا.

You already know that a pseudo-class like :hover lets a rule apply only while the pointer sits over an element. By default, the change from the normal state to the :hover state is instant — one frame the button is blue, the next it is dark blue, with nothing in between. A transition fills in that gap: instead of jumping, the property animates smoothly from its old value to its new one over a duration you choose.

Four properties that control a transition

PropertyControlsExample
transition-propertyWhich property animatesbackground-color
transition-durationHow long it takes0.3s
transition-timing-functionHow the speed changes over that timeease-in-out
transition-delayHow long to wait before starting0.1s

All four together describe one transition. Only transition-property and transition-duration are required for anything visible to happen.

.btn {
    background-color: navy;
    transition-property: background-color;
    transition-duration: 0.3s;
    transition-timing-function: ease-in-out;
}

.btn:hover {
    background-color: tomato;
}
Hover the button in the preview. The color change now takes 0.3 seconds and eases in and out, instead of snapping.

The shorthand: transition

Writing all four longhand properties gets repetitive, so almost every real stylesheet uses the shorthand instead: transition: property duration timing-function delay;. The delay is optional and can be left off entirely.

.btn {
    background-color: navy;
    transition: background-color 0.3s ease-in-out;
}

.btn:hover {
    background-color: tomato;
}
The exact same effect as before, in one line. Add a fourth value, like 0.15s, and the color change would wait that long before it starts.
  • ease — starts slow, speeds up, ends slow (the default)
  • linear — the same speed from start to finish
  • ease-in — starts slow, then speeds up
  • ease-out — starts fast, then slows down

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

Preview / المعاينة

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

1. Which property controls how long a transition takes?

2. You wrote transition only inside a :hover rule. What happens?

3. In transition: background-color 0.3s ease-in-out 0.1s;, what does the last value, 0.1s, set?

Your task / مهمتك

Style a button with a base background color, and a different background color on :hover. Add a transition to the base selector, with a duration of at least 0.2s, so the color animates smoothly in both directions.

صمّم زرًا بلون خلفية أساسي، ولون خلفية مختلف عند :hover. أضف transition إلى المُحدِّد الأساسي، بمدة 0.2 ثانية على الأقل، ليتحرك اللون بسلاسة في الاتجاهين.

  • transition is set on the base .btn selector, not only inside :hover transition مضبوطة على المُحدِّد الأساسي .btn، لا داخل :hover وحده
  • The duration is at least 0.2s المدة 0.2 ثانية على الأقل
  • The background color visibly animates both entering and leaving :hover يتحرك لون الخلفية بوضوح عند الدخول إلى :hover وعند مغادرته
How do you want to submit? / كيف تريد التسليم؟