The CSS distinction() filter operate will increase or decreases the distinction of a component, both making colours come out extra or dulling them to grey. In contrast to different filter features like brightness() or saturate(), distinction() impacts each saturation and lightness, retaining solely the colour’s hue.
.low {
filter: distinction(50%);
}
.regular {
filter: distinction(100%);
}
.excessive {
filter: distinction(200%);
}
The distinction() operate is outlined within the Filter Results Module Stage 1 specification.
Syntax
The official syntax for the distinction() operate is:
= distinction( [ | ]? )
Or just:
filter: distinction();
The distinction() operate is simply appropriate with the CSS filter and backdrop-filter properties.
Arguments
/* Utilizing percentages */
filter: distinction(0%); /* Completely grayed out */
filter: distinction(50%); /* Partially grayed out */
filter: distinction(100%); /* No change */
filter: distinction(150%); /* Factor is 1.5 occasions extra outlined */
/* Utilizing numbers (0–1 vary) */
filter: distinction(0); /* Completely grayed out */
filter: distinction(0.5); /* Partially grayed out */
filter: distinction(1); /* No change */
filter: distinction(1.5); /* Factor is 1.5 occasions extra outlined */
/* Utilizing percentages */
filter: distinction(0%); /* Completely grayed out */
filter: distinction(50%); /* Partially grayed out */
filter: distinction(100%); /* No change */
filter: distinction(150%); /* Factor is 1.5 occasions extra outlined */
/* Utilizing numbers (0–1 vary) */
filter: distinction(0); /* Completely grayed out */
filter: distinction(0.5); /* Partially grayed out */
filter: distinction(1); /* No change */
filter: distinction(1.5); /* Factor is 1.5 occasions extra outlined */
/* Works with CSS variables */
--amount: 200%;
filter: distinction(--amount);
/* No argument */
filter: distinction(); /* No change */
/* Detrimental worth */
filter: distinction(-1.5); /* No impact */
filter: distinction(--amount);
/* No argument */
filter: distinction(); /* No change */
/* Detrimental worth */
filter: distinction(-1.5); /* No impact */
The distinction() operate takes a single argument, which is usually a optimistic decimal or proportion worth. The argument determines the brand new distinction for the aspect, the place:
0or0%dries out all distinction from the aspect, leading to a totally grey picture.1or100%leaves the aspect fully unchanged.- Values above
1or100%improve the distinction linearly.
Detrimental values aren’t allowed. However CSS variables are:
.aspect {
--filter-amount: 150%;
filter: distinction(var(--filter-amount));
}
How distinction() impacts shade
Like different filter features, the distinction() filter operates purely on RGB math. Particularly, given an it multiplies every RGB channel by that after which provides 255 * (0.5 - 0.5 * to the end result. In apply, this impacts colours in one in every of two methods:
- Excessive distinction (higher than
1) makes gentle pixels get lighter and darkish pixels get darker, so colours turn out to be extra vivid. - Low distinction (smaller than
1) pulls all pixels towards a center grey. This reduces the distinction between gentle and darkish areas, making the picture look flat and muted.
Fundamental utilization
Some background pictures, often in hero sections or carousels, could make the foreground textual content tough to learn. Particularly if it has very vivid and darkish colours, which compete with any textual content shade. To resolve this, we are able to use distinction() to cut back the distinction between the picture’s whites and blacks, making textual content extra readable in opposition to the entire picture.
img {
filter: distinction(70%) brightness(60%);
}
The low distinction flattens the picture, and as a plus, we are able to additionally scale back the picture’s brightness to make the textual content pop no matter its colours.
Demo: Making product card pictures pop on hover
One other helpful software for distinction() is to focus on a picture in a person’s interplay. For instance, in a row of picture playing cards, we may improve the picture’s distinction and likewise scale it on hover
.card img {
transition:
filter 0.4s ease,
rework 0.4s ease;
}
.card:hover img {
filter: distinction(125%);
rework: scale(1.05);
}
Is distinction() the identical as contrast-color()?
Whereas each CSS features have comparable names, they don’t seem to be to be confused with one another.
distinction()is a filter operate that makes a component extra vivid by making whites lighter and blacks darker.contrast-color()returns the textual content shade with the very best distinction to a strong background. Its ensuing shade is both white or black, relying on which shade contrasts most with the background. It’s also not a filter operate.
Browser help
The distinction() operate is at present supported throughout all trendy browsers.









