CSS Image Effects
In this page:
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.
- 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
<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.
- 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
<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.
- 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
<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.
- 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
<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.
- 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
<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">
- Animating with
widthandheightwhentransformis smoother. - Forgetting
overflow: hiddenon the wrapper, so a zoomed image spills outside the box. - Missing
transitionon the base state, so the effect snaps back suddenly.
Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Gradients
- CSS Shadows
- CSS Filters
- CSS Blend Modes
- CSS Transforms
- CSS 2D Transforms
- CSS 3D Transforms
- CSS Transitions
- CSS Animations
- CSS Image Styling
- CSS Image Effects
- CSS Hover Overlays
- CSS Image Modal
- CSS Image Centering
- CSS Image Shapes
- CSS Buttons
- CSS Columns
- CSS Clip Path
- CSS Shapes
- CSS Scroll Snap
- CSS Architecture: BEM
- CSS Custom Properties