Skip to content
Created: December 25, 2015

1. What is transition? ​

transition is an property transition effect provided by CSS3. It allows attribute changes to occur smoothly within a certain period of time, rather than jumping immediately.

📌 Simple understanding: From "old style" → "new style", there is a smooth animation process in the middle.

2. Basic grammar format ​

css
transition: property duration timing-function delay;
ParametersDescriptionExample
propertyThe name of the property that needs to be transitioned (such as width, color)all means all animatable properties
durationanimation duration0.5s, 200ms
timing-functionSpeed curve function (control animation rhythm)ease, linear, ease-in
delayanimation delay time0s, 1s

🔹 Example 1: The simplest hover transition ​

:::demo [vanilla]

html
<html>
<div class="box"></div>
</html>
<style>
.box {
  width: 100px;
  height: 100px;
  background: #42b983;
  transition: all 0.5s ease;
}

.box:hover {
  width: 200px;
  background: #ff8c00;
}
</style>

:::

✅ Effect:

When the mouse moves in, the width of the box becomes wider and the color gradually changes. The whole process lasts for 0.5 seconds.

3. Four commonly used attributes of transition ​

1️⃣ transition-property ​

Specify which properties need to be transitioned.

css
transition-property: width, background-color;

If you write all, it means all animatable properties.

2️⃣ transition-duration ​

Set transition time.

css
transition-duration: 0.3s;

3️⃣ transition-timing-function ​

Control the animation speed curve.

ValueDescriptionCurve Characteristics
linearUniform speed——
easeDefault value, slow start and end🚶‍♂️
ease-inSlow in and fast out⤴️
ease-outFast in and slow out⤵️
ease-in-outSlow in, slow out↗️↘️
cubic-bezier(n, n, n, n)Custom Bezier curve🎨

Example:

css
transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);

👉 in https://cubic-bezier.com The curve can be adjusted online.

4️⃣ transition-delay ​

Set delay time.

css
transition-delay: 0.2s;

🔹 Example 2: How to write four attributes separately ​

css
transition-property: width;
transition-duration: 1s;
transition-timing-function: ease-in-out;
transition-delay: 0.5s;

Equivalent to 👇

css
transition: width 1s ease-in-out 0.5s;

4. Multiple attribute transition writing methods ​

css
transition:
  width 1s ease-in,
  height 0.5s linear,
  background-color 0.8s ease-out;

✅ Each attribute can define different times and curves.

5. Common application scenarios ​

ScenarioExample
🎨 hover interactionButton color change, picture zoom
🧭 Navigation menuMouse hover expansion animation
📦 Modal boxAppear/disappear gradient effect
🔄 loading animationcombined with transform to rotate and scale

🔹 Example 3: Button hover animation ​

:::demo [vanilla]

html
<html>
<button class="btn">Hover Me</button>
</html>
<style>
.btn {
  padding: 10px 20px;
  background: #409eff;
  color: white;
  border: none;
  border-radius: 6px;
  transition: background-color 0.3s, transform 0.3s;
}

.btn:hover {
  background: #66b1ff;
  transform: scale(1.05);
}
</style>

:::

✅ The transition is more natural and has a slight amplification effect.

6. Use transition with transform (very common) ​

css
.card {
  transition: transform 0.5s ease;
}
.card:hover {
  transform: translateY(-10px) scale(1.05);
}

🎯 Advantages: transform will not trigger layout reflow (reflow) and has better performance.

7. Performance optimization suggestions ​

SuggestionsReasons
✅ Prioritize using transform, opacityDoes not trigger layout and redrawing, best performance
❌ Avoid transition width / heightcan cause reflow
✅ Use will-change to optimize in advanceNotify the browser to prepare the animation layer in advance
✅ Simplify transition attributesAvoid using transition: all

Example of optimized writing:

css
.box {
  transition: transform 0.3s ease;
  will-change: transform;
}

🧭 8. Transition vs animation comparison ​

Contrast itemstransitionanimation
Trigger methodStatus change (such as hover)Automatic loop playback
Whether trigger conditions are requiredRequiredNot required
Control complexitySimpleHigh (supports key frames)
Applicable scenariosSimple transitionContinuous animation, complex motion effects

💡 9. Structured summary (mind map structure) ​

CSS3 Transition(过渡)
 ├── 定义:样式变化的平滑动画
 ├── 语法:transition: property duration timing delay;
 ├── 四个属性
 │   ├── transition-property
 │   ├── transition-duration
 │   ├── transition-timing-function
 │   └── transition-delay
 ├── timing-function 曲线
 │   ├── ease / linear / ease-in / ease-out / ease-in-out
 │   └── cubic-bezier()
 ├── 应用场景
 │   ├── hover 动效
 │   ├── 导航展开
 │   ├── 按钮放大
 │   └── 模态框渐变
 ├── 优化建议
 │   ├── 使用 transform / opacity
 │   ├── 避免 all
 │   ├── will-change 提前优化
 └── 与 animation 对比
     ├── 触发:transition需触发,animation自动
     ├── 复杂度:transition简单,animation强大