CSS: CSS Transition & Filter
1. CSS Transition
Transition is the simplest way to animate CSS — specify which properties change smoothly, how long, and what timing.
(1) 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
▶ Try it Yourself2. CSS Filter
Filters apply visual effects to elements:
(1) 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
▶ Try it Yourself3. Combining Transition + Filter
▶ Example
▶ Try it Yourself❓ FAQ
Q How do I use CSS transition, and what do its four values mean?
A The format is
transition: property duration timing-function delay;, e.g. transition: background-color 0.3s ease 0.1s; — the property to animate, how long it takes, the speed curve, and a delay before starting. Put transition on the element itself, not on hover, so the mouse-out direction also animates back smoothly.Q Is transition: all a good idea?
A It works but isn't recommended.
transition: all 0.3s gives every property change a transition, including show/hide layout shifts, and the browser can't optimize for specific properties. List them explicitly: transition: transform 0.3s, opacity 0.3s; — faster, predictable, and you won't get mystery animations.Q How do I make a button color change smoothly on hover?
A Two rules:
button { transition: background-color 0.3s; } button:hover { background-color: #2980b9; }. Keep transition on the button itself and only the target value in hover — then both directions animate; if transition lives inside hover, un-hovering snaps back instantly. Colors, borders, and shadows all transition this way.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 How do I pick between ease, linear, and ease-in-out timing functions?
A
linear moves at constant speed — right for spins and loading loops. ease is the default: slow start, faster middle, gentle landing — fine for everyday interactions. ease-out starts fast and settles softly, good for entrances; ease-in is the opposite for exits; ease-in-out is smooth at both ends. For extra character, a custom curve like cubic-bezier(0.22, 1, 0.36, 1) gives a springy feel.Q Why does my transition sometimes not work?
A Three common causes: the property doesn't support transitions at all (like
display or float); the start or end value isn't defined so the browser doesn't know what to animate between; or the element starts at display: none, where transitions don't run — switch to animation, or combine visibility with opacity.Q What filters does CSS have? How do I make an image grayscale or blurred?
A The everyday set:
filter: grayscale(100%) for black-and-white, filter: blur(4px) for blur, brightness(1.2) to lift brightness, sepia(0.8) for a vintage tone, contrast(1.5) for punch. Chain them with spaces in one declaration: filter: grayscale(50%) blur(2px);. A classic pattern is hover-to-color: images default to grayscale(100%) and return to grayscale(0) on hover.Q How is filter: drop-shadow() different from box-shadow?
A box-shadow follows the element's rectangular box edge, regardless of content shape. drop-shadow() follows the actual outline of the rendered content — shadowing a transparent PNG with box-shadow gives a rectangle, while drop-shadow() hugs the object inside the image. Different tools for different jobs.
Q Does filter: blur() cause performance problems?
A Yes. blur is compute-intensive — blurring large areas (big images, full-page backgrounds) in real time drops frames noticeably. Keep it under control: use blur only on small elements, stay around 10px or less on radius, and consider pre-blurring a background into a static image instead of a live filter. Brief hover-triggered blurs are usually fine; continuous rendering during scrolling deserves caution.
📖 Summary
- Transition is CSS animation's simplest form — specify which properties animate, duration, and timing
- Filters provide powerful image processing — blur, grayscale, color adjustments
- Combining both (like hover filter effects) creates rich interactive feedback
📝 Exercises
- Create a button that changes background color in 0.3s on hover, with slight scale increase.
- Create an image gallery: images show grayscale by default, smoothly become colorful on hover.
- Create a "navigation underline animation" — bottom line expands from center on hover.
- Use filter + transition to make images brighter and slightly blurred on hover.
🧩 Related Examples
Hover Reveal CardA profile card: circular avatar over a gradient banner; on hover the card lifts and a hidden stats panel fad…
Outline Slide ButtonTransparent outline buttons whose theme color slides up from the bottom on hover while the text flips to whi…
Floating Label InputsThe label starts living inside the input, then shrinks, floats up and turns theme-colored on :focus or once…