Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to apply a CSS filter to ONLY the background image

I’ve got a full screen snap scrolling HTML page which has different topics beneath eachother; you are able to access these by scrolling down.
The different categories have different background images which are set using the background-image: url("image.png") tag.

Here is my code:

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
    <style>
        body, html {
            height: 100%;
        }
        body {
            background-color: black;
        }

        .slides {
            height: 100%;
            overflow: scroll;
            overflow-x: hidden;
            scroll-snap-type: y mandatory;
        }

        .slide {
            height: 100%;

            background-attachment: fixed;
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;

            scroll-snap-align: start;
        }
        
        .home {
            background-image: url("/img/bg.jpg");
        }
        .band {
            background-image: url("/img/bg2.jpg");
        }
        .pictures {
            background-image: url("/img/bg3.jpg");
        }
    </style>
</head>
<body>
    <div class="slides">
        <div class="slide home"></div>
        <div class="slide band"></div>
        <div class="slide pictures"></div>
    </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</html>

When I want to apply a filter using

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

            filter: brightness(60%) blur(4px);
            -webkit-filter: brightness(60%) blur(4px);

it blurres all child elements in the div instead of the background image.
I’ve already tried backdrop-filter but never got that to work.

>Solution :

You could put the background in a pseudo element, like this

    .home {
        position: relative;
    }
    .home:before {
          content: " ";
          position: absolute;
          top: 0;
          right: 0;
          bottom:0;
          left: 0;             
          background-image: url("/img/bg.jpg");

          //apply your filter here
       }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading