The CSS filter property's saturate() function adjusts the color intensity of an element. A value of 100% or 1 represents the original image, while a value of 0% or 0 will make the image completely grayscale. Values above 100% will increase the color saturation, making them appear more vivid.
Example 1: Basic Saturation
This example shows how to increase the saturation of an image, making the colors appear much richer and more vibrant than the original.
.saturated-image {
/* Increases the color saturation by 80% from the original */
filter: saturate(180%);
}
Explanation The .saturated-image class is assigned to an image element. The filter property with the saturate() function is used to increase the color intensity of the image to 180%.
Example 2: Desaturation (Grayscale)
Here, we will completely remove all color from the image, rendering it in grayscale by setting the saturation to zero.
.desaturated-image {
/* Removes all color, resulting in a grayscale image */
filter: saturate(0%);
}
Explanation The .desaturated-image class uses the saturate() function with a value of 0%. This removes all color information from the image, effectively turning it into a black and white or grayscale image.
Example 3: Subtle Saturation Boost
This demonstrates a more subtle enhancement, where the color saturation is only slightly increased to make the image pop without looking unnatural.
.subtle-boost {
/* A gentle 20% boost in color saturation */
filter: saturate(1.2);
}
Explanation In this case, the .subtle-boost class applies a saturation level of 1.2. This is equivalent to 120% and provides a minor but noticeable increase in color vibrancy.
Example 4: Saturation on Hover
This example applies a saturation effect when a user hovers their mouse over the image, providing a simple interactive visual feedback.
.hover-saturate {
/* Set initial state to be partially desaturated */
filter: saturate(50%);
transition: filter 0.3s ease-in-out;
}
.hover-saturate:hover {
/* Return to full color on hover */
filter: saturate(100%);
}
Explanation The image with the .hover-saturate class is initially displayed at 50% saturation. When hovered over, the saturation smoothly transitions to 100% over 0.3 seconds, creating an engaging effect.
Example 5: Using saturate() with other filters
The saturate() function can be combined with other filter functions to create more complex and unique visual effects on an element.
.multi-filter {
/* Apply multiple filters for a unique look */
filter: saturate(200%) contrast(120%);
}
Explanation For the .multi-filter class, we are chaining two filter functions. The browser first applies saturate(200%) to make the colors hyper-vibrant and then increases the contrast by 120%.
Example 6: Animated Saturation with Keyframes
You can use CSS animations to create a pulsating color effect by animating the saturate() value over time.
.animated-saturation {
animation: pulse-color 2s infinite alternate;
}
@keyframes pulse-color {
from {
filter: saturate(25%);
}
to {
filter: saturate(175%);
}
}
Explanation The .animated-saturation class applies an animation named pulse-color. This animation cycles the saturation of the element from 25% to 175% and back again infinitely, creating a pulsing color effect.
Example 7: Saturating a Video Element
The saturate() filter is not limited to images and can be applied to other HTML elements, such as videos, to adjust their color intensity.
.saturated-video {
/* Make the video colors extremely vivid */
filter: saturate(2.5);
}
Explanation This code applies the saturate() filter to a video element with the class .saturated-video. The value of 2.5 (or 250%) will make the colors in the playing video appear much more intense.