CSS Transition & Filter

CSS Transition

Transition is the simplest way to animate CSS — specify which properties change smoothly, how long, and what timing.

Transition Properties

Property Values Purpose
transition-property all, color, transform Which properties animate
transition-duration 0.3s, 500ms How long the transition takes
transition-timing-function ease, linear, ease-in, ease-out Speed curve
transition-delay 0s, 0.2s Delay before starting
transition all 0.3s ease Shorthand

Example

HTML
<style>
.button {
  background: #3498db;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  transition: all 0.3s ease;
}
.button:hover {
  background: #2980b9;
  transform: scale(1.05);
}
</style>
<button class="button">Hover me</button>
▶ Try it Yourself

CSS Filter

Filters apply visual effects to elements:

Filter Functions

Function Values Effect
blur() 5px, 10px Gaussian blur
brightness() 0.5, 1.5 Darker/brighter
contrast() 0.5, 2 Less/more contrast
grayscale() 0%, 100% Grayscale amount
hue-rotate() 90deg, 180deg Rotate hue
invert() 0%, 100% Invert colors
opacity() 0.5, 1 Transparency
saturate() 0.5, 2 Less/more saturated
sepia() 0%, 100% Sepia tone

Example

HTML
<style>
.image {
  filter: grayscale(100%);
  transition: filter 0.3s ease;
}
.image:hover {
  filter: grayscale(0%);
}
</style>
<img class="image" src="/assets/images/tutorials/html-example-photo.webp" alt="Photo">
▶ Try it Yourself

Combining Transition + Filter

Example

HTML
<style>
.card {
  filter: blur(2px) brightness(0.8);
  transition: filter 0.5s ease;
}
.card:hover {
  filter: blur(0) brightness(1);
}
</style>
<div class="card">Hover to sharpen and brighten</div>
▶ Try it Yourself

❓ FAQ

Q What's the difference between transition and animation?
A transition needs a trigger (like hover) and only goes from state A to state B — no loops. animation doesn't need a trigger, can auto-play, supports multiple keyframes, loops, and reverse. Simple interactions use transition; complex auto-playing effects use animation.
Q Can all CSS properties be transitioned?
A No. Only some properties support transitions. Best performance with `transform` and `opacity` — browsers can GPU-accelerate them. Don't animate `width`, `height`, `top`, `left` — causes reflow and jank.
Q Does filter: blur() cause performance issues?
A Yes. blur is GPU-intensive — large area blur causes performance drops. Best to use blur on small elements only, or only in brief interactions like hover. Don't apply blur to videos or large images.

📖 Summary

📝 Exercises

  1. Create a button that changes background color in 0.3s on hover, with slight scale increase.
  2. Create an image gallery: images show grayscale by default, smoothly become colorful on hover.
  3. Create a "navigation underline animation" — bottom line expands from center on hover.
  4. Use filter + transition to make images brighter and slightly blurred on hover.
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏