Section 29: Advanced CSS Visual Effects
CSS filters, blend modes, clipping paths, and masks create visual effects without requiring you to edit the original image file.
1. CSS filters
.photo {
filter: grayscale(100%);
}
.photo:hover {
filter: grayscale(0%);
}
.soft-image { filter: blur(2px); }
.dark-image { filter: brightness(70%); }
.colorful-image { filter: saturate(150%); }
.shadow-image { filter: drop-shadow(0 6px 8px #0005); }Common filters include blur(), brightness(), contrast(), grayscale(), hue-rotate(), invert(), opacity(), saturate(), sepia(), and drop-shadow(). Filters can be combined in one declaration.
2. Blend modes
.overlay {
background: rgb(20 80 180 / 60%);
mix-blend-mode: multiply;
}
.background-layer {
background-blend-mode: screen;
}mix-blend-mode blends an element with its backdrop. background-blend-mode blends an element's background layers with one another.
3. Clip paths
.triangle {
clip-path: polygon(50% 0, 100% 100%, 0 100%);
}
.circle {
clip-path: circle(45% at 50% 50%);
}clip-path controls which part of an element is visible. The clipped-away region is not displayed.
4. CSS masks
.fade-edge {
mask-image: linear-gradient(to right, transparent, black);
}Masking uses the mask image's transparency or alpha information, or luminance where applicable, to control visibility. Check browser support for advanced mask features.
Runnable example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Visual Effects</title>
<style>
body { font-family: Arial, sans-serif; padding: 24px; }
.photo { width: 260px; height: 150px; object-fit: cover; border-radius: 12px; filter: saturate(65%); transition: filter 250ms ease, transform 250ms ease; }
.photo:hover { filter: saturate(140%) contrast(110%); transform: scale(1.02); }
.triangle { width: 160px; height: 100px; margin-top: 24px; background: linear-gradient(135deg, #2563eb, #a855f7); clip-path: polygon(50% 0, 100% 100%, 0 100%); }
</style>
</head>
<body>
<h3>Hover to change the image treatment</h3>
<img class="photo" src="https://images.unsplash.com/photo-1500530855697-b586d89ba3ee?auto=format&fit=crop&w=600&q=80" alt="Mountain landscape under a wide sky">
<h3>Clipped triangle</h3>
<div class="triangle" aria-hidden="true"></div>
</body>
</html>Important notes
- Filters and blending can be visually expensive when applied to large areas or many animated elements.
- Clipping changes the visible region but does not automatically change the element's layout dimensions.
- Do not use visual effects to communicate essential information without providing readable text or another accessible cue.
- Use meaningful alternative text for informative images.
- Test overlays for sufficient text contrast.
Practice exercise
Build a photo card with a grayscale-to-color hover effect, a gradient overlay, and a decorative clipped shape. Ensure the image has useful alt text.
Knowledge check: 10 MCQs
- Which property applies grayscale to an image? Answer: filter. Explanation: Use filter: grayscale(100%).
- Which filter softens an image? Answer: blur(). Explanation: It applies a Gaussian blur effect.
- Which property blends an element with its backdrop? Answer: mix-blend-mode. Explanation: It controls how the element blends with content behind it.
- Which property blends background layers? Answer: background-blend-mode. Explanation: It affects the element's background layers.
- What does clip-path do? Answer: Defines the visible clipping region. Explanation: Pixels outside the clipping shape are not shown.
- Can clip-path change normal layout dimensions? Answer: No, not by itself. Explanation: Clipping affects appearance rather than the element's allocated layout box.
- What can mask-image control? Answer: Element visibility using mask information. Explanation: Mask transparency or luminance can reveal or hide portions.
- Which filter increases color intensity? Answer: saturate(). Explanation: It changes color saturation.
- Does a CSS filter permanently modify the original image file? Answer: No. Explanation: It changes how the image is rendered by the browser.
- What should be checked when placing text over a blended background? Answer: Text contrast and readability. Explanation: Decorative effects must not make content difficult to read.