The CSS filter
property applies graphical effects like blurring or color shifting to an element. The brightness()
function adjusts the brightness of the image or element, making it appear lighter or darker. A value of 0% will create an image that is completely black, while a value of 100% leaves the image unchanged. Values over 100% will make the image brighter.
Example 1: Making an Image Brighter
/* This CSS rule targets the image with the class 'brighten-me' */
.brighten-me {
/* The filter property is used to apply the brightness effect. */
/* A value of 1.5 or 150% makes the image 50% brighter than its original state. */
filter: brightness(1.5);
}
Explanation:
This code selects an element with the class .brighten-me
and applies a brightness()
filter. The value of 1.5
increases the brightness of the element by 50%, making it appear significantly lighter.
Example 2: Making an Image Darker
/* This rule targets an image with the class 'dim-the-lights' */
.dim-the-lights {
/* Applying a brightness value less than 1 or 100% will darken the image. */
/* Here, 0.6 or 60% makes the image 40% darker. */
filter: brightness(0.6);
}
Explanation:
This example targets the .dim-the-lights
class. The brightness(0.6)
filter reduces the element's brightness to 60% of its original value, resulting in a noticeably darker, dimmed appearance.
Example 3: Default Brightness
/* This class 'normal-image' will have its brightness unchanged. */
.normal-image {
/* A brightness value of 1 or 100% represents the original brightness of the element. */
/* This is the default value for the brightness function. */
filter: brightness(1);
}
Explanation:
The .normal-image
class uses brightness(1)
, which is equivalent to 100%. This value represents the original, unaltered brightness of the element, so visually, no change occurs.
Example 4: Completely Black Element
/* This rule applies to the element with the class 'black-out' */
.black-out {
/* A brightness value of 0 or 0% will render the element completely black. */
/* All color information is removed, leaving only black. */
filter: brightness(0);
}
Explanation:
Here, the .black-out
class has its brightness set to 0
. This effectively removes all light from the element, rendering it as a completely black shape, losing all its original colors and details.
Example 5: Using Percentage Values
/* This CSS targets the 'brighter-image-percent' class */
.brighter-image-percent {
/* The brightness function also accepts percentage values. */
/* 200% will make the image twice as bright as the original. */
filter: brightness(200%);
}
Explanation:
This code demonstrates using a percentage value. brightness(200%)
is applied to the .brighter-image-percent
class, doubling its brightness compared to the original state. This is an alternative syntax to using decimal values like 2
.
Example 6: Brightness on Hover
/* This rule targets an element with the class 'hover-effect' */
.hover-effect {
/* Initially, the element has its default brightness. */
filter: brightness(1);
/* A smooth transition is added for the filter property. */
transition: filter 0.3s ease-in-out;
}
/* This rule applies when the user hovers over the element. */
.hover-effect:hover {
/* On hover, the brightness is increased to 120%. */
filter: brightness(1.2);
}
Explanation:
This example creates an interactive effect. The .hover-effect
element starts at normal brightness. When a user hovers over it, the brightness increases to 1.2
(120%), creating a subtle highlighting effect. The transition
property ensures this change happens smoothly.
Example 7: Combining Brightness with Other Filters
/* This rule is for an element with the class 'combo-filter' */
.combo-filter {
/* Multiple filter functions can be chained together, separated by a space. */
/* Here, we increase brightness by 110% and also apply a sepia effect. */
filter: brightness(1.1) sepia(0.8);
}
Explanation:
This demonstrates the power of combining multiple filter functions. The .combo-filter
class gets both a brightness enhancement to 1.1
(110%) and a sepia tone. This allows for creating more complex and layered visual effects on a single element.