CSS Filters
1. Introduction to CSS Filters
CSS Filters are used to modify the appearance of images, backgrounds, and HTML elements. They are commonly used in photo galleries, cards, hover effects, and modern web interfaces.
Why Use CSS Filters?
- Improve image appearance.
- Create modern hover effects.
- Apply visual effects without image editing software.
- Enhance user experience.
- Create attractive UI designs.
img{
filter:grayscale(100%);
}
2. filter Property
The filter property applies one or more graphical effects to an element.
img{
filter:blur(4px);
}
| Function | Purpose |
|---|---|
| blur() | Blurs the image. |
| brightness() | Adjusts brightness. |
| contrast() | Adjusts contrast. |
| grayscale() | Converts image to grayscale. |
| sepia() | Applies sepia effect. |
filter property.
3. blur() Function
The blur() function makes an image appear
blurry. The higher the pixel value, the stronger the blur.
img{
filter:blur(5px);
}
| Value | Effect |
|---|---|
| blur(2px) | Light blur. |
| blur(5px) | Medium blur. |
| blur(10px) | Heavy blur. |
.banner{
filter:blur(3px);
}
4. brightness() Function
The brightness() function increases or
decreases the brightness of an image.
img{
filter:brightness(150%);
}
| Value | Result |
|---|---|
| 50% | Darker image. |
| 100% | Original brightness. |
| 150% | Brighter image. |
.photo:hover{
filter:brightness(120%);
}
5. contrast() Function
The contrast() function adjusts the difference
between the light and dark areas of an image.
img{
filter:contrast(200%);
}
.gallery img:hover{
filter:contrast(130%);
}
| Value | Effect |
|---|---|
| 50% | Lower contrast. |
| 100% | Normal contrast. |
| 200% | Higher contrast. |
contrast() carefully to improve image visibility without making it look unnatural.
6. grayscale() Function
The grayscale() function converts a colored
image into shades of gray. It is commonly used for image
galleries and hover effects.
img{
filter:grayscale(100%);
}
| Value | Result |
|---|---|
| 0% | Original color image. |
| 50% | Partially grayscale. |
| 100% | Completely grayscale. |
.gallery img:hover{
filter:grayscale(0%);
}
7. Other CSS Filter Functions
CSS provides several additional filter functions for creating unique visual effects.
| Function | Purpose |
|---|---|
| sepia() | Applies an old-photo effect. |
| invert() | Inverts image colors. |
| opacity() | Adjusts image transparency. |
| saturate() | Changes color intensity. |
| hue-rotate() | Rotates image colors. |
img{
filter:sepia(100%);
}
img{
filter:invert(100%);
}
img{
filter:opacity(50%);
}
img{
filter:saturate(200%);
}
img{
filter:hue-rotate(90deg);
}
8. Practical CSS Filter Examples
Example 1 : Blur Image
img{
filter:blur(4px);
}
Example 2 : Black & White Image
img{
filter:grayscale(100%);
}
Example 3 : Bright Hover Effect
img:hover{
filter:brightness(120%);
}
Example 4 : Vintage Image
img{
filter:sepia(80%);
}
9. Advantages of CSS Filters
- Create beautiful visual effects.
- Improve user interaction.
- Work without image editing software.
- Easy to combine with transitions.
- Perfect for hover effects.
- Improve modern website design.
- Easy to implement using CSS.
| Feature | Available |
|---|---|
| Blur Effect | ✔ Yes |
| Color Effects | ✔ Yes |
| Hover Effects | ✔ Yes |
10. CSS Filter Comparison Table
| Filter Function | Purpose | Example |
|---|---|---|
| blur() | Blurs an image. | blur(5px) |
| brightness() | Adjusts brightness. | brightness(150%) |
| contrast() | Adjusts contrast. | contrast(200%) |
| grayscale() | Converts image to grayscale. | grayscale(100%) |
| sepia() | Creates a vintage effect. | sepia(100%) |
| invert() | Inverts colors. | invert(100%) |
| opacity() | Changes transparency. | opacity(50%) |
| saturate() | Adjusts color intensity. | saturate(200%) |
| hue-rotate() | Changes color hue. | hue-rotate(90deg) |
img:hover{
filter:
brightness(120%)
contrast(110%)
saturate(150%);
}
11. CSS Filter Best Practices
Following best practices helps create attractive visual effects while maintaining good performance and usability.
- Use filter effects only where necessary.
- Combine filters with CSS transitions for smooth hover effects.
- Avoid applying heavy blur on large images.
- Maintain readability when using brightness or contrast filters.
- Optimize images before applying filters.
- Test filter effects on different devices.
- Use subtle effects for professional designs.
- Combine multiple filters carefully.
12. Common CSS Filter Mistakes
| Mistake | Correct Practice |
|---|---|
| Using excessive blur. | Apply only the required blur effect. |
| Applying filters to every image. | Use filters only where needed. |
| Using very high brightness values. | Keep brightness natural. |
| Ignoring performance. | Optimize images before applying filters. |
| Not testing filter combinations. | Preview effects on multiple devices. |
❌ Poor
filter:
blur(15px)
brightness(300%);
✔ Better
filter:
blur(2px)
brightness(110%);
13. Browser Support
The CSS filter property is supported in all
modern web browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS Filter Interview Questions
- What is the purpose of the
filterproperty? - What does the
blur()function do? - How does
brightness()differ fromcontrast()? - What is the use of the
grayscale()function? - What does the
sepia()function do? - How does
invert()work? - Can multiple filter functions be used together?
- What is the purpose of
hue-rotate()? - How can CSS Filters improve user experience?
- Are CSS Filters supported by modern browsers?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Filters.
- ✔ The
filterproperty. - ✔
blur()Function. - ✔
brightness()Function. - ✔
contrast()Function. - ✔
grayscale()Function. - ✔ Other filter functions such as
sepia(),invert(),opacity(),saturate(), andhue-rotate(). - ✔ Practical examples.
- ✔ Best Practices and Common Mistakes.
- ✔ Browser Support and Interview Questions.
Quick Quiz
Which CSS property is used to apply visual effects such as blur, brightness, and grayscale?