How to apply a CSS filter to ONLY the background image

78 Views Asked by At

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/[email protected]/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/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</html>

When I want to apply a filter using

            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.

2

There are 2 best solutions below

0
Mahesh Prajapati On BEST ANSWER

I have modified your css code, please try to use, I hope your problem will be solved.

body, html {
            height: 100%;
            margin: 0; /* Remove default margin */
        }

        body {
            background-color: black;
            overflow: hidden; /* Hide scrollbars */
        }

        .slides {
            height: 100%;
            overflow: hidden; /* Hide scrollbars */
        }

        .slide {
            position: relative;
            height: 100%;
            overflow: hidden;
            scroll-snap-align: start;
        }

        .slide::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-attachment: fixed;
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
            filter: brightness(60%) blur(4px);
            -webkit-filter: brightness(60%) blur(4px);
            z-index: -1; 
        }

        .home::before {
            background-image: url("poster_1.png");
        }

        .band::before {
            background-image: url("poster_1.png");
        }

        .pictures::before {
            background-image: url("poster_1.png");
        }
0
Alex On

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
       }
    }