CSS Filters Examples

CSS Filters: Adding Visual Effects to Elements

CSS filters provide a way to apply visual effects like blurring, color manipulation, and more to elements in your HTML. They're applied using the filter property and can significantly enhance the appearance of your website.

Basic Syntax:

element {
  filter: filter-function1(value1) filter-function2(value2) ...;
}

Key Concepts:

filter property: This is the core CSS property that activates the filter effects.

filter-function: Specifies the type of filter effect you want to apply (e.g., blur, grayscale, brightness).

value: Sets the intensity or parameters of the filter effect.

Common Filter Functions and Examples:

Here's a breakdown of some commonly used filter functions with examples:

blur(): Applies a Gaussian blur effect.

img {
  filter: blur(5px); /* Applies a 5-pixel blur */
}

Explanation: The blur(5px) function makes the image appear slightly out of focus. You can adjust the pixel value to control the blur's intensity.

brightness(): Adjusts the brightness of an element.

.bright-image {
  filter: brightness(1.5); /* Increases brightness by 50% */
}

Explanation: brightness(1.0) is the default (no change). Values greater than 1 increase brightness, and values less than 1 decrease brightness (making it darker).

contrast(): Adjusts the contrast of an element.

 
.high-contrast {
  filter: contrast(150%); /* Increases contrast by 50% */
}

Explanation: contrast(100%) is the default. Higher values increase contrast (making darks darker and lights lighter), while lower values decrease contrast.

grayscale(): Converts an element to grayscale.

.grayscale-image {
  filter: grayscale(100%); /* Makes the image completely grayscale */
}

Explanation: The value is a percentage. grayscale(100%) removes all color, while grayscale(0%) leaves the element unchanged.

hue-rotate(): Rotates the hue of an element's colors.

.hue-shifted {
  filter: hue-rotate(90deg); /* Rotates the hue by 90 degrees */
}

Explanation: The value is an angle in degrees (e.g., deg, rad, turn). It cyclically shifts the colors of the element.

invert(): Inverts the colors of an element.

.inverted {
  filter: invert(100%); /* Inverts all colors */
}

Explanation: Similar to grayscale, invert(100%) fully inverts the colors, while invert(0%) has no effect.

opacity(): Adjusts the opacity of an element.

.semi-transparent {
  filter: opacity(0.5); /* Makes the element 50% opaque */
}

Explanation: This is an alternative to the opacity property. opacity(1) is fully opaque, and opacity(0) is fully transparent.

saturate(): Adjusts the saturation of an element's colors.

.more-saturated {
  filter: saturate(200%); /* Doubles the color saturation */
}

Explanation: saturate(100%) is the default. Values greater than 100% increase saturation (making colors more vibrant), while values less than 100% decrease saturation (making colors more muted).

sepia(): Applies a sepia tone to an element.

.sepia-toned {
  filter: sepia(80%); /* Applies an 80% sepia tone */
}

Explanation: sepia(100%) applies a full sepia tone, while sepia(0%) has no effect.

drop-shadow(): Adds a drop shadow effect to an element.

.shadowed {
  filter: drop-shadow(5px 5px 5px #888); /* Adds a black shadow */
}

Explanation: The arguments are: drop-shadow(horizontal offset, vertical offset, blur radius, color). You can use different colors and offsets to customize the shadow. Important: This is different from box-shadow. drop-shadow follows the alpha channel of the image, while box-shadow applies to the element's bounding box.

Combining Filters:

You can combine multiple filter functions to create more complex effects.

img {
  filter: grayscale(50%) blur(2px) brightness(1.2);
}

Example HTML:

Original Image

Original

Blurred Image

Blur

Brightness Image

Brightness

Contrast Image

Contrast

Grayscale Image

Grayscale

Hue Rotate Image

Hue Rotate

Invert Image

Invert

Opacity

Saturate Image

Saturate

Sepia

Drop Shadow Image

Drop Shadow

Important Considerations:

Performance: Applying complex filters can impact performance, especially on older devices or with many filtered elements. Test thoroughly and optimize as needed.

Browser Compatibility: CSS filters are widely supported in modern browsers, but older browsers may not fully support them. Consider using a polyfill or fallback for older browsers if necessary. Check compatibility tables like those on MDN.

Accessibility: Be mindful of how filters might affect users with visual impairments. For example, very high contrast or inverted colors can be problematic for some users. Provide alternative viewing options if needed.

backdrop-filter: There's also a related backdrop-filter property, which applies filters to the area behind an element (e.g., blurring content behind a semi-transparent modal).

CSS filters provide a powerful and flexible way to add visual effects to your web pages. Experiment with different filter functions and values to create the desired look and feel for your site. Remember to consider performance, browser compatibility, and accessibility in your implementation.