← Back to CSS Course | Chapter 8: Animations & Effects | Lesson 11 of 22

CSS Image Effects

Pairing a hover selector with transform and transition properties turns a static photo into something that responds when a visitor's cursor arrives — zooming in, fading, or shifting color — all without a single line of JavaScript.
Syntax
css
img {
  filter: function(value);
  transition: filter duration;
}
img:hover {
  filter: function(newValue);
}

Hover-triggered Zoom

Scaling an image up slightly on :hover with transform: scale() creates a classic 'zoom in' product-photo effect; wrapping the image in a container with overflow: hidden keeps the enlarged image from spilling outside its box.

When an image is scaled up significantly, as during this kind of zoom, image-rendering: pixelated keeps sharp, blocky pixel edges instead of the browser's default smooth blur, which is a deliberate look often used for pixel-art thumbnails.

Note:
  • Accepted values:
  • transform: scale(<number>) — enlarges the image
  • overflow: hidden — clips the zoom to the container
  • transition: transform 0.3s ease — animates the zoom
  • transform-origin — sets the zoom center

Example: Hover-triggered Zoom

css
<style>
.wrap {
  overflow: hidden;
}
.wrap img {
  transition: transform 0.3s;
}
.wrap:hover img {
  transform: scale(1.2);
}
</style>
<div class="wrap">
  <img src="https://placehold.co/200x150" alt="Zooms on hover">
</div>

Fade Between Two Images

Stacking two images with absolute positioning and transitioning one's opacity from 0 to 1 on hover creates a smooth crossfade effect, commonly used for 'before/after' or normal/highlighted image states.

Note:
  • Accepted values:
  • opacity: 0 — fully transparent
  • opacity: 1 — fully opaque (default)
  • transition: opacity 0.4s — animates the fade
  • position: absolute — stacks the two images

Example: Fade Between Two Images

css
<style>
.stack {
  position: relative;
}
.stack img {
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
  transition: opacity 0.3s;
}
.stack:hover .top {
  opacity: 1;
}
</style>
<div class="stack">
  <img src="https://placehold.co/200x150" alt="Base">
  <img class="top" src="https://placehold.co/200x150" alt="Fades in on hover">
</div>

Brightness and Filter Transitions

Because filter values can be transitioned just like colors or positions, changing an image's brightness() or grayscale() amount on hover — combined with a transition property — produces a smooth fade rather than an abrupt jump.

Note:
  • Accepted values:
  • transition: filter 0.3s — animates filter changes
  • filter: brightness(1.2) — brighter
  • filter: grayscale(1) — gray to color on hover
  • filter: none — clears the effect

Example: Brightness and Filter Transitions

css
<style>
img {
  filter: brightness(70%);
  transition: filter 0.3s;
}
img:hover {
  filter: brightness(100%);
}
</style>
<img src="https://placehold.co/200x150" alt="Brightens smoothly on hover">

Combining transform and transition

The transition property must name transform (or use the all keyword) for a hover-triggered transform change to animate smoothly instead of snapping instantly; the transition is declared on the image's base state, not on the :hover rule itself.

Note:
  • Accepted values:
  • transform: <function>() — one or more transform functions
  • transition: transform 0.3s ease — animates the change
  • scale() | translate() | rotate() — common combos
  • will-change: transform — hint for heavy animation

Example: Combining transform and transition

css
<style>
img {
  transition: transform 0.3s;
}
img:hover {
  transform: scale(1.1);
}
</style>
<img src="https://placehold.co/200x150" alt="Transition declared on the base state">

Performance of Animated Images

Animating transform and opacity is comparatively cheap for the browser because they can run on the GPU without forcing a page layout recalculation, which is why hover effects favor those two properties over animating width, height, or margin directly.

Note:
  • Accepted values:
  • transform — compositor-friendly
  • opacity — compositor-friendly
  • filter — often GPU-accelerated but costly on large images
  • width / height / top / left — trigger layout, avoid animating

Example: Performance of Animated Images

css
<style>
img {
  transition: transform 0.3s, opacity 0.3s;
}
img:hover {
  transform: scale(1.1);
  opacity: 0.9;
}
</style>
<img src="https://placehold.co/200x150" alt="Animates transform and opacity, both GPU-cheap">
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Animating with width and height when transform is smoother.
  2. Forgetting overflow: hidden on the wrapper, so a zoomed image spills outside the box.
  3. Missing transition on the base state, so the effect snaps back suddenly.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.