- !important
- Animation
- Background color
- Background image
- Blur() function
- Border color
- Border radius
- Border width
- Borders
- Box model
- Box shadow
- Class attribute
- Clear property
- Clip path
- Color
- Comment
- Container queries
- Cursor
- Display property
- Filter property
- First-child selector
- Flexbox
- Float property
- Focus
- Font family
- Font size
- Font style
- Font weight
- Gap
- Gradient
- Grid layout
- Height
- Hover
- ID selector
- Letter spacing
- Line height property
- Linking a style sheet
- Margin
- Media query
- Minmax() function
- N-th-child selector
- Object fit
- Opacity
- Outline
- Overflow property
- Padding
- Pixels
- Pointer events
- Position absolute
- Position fixed
- Position property
- Position sticky
- Pseudo-classes
- Pseudo-elements
- Quotes property
- Rotate
- Rounding an image
- Scale()
- Selectors
- Specificity
- Text align
- Text shadow
- Text wrap
- Transform property
- Transition property
- Translate() property
- Units
- Variable
- Viewport
- white-space
- Width
- Z-index
CSS
CSS Filter Property: Syntax, Usage, and Examples
The CSS filter property allows you to apply graphical effects to elements like images, backgrounds, or even text. With a single line of CSS, you can blur, brighten, invert, or otherwise manipulate the visual appearance of HTML elements. The filter property is particularly useful in modern web design for creating interactive UI effects, hover states, and visually appealing image displays.
What Is the CSS Filter Property?
The CSS filter property applies visual effects to an element by altering the rendering of its pixels. Rather than modifying the element’s layout or content, the filter changes how the browser paints the element on the screen.
These effects are similar to those found in image editing tools like Photoshop and can be applied dynamically using transitions and animations.
Basic Syntax
selector {
filter: function(value);
}
For example:
img {
filter: grayscale(100%);
}
This turns the image into black and white using a grayscale filter.
Common Filter Functions in CSS
The CSS filter property supports several built-in functions. You can apply one or multiple functions at once.
1. blur(px)
Blurs the element by the specified number of pixels.
img {
filter: blur(5px);
}
2. brightness(%)
Adjusts the brightness. 100% is the original brightness.
img {
filter: brightness(150%);
}
3. contrast(%)
Changes the contrast. 100% is the original contrast.
img {
filter: contrast(200%);
}
4. grayscale(%)
Converts the image to shades of gray.
img {
filter: grayscale(100%);
}
5. invert(%)
Inverts the colors. 100% inverts all colors; 0% leaves them unchanged.
img {
filter: invert(100%);
}
6. opacity(%)
Adjusts transparency.
img {
filter: opacity(50%);
}
7. saturate(%)
Controls color saturation. 100% is normal; 0% is grayscale.
img {
filter: saturate(300%);
}
8. sepia(%)
Applies a sepia tone.
img {
filter: sepia(100%);
}
9. drop-shadow(offsetX offsetY blurRadius color)
Adds a shadow that follows the shape of transparent images.
img {
filter: drop-shadow(4px 4px 10px rgba(0,0,0,0.5));
}
Combining Multiple Filters
You can chain multiple functions together in a single CSS filter property declaration.
img {
filter: grayscale(100%) blur(3px) brightness(120%);
}
The order matters: filters are applied in sequence, so changing the order may lead to a different visual outcome.
Filter Property in CSS with Transitions and Animations
The CSS filter property can be smoothly animated using the transition
property or keyframe animations.
Example: Image Hover Effect
img {
transition: filter 0.3s ease-in-out;
}
img:hover {
filter: brightness(80%) grayscale(50%);
}
This creates a subtle darkened grayscale effect when hovering over an image.
Example: Keyframes Animation
@keyframes blurAnimation {
0% {
filter: blur(0);
}
100% {
filter: blur(5px);
}
}
.blur-on-load {
animation: blurAnimation 1s ease-out forwards;
}
Animations allow you to introduce effects as the page loads or as elements come into view.
Fallbacks and Compatibility
The CSS filter property is supported in all major modern browsers:
- Chrome
- Firefox
- Safari
- Edge
- Opera
However, older versions of Internet Explorer (IE10 and earlier) do not support it. If backward compatibility is essential, avoid relying solely on filter effects for core functionality.
There is no need to use vendor prefixes for the filter property in modern environments.
Performance Considerations
Although the CSS filter property is GPU-accelerated, applying too many filters—especially to large images or when animating—can affect performance.
Tips to maintain performance:
- Use filters sparingly, especially with animations.
- Avoid animating high-cost filters like
blur()
over large areas. - Limit the number of DOM elements affected simultaneously.
- Consider reducing image resolution if combining multiple effects.
Accessibility and Usability Tips
The CSS filter property affects the visual presentation but not the underlying content. However, filters can reduce visibility or contrast, so be cautious:
- Avoid high
blur()
or lowopacity()
values for essential content. - Maintain sufficient color contrast when using
brightness()
andcontrast()
. - Use filters for decorative effects, not for conveying essential information.
- Test with screen readers to ensure content remains understandable when filters are used for interactivity.
Real-World Use Cases for CSS Filter Property
1. Dimmed Background on Modal Activation
body.modal-open .page-content {
filter: brightness(50%);
}
Reduces background distraction when a modal or popup is active.
2. Image Effects for Galleries or Portfolios
.gallery img {
filter: grayscale(100%);
}
.gallery img:hover {
filter: none;
}
Creates a clean hover interaction to draw attention.
3. Emphasizing Buttons or UI Elements
button:focus {
filter: drop-shadow(0 0 5px #ff9900);
}
Adds a glowing effect on interaction or focus.
4. Accessibility Enhancement for Visual Modes
You can apply invert()
and grayscale()
dynamically to create a "dark mode" or "reading mode."
body.dark-mode {
filter: invert(100%) hue-rotate(180deg);
}
Creating Custom Visual Themes
Filters can be combined to create entirely new visual styles across entire pages.
Example: Vintage Look
body.vintage {
filter: sepia(80%) contrast(120%) brightness(90%);
}
This can be toggled via JavaScript or user preferences.
Example: Night Mode Overlay
.night-overlay {
position: fixed;
top: 0; left: 0;
width: 100%; height: 100%;
backdrop-filter: brightness(50%);
pointer-events: none;
}
Useful for user-focused night reading experiences.
Best Practices
- Use with Transitions Sparingly – Animating blur or brightness over large areas can be costly.
- Keep Accessibility in Mind – Avoid filters that interfere with readability.
- Combine with Other Effects – Blend filters with
transform
oropacity
for richer UI. - Avoid Filter Overload – Using too many filter functions simultaneously can create confusion.
- Test on Multiple Devices – GPU capabilities vary; test performance across platforms.
The CSS filter property is a flexible and creative tool for enhancing visual design on the web. With functions like blur, grayscale, brightness, and drop-shadow, it allows designers and developers to craft sophisticated visual effects without JavaScript or heavy graphic assets.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.