CSS Image Filters allow you to apply visual effects to images without
editing the original image. Using the filter property,
you can create effects such as blur, grayscale, brightness, contrast,
opacity, sepia, hue rotation, invert, saturate, and drop shadows. These
effects are commonly used in image galleries, portfolios, product pages,
and modern website designs.
Why Use CSS Image Filters?
CSS filters make it easy to enhance images, create hover effects, improve
visual appearance, and highlight important content. Since the original
image is not modified, filters can be changed or removed at any time
using CSS.
Basic Image Filter Syntax
What this code does: Applies a full grayscale filter to an image with filter: grayscale(100%), turning it black and white.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the basic filter syntax demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has a single rule for one image. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
.demo-img targets only the image with that class. width: 300px matches the 300 pixel width of the source file, so the picture is shown at its natural size.
The border is part of the element, so the filter changes it as well. In this example the blue border is turned gray along with the photo.
filter takes a filter function. The syntax is filter: function(amount). grayscale(100%) removes all color: 0% leaves the image unchanged, 50% is half faded and 100% is full black and white. The original file is not edited, and removing this line brings the colors back.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one image.
class="demo-img" applies the rule. The photo itself is in color. The browser applies the gray effect only when it draws the image.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 1: Apply a Grayscale Filter
The grayscale() filter removes colors from an image and converts it into black and white.
What this code does: Sets the image width to 300px and removes all its colour with grayscale(100%).
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the grayscale filter demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo adds rounded corners to the basic grayscale example. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The image is displayed 300px wide, and its height follows the original ratio.
A 3px border outlines the picture and border-radius: 8px rounds its corners. As the filter applies to the whole element, the blue border also loses its color and appears gray.
grayscale(100%) converts every pixel to a shade of gray. Try grayscale(50%) in the editor to see a half-faded look, which is a good way to understand how the percentage works.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one image.
The alt text describes the image for screen readers. A filter changes only the appearance, so the alt text is still needed.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 2: Apply Multiple Filters
You can combine multiple filters together to create unique visual effects.
What this code does: Chains brightness, contrast and saturate in one filter value to make the image brighter, sharper and more colourful.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the multiple filters demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo chains three filter functions in a single declaration. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The usual size, border and rounded corners. These do not change the filter result, but the border is also filtered, so it becomes brighter and more saturated.
filter: begins a list of functions separated by spaces, without commas. brightness(120%) makes the image 20% brighter. 100% means no change and 0% is completely black.
contrast(130%) increases the difference between light and dark areas, so the image looks sharper. Below 100% the image looks flatter and grayer. The extra indentation is only for readability, since CSS ignores line breaks.
saturate(150%) makes the colors 50% more vivid, and 0% would remove all color. The semicolon at the end finishes the whole filter declaration. The filters run from left to right, so the order can change the result.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one image.
One image receives all three effects from the single demo-img class.
</body> closes the body.
</html> is the final closing tag and ends the document.
Common CSS Image Filters
CSS provides several built-in filter functions for creating different image effects.
What this code does: Shows four filters side by side, each labelled: blur, sepia, invert and drop-shadow.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the common filters demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo uses one shared class for layout and one extra class for each filter. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
Each image and its label sit in a demo-item box. display: inline-block lets the boxes sit side by side in a row while still accepting margins and a width. text-align: center centers the label under the image.
margin: 10px separates the items from each other, and font-size: 14px makes the labels smaller than normal text.
Images are inline by default, which would place the label on the same line. display: block puts the image on its own line, so the label goes below it.
margin-bottom: 6px leaves a small gap between the image and its label. The border and rounded corners frame each photo. All four images share these styles through demo-img.
blur(3px) softens the image with a blur of 3 pixels. Unlike the other filters it takes a length, not a percentage. A larger number gives a stronger blur, and a value without a unit is invalid.
sepia(100%) gives the warm brown tone of an old photograph. Lower values mix in less of the effect.
invert(100%) turns every color into its opposite, like a photographic negative. The blue border becomes orange, because it is inverted too.
drop-shadow(5px 5px 8px gray) takes horizontal offset, vertical offset, blur radius and color. The shadow is 5px to the right and 5px down. It follows the real shape of the element, including the rounded corners, which is the main difference from box-shadow.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: four items, each with an image and a label.
The image has two classes: demo-img for the common size and border, and demo-blur for the filter. The word blur is the visible label.
Same structure with demo-sepia. The ?2 at the end of the URL just requests a different sample photo.
Same structure with demo-invert.
Same structure with demo-shadow. The shadow needs the 10px margin around the item to be visible.
</body> closes the body.
</html> is the final closing tag and ends the document.
Complete CSS Image Filter Example
What this code does: Shows a grayscale image that smoothly returns to full colour and grows slightly when you hover over it.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the hover filter demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets the font and 24px of padding. background: #f5f9ff gives the preview a very light blue color, so the white photo frame stands out.
The photo is 320px wide with rounded 10px corners.
A white border works as a photo frame, and a soft shadow lifts the picture off the background.
transition: .4s makes every changing property, here filter and transform, animate over 0.4 seconds instead of switching at once. It is written on the normal state so the animation also plays when the mouse leaves.
In its normal state the photo is fully gray.
:hover applies while the mouse is over the image. filter: none removes the grayscale, so the colors fade in. transform: scale(1.05) enlarges the photo by 5% without moving the content around it.
</style> closes the CSS.
</head> closes the head. The visible content comes next.
<body> starts the visible content: a heading and one image.
A heading telling the learner what to do. Move the mouse over the photo to see the effect.
The demo-photo class gives the image its gray start state and its hover behavior.
</body> closes the body.
</html> ends the document.
Advantages of CSS Image Filters
Advantage
Benefit
Description
No Image Editing
Easy Changes
Effects are applied using CSS without modifying the original image.
Interactive Effects
Better User Experience
Filters can change smoothly on hover.
Modern Design
Professional Look
Enhances the appearance of websites and galleries.
Flexible Styling
Creative Effects
Multiple filters can be combined for unique results.
Easy Maintenance
Reusable Code
One CSS rule can style many images.
Common Mistakes
Beginners often misspell filter function names, forget percentage units,
apply too many filters at once, or expect filters to permanently change
the original image. CSS filters only affect how the image is displayed in
the browser.
What this code does: Compares an invalid filter name (gray(100), ignored by the browser) with the correct grayscale(100%) on the same photo.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
/* Incorrect: gray() is not a valid filter, so it is ignored */
.demo-wrong {
width: 250px;
border: 3px solid #dc3545;
filter: gray(100);
}
/* Correct: grayscale() with a percentage */
.demo-correct {
width: 250px;
border: 3px solid #28a745;
filter: grayscale(100%);
}
.demo-label {
font-weight: bold;
}
</style>
</head>
<body>
<p class="demo-label">Incorrect (gray(100)) - stays in colour</p>
<img class="demo-wrong" src="https://picsum.photos/300/200?5" alt="Sample Image">
<p class="demo-label">Correct (grayscale(100%))</p>
<img class="demo-correct" src="https://picsum.photos/300/200?5" alt="Sample Image">
</body>
</html>
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the common mistake demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has one incorrect rule and one correct rule. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
A CSS comment, ignored by the browser and written for the reader. It labels the next rule as the wrong version.
The size and the red border are valid and work. The red color marks the incorrect example.
There is no filter function called gray(). The correct name is grayscale(), and it also needs %. The browser does not report an error: it drops the whole declaration, so the image stays in color. Misspelled filter names are a very common cause of nothing happening.
Another comment, labeling the correct version.
Same size as the wrong example, with a green border to mark it as correct.
grayscale(100%) has the right function name and a percentage, so the browser applies it and the image turns gray. The green border also turns gray, because the filter affects the whole element.
font-weight: bold makes the captions above the images stand out.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: two captions, each followed by an image.
Says that the image below uses the invalid gray(100) and stays in color.
Uses demo-wrong. Its filter is ignored, so it is still in color.
Says that the image below uses the valid grayscale(100%).
Uses demo-correct. Both images use ?5, so it is the same photo, and only the filter is different.
</body> closes the body.
</html> is the final closing tag and ends the document.
Best Practices
Use image filters only when they improve the design.
Avoid applying too many filters at the same time.
Use hover effects with smooth transition animations.
Optimize images before applying CSS filters.
Test filter effects on different browsers.
Combine filters carefully to achieve natural-looking results.
Use grayscale or opacity effects to highlight important images.
Keep accessibility in mind and don’t reduce image visibility excessively.