The CSS filter
property applies graphical effects like blurring or color shifting to an element. The invert()
function inverts the colors on the element. A value of 1
(or 100%
) represents a full inversion, while 0
(or 0%
) leaves the element unchanged.
Example 1: Full Inversion on an Image
/* style.css */
.inverting-image {
/* This applies a 100% color inversion to the image. */
filter: invert(100%);
}
Explanation
This code selects an element with the class .inverting-image
and applies a complete color inversion. Black becomes white, white becomes black, and all other colors are converted to their opposite on the color wheel.
Example 2: Partial Inversion
/* style.css */
.partially-inverted {
/* Inverts the colors of the element by 75%. */
filter: invert(75%);
}
Explanation
Here, the invert()
function is set to 75%
, creating a partial color inversion. The effect is less stark than a full 100% inversion, resulting in muted, shifted colors.
Example 3: Inversion on Hover
/* style.css */
.hover-invert-effect {
/* A smooth transition is applied for the filter property. */
transition: filter 0.5s ease-in-out;
}
.hover-invert-effect:hover {
/* Inverts the element's colors only when the mouse pointer is over it. */
filter: invert(1);
}
Explanation
This example creates an interactive effect where the element's colors fully invert when a user hovers over it. The transition
property ensures the change is gradual and not instantaneous.
Example 4: Inverting a Div with Text
/* style.css */
.inverted-text-block {
background-color: #f0f0f0;
color: #333;
padding: 20px;
/* Applying a full inversion to the entire div. */
filter: invert(1);
}
Explanation
This CSS rule applies the invert()
filter to a div
. Consequently, the light gray background becomes dark, and the dark text becomes light, demonstrating how invert()
affects all aspects of an element.
Example 5: Using Decimal Values for Inversion
/* style.css */
.subtle-inversion {
/* This applies a subtle 20% inversion effect using a decimal value. */
filter: invert(0.2);
}
Explanation
The invert()
function accepts decimal values between 0 and 1. In this case, 0.2
is equivalent to 20%
, providing a very slight color inversion for a subtle design touch.
Example 6: Combining Invert with Other Filters
/* style.css */
.multi-filter-image {
/* This first inverts the image and then applies a sepia tone. */
filter: invert(1) sepia(1);
}
Explanation
You can chain multiple filter functions together. This code first inverts the colors of the element and then applies a full sepia effect, creating a unique, stylized look.
Example 7: Inversion on an Active Link
/* style.css */
a.active-invert-link {
color: blue;
transition: filter 0.3s;
}
a.active-invert-link:active {
/* The link's color inverts for a moment when it is clicked. */
filter: invert(100%);
}
Explanation
This CSS provides user feedback by inverting the link's color when it is being clicked (the :active
state). It's a simple way to show that the click has been registered by the browser.