๐ Introduction
The contrast() function in CSS is part of the powerfulfilter property. It adjusts the contrast of an elementโmaking darks darker, lights lighter, or flattening the difference between them. This is often used in image editing, hover effects, UI states, and creative visual styling. ๐จโจ
๐ฏ What Does contrast() Do?
- Improves or reduces contrast in images or elements
- Can create dramatic or subtle visual effects
- Supports percentages and numbers
- Can be combined with other CSS filters
โจ Syntax
Syntax
selector {
filter: contrast(<value>);
}Note
๐งฉ Accepted Values
1. Using Percentages (%)
100% โ original contrast
0% โ completely grey
>100% โ increased contrast
Percentage Example
filter: contrast(150%);2. Using Numbers
1 โ original contrast
0 โ no contrast (grey)
>1 โ boosted contrast
Numeric Example
filter: contrast(2); /* 200% */๐ฅ Practical Examples
1. Increasing Contrast
Increase
.high-contrast {
filter: contrast(180%);
}2. Reducing Contrast
Decrease
.low-contrast {
filter: contrast(50%);
}3. Grayscale-Like Effect (contrast: 0)
No Contrast
.flat {
filter: contrast(0);
}4. Button Hover Contrast Boost
Hover UI Effect
button:hover {
filter: contrast(130%);
transition: filter 0.3s ease;
}5. Combining Multiple Filters
Combined Filters
img {
filter: brightness(1.1) contrast(140%) saturate(120%);
}๐ Contrast Effect Levels
| Value | Effect |
|---|---|
| contrast(0) | No contrast (flat grey) |
| contrast(0.5) | Lower contrast |
| contrast(1) | Normal contrast |
| contrast(2) | High contrast |
| contrast(3) | Very intense contrast |
๐ง Tips & Best Practices
- Use smooth transitions when applying filters to UI components
- Too much contrast can destroy image qualityโuse moderation โ ๏ธ
- Combine contrast() with brightness() for more natural effects
- Use filters for hover states instead of swapping image files
๐ Conclusion
The contrast() CSS filter gives you full control over the intensity and clarity of visual elements. Whether you're creating hover effects, enhancing images, or building creative UI animations, mastering contrast() helps you achieve polished and expressive designs. ๐