Open In App

How to Add Filter Effects to Images ?

Adding filter effects to images can make images more enhanced on your website. Filter effects can be started from simple adjustments like brightness and contrast to more complex effects like blur, color overlays, and many more.

The below approaches can be used to add filter effect to images:

Using CSS Filters

Example: The below code implements CSS filters to add filter effects to an image.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>
        CSS Filter Effects Example
    </title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="image-container">
        <img alt="GFG Image" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20210915115837/gfg3-300x300.png"
            id="gfg-image">
    </div>
</body>

</html>
.image-container {
    width: 300px;
    margin: 20px auto;
    text-align: center;
}

img {
    width: 100%;
    height: auto;
    border-radius: 10px;
    transition: filter 0.3s ease;
}

img:hover {
    filter: grayscale(100%);
}

Output:

fosiGIF

Using SVG Filters

Example: The below code uses SVG filters to add a filter effect to the images.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>
        CSS SVG Effects Example
    </title>
    <link rel="stylesheet" href="style.css">

</head>

<body>

    <div class="image-container">
        <img alt="GFG Image" id="gfg-image" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20210915115837/gfg3-300x300.png">
    </div>

    <svg width="0" height="0">
        <filter id="blur" x="-50%" y="-50%" 
            width="200%" height="200%">
            <feGaussianBlur in="SourceGraphic" stdDeviation="5" />
        </filter>
    </svg>

    <script>
        const image = 
            document.getElementById('gfg-image');
        image.style.filter = "url(#blur)";
    </script>

</body>

</html>
.image-container {
    width: 300px;
    margin: 20px auto;
    text-align: center;
}

img {
    width: 100%;
    height: auto;
    border-radius: 10px;
    transition: filter 0.3s ease;
}

img:hover {
    filter: grayscale(100%);
}

Output:

filter

Using CSS Blend Modes

Example: The below code implements CSS blend modes to add filter effects to the images.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>
        CSS BlendMode Effects Example
    </title>
    <link rel="stylesheet" href="style.css">
</head>

<body>

    <div class="container">
        <div class="image">
            <img alt="Gfg 1" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20210915115837/gfg3-300x300.png">
            <div class="filter-overlay"></div>
        </div>
        <div class="image">
            <img alt="Gfg 2" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20210915115837/gfg3-300x300.png">
            <div class="filter-overlay"></div>
        </div>
        <div class="image">
            <img alt="Gfg 3" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20210915115837/gfg3-300x300.png">
            <div class="filter-overlay"></div>
        </div>
    </div>

</body>

</html>
.container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 20px;
}

.image {
    width: 300px;
    height: 200px;
    overflow: hidden;
    position: relative;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease-in-out;
}

.image:hover {
    transform: scale(1.05);
}

.image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: filter 0.3s ease-in-out;
}

.image:hover img {
    filter: 
        grayscale(100%) contrast(150%) 
        brightness(80%);
}

.filter-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    mix-blend-mode: overlay;
    transition: opacity 0.3s ease-in-out;
    pointer-events: none;
}

.image:hover .filter-overlay {
    opacity: 0;
}

Output:

e3

blend-modes

Article Tags :