CSS invert() Function
The invert() function is an inbuilt function that is used to apply a filter to the image to set the invert of the color of the sample image.
Syntax:
invert( amount )
Parameters: This function accepts single parameter amount which holds the amount of conversion. The value of invert is set in terms of value and percentage. The value 0% represents original image and 100% represents the inverted image. The invert() function internally uses the following formula, to computer the inverse of the image:
amount * (255 - value) + (1 - amount) * value
The value of inversion is controlled by the variable amount, the variable value lies between 0 – 1(floating) range (this is done by converting the passed percentage of color inversion to a value between 0-1). The value is the color value of the pixel. (255-value) gives the color after subtracting the color value with the max pixel value, assumes that the value of the pixel is in the range 0 – 255 (though input image sample space could be stretched/scaled to meet the specified criteria). Below is a table containing a list of inversion percentages and the result they produce.
Inversion | Result |
---|---|
0% | Original Image |
50% | Image with each pixel having grey color |
100% | Completely inverted Image |
Below example illustrates CSS invert() function in CSS:
Example:
<!DOCTYPE html>
<html>
<head>
<title>CSS invert() Function</title>
<style>
h1 {
color:green;
}
body {
text-align:center;
}
.invert_effect {
filter: invert(100%);
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>CSS invert() function</h2>
<img class="invert_effect" src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png"
alt="GeeksforGeeks logo">
</body>
</html>
Output:
Supported Browsers: The browsers supported by invert() function are listed below:
- Google Chrome 18 and above
- Edge 12 and above
- Internet Explorer not supported
- Firefox 35 and above
- Safari 6 and above
- Opera 15 and above
CSS invert() Function – FAQs
What does the invert() function do in CSS?
The invert() function is used with the filter property to invert the colors of an element, making light areas dark and dark areas light.
How do you apply the invert() function in CSS?
Apply inversion using filter: invert(100%); on the desired element to fully invert its colors.
Can the invert() function be applied partially?
Yes, you can use values between 0% and 100% to partially invert colors, such as filter: invert(50%);.
What are common uses for the invert() function?
The invert() function is commonly used for creating dark mode effects, inverting logos, or enhancing images for accessibility.
Does the invert() function affect performance?
Using invert() on large or numerous elements can slightly impact performance, especially on less powerful devices, due to increased rendering effort.