Open In App

How to Add filter to the background image using CSS?

Last Updated : 01 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Adding filters to background images using CSS allows you to apply visual effects like blur, grayscale, brightness adjustment, and more without modifying the actual image file. CSS provides a set of filter functions that can be applied to elements with background images.

This approach enhances design flexibility, enabling dynamic adjustments to background visuals to better match the website’s aesthetic or enhance user experience.

How to Apply CSS Filters to Background Images

You can apply filters to background images using the CSS filter property. The background image is set using the background-image property, and then filters are applied to modify the image’s appearance.

For example:

  • filter: brightness(90%) reduces the brightness of the image, making it darker.
  • filter: grayscale(70%) reduces color saturation, making the image less colorful. This is useful for improving text readability on top of the image.

Common CSS Filter Functions

Here are some commonly used CSS filter functions:

  • blur(px): Applies a blur effect to the image.
  • brightness(%): Adjusts the brightness. A value less than 100% darkens the image, while a value greater than 100% brightens it.
  • grayscale(%): Converts the image to grayscale. A value of 100% makes the image fully grayscale.
  • contrast(%): Adjusts the contrast. Values below 100% reduce contrast, and values above increase it.
  • opacity(%): Adjusts the transparency of the image.
  • sepia(%): Applies a sepia tone for a vintage effect.
  • hue-rotate(deg): Rotates the image’s hue by a certain degree, changing its colors.

Syntax

filter: none | blur() | brightness() | contrast() | drop-shadow() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia() | url();

Examples of Adding Filter in Background Image

Here are two examples of adding filters to the background image using CSS.

Example 1: Applying a Filter to a Background Image

In this example, we apply the brightness(90%) and grayscale(70%) filters to a background image. This makes the image slightly darker and reduces its colors, which can help improve readability for text placed on top.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        body {
            background-image: url(
"https://media.geeksforgeeks.org/wp-content/uploads/rk.png");
            filter: brightness(90%);
            filter: grayscale(70%);
            text-align: center;
        }
    </style>
</head>

<body>

    <h2>
        GeeksForGeeks
    </h2>
    <h2>
        How to add a filter to a
        background image using CSS?
    </h2>

</body>

</html>

Output:

Example 2: Applying Multiple Filters to a Background Image

You can also stack multiple filters on the same background image. In this example, we add both brightness and blur effects to the image:

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .darkened-image {
            filter: brightness(50%);
            background-image: url(
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20191121162913/s11.png");
            height: 94px;
            width: 120px;
        }
    </style>
</head>

<body>
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
    <p>
        The image below is the
        normal image
    </p>

    <img src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20191121162913/s11.png">

    <p>
        The image below is the
        darkened image:
    </p>

    <div class="darkened-image"></div>
</body>

</html>

Output:



Next Article

Similar Reads