# Filters & Blend Modes

> Apply Photoshop-style effects, glassmorphism, and color blending natively in the browser.

Subject: CSS | Lesson 34 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/filters-and-blend-modes
Previous: [Scroll-Driven & Reduced Motion](https://codewithanimation.in/css/scroll-driven-and-reduced-motion.md) | Next: [Clip-Path & Masking](https://codewithanimation.in/css/clip-path-and-masking.md)

## What are visual effects in CSS?

Modern CSS provides direct access to graphical pixel manipulation that used to require heavy image editing software.

The `filter` property applies effects directly to an element (like blurring an image or shifting its colors). The `backdrop-filter` property applies those exact same effects to whatever sits *behind* the element.

## Why use CSS instead of images?

Applying effects via CSS is vastly superior to exporting pre-filtered images. It keeps your file sizes small, allows effects to change dynamically on hover, and lets you animate the intensity of the filter.

Most importantly, `backdrop-filter` allows you to create dynamic frosted glass (glassmorphism) that blurs moving content as you scroll past it, which is impossible to do with static images.

## How do you apply them?

You declare the property and chain functions together. For example: `filter: blur(4px) grayscale(100%);`.

For `mix-blend-mode`, you declare how the element should mathematically merge with its background. For example, `mix-blend-mode: multiply;` will make the colors combine and get darker, while `screen` will make them brighter.

## Common mistake: The transparent background trap

If you apply `backdrop-filter: blur(10px)` to an element, but that element has a solid `background-color: white`, you will never see the blur! The background must be partially transparent (e.g., `rgba(255, 255, 255, 0.5)`) for the backdrop to show through.

## Tip: drop-shadow() vs box-shadow

A standard `box-shadow` draws a rectangle around the element's bounding box. But `filter: drop-shadow()` looks at the actual transparent pixels of an image (like a PNG logo with a cut-out background) and draws the shadow exactly to the shape of the logo!

## Try it: Try it

Adjust the sliders to apply filters directly to the image, or toggle the Glass Overlay to see how backdrop-filter affects the content behind it.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/filters-and-blend-modes

## Check yourself

### Question 1

What is the core difference between filter and backdrop-filter?

- A. filter applies to the element itself, while backdrop-filter applies to what is behind the element.
- B. backdrop-filter is hardware accelerated, while filter is not.
- C. filter only works on images, while backdrop-filter works on text.

Correct answer: A

Use `filter` when you want to blur a photo. Use `backdrop-filter` when you want to create a frosted glass card that blurs the photo sitting underneath it.

- Why not B: Both properties can leverage hardware acceleration depending on the specific function used.
- Why not C: Both can work on any valid DOM element.

### Question 2

Why might backdrop-filter appear to do absolutely nothing?

- A. You forgot to apply a solid background color.
- B. The element's background color is not partially transparent.
- C. You used px units instead of percentages for the blur.

Correct answer: B

If the element is fully opaque (like `background: white`), the browser renders the white pixels on top of the blurred backdrop, completely hiding the effect. You must use a transparent color like `rgba(255, 255, 255, 0.2)`.

- Why not A: A solid background color is exactly what breaks it!
- Why not C: blur() requires length units like px or rem, not percentages.

### Question 3

Why is filter: drop-shadow() often better than box-shadow for transparent PNGs?

- A. It runs faster on the CPU.
- B. It contours to the actual non-transparent pixels of the image instead of the square CSS bounding box.
- C. It allows you to use multiple colors in the shadow.

Correct answer: B

If you have a PNG of a star shape with a transparent background, `box-shadow` will cast a square shadow behind the image bounds. `drop-shadow()` will cast a star-shaped shadow.

- Why not A: drop-shadow is often slightly more expensive to compute than a basic box-shadow.
- Why not C: Both properties only support a single color per shadow declaration.

## Remember this

- Chain multiple filters together: filter: brightness(1.2) contrast(1.5).
- backdrop-filter requires a partially transparent background color to be visible.
- Use drop-shadow() instead of box-shadow for PNGs with transparent cut-outs.
- mix-blend-mode controls how foreground pixels combine mathematically with background pixels.
