Home > other >  How do I blur the background of an image without affecting the text that sits on top of it?
How do I blur the background of an image without affecting the text that sits on top of it?

Time:12-30

I'm trying to create a banner with a blurred background and visible text in front however when I run the code the text ends up being blurred along with the background. I have found various solutions, and none of them worked. Please help me.

.bg-image {
        background-image: url("PHT4.jpg");

        filter: blur(8px);
        -webkit-filter: blur(8px);

        height: 40%;

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

      }

      .bg-text {
        background-color: rgb(0,0,0);
        background-color: rgba(0,0,0, 0.4);
        color: white;
        font-weight: bold;
        border: 3px solid #f1f1f1;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 2;
        width: 80%;
        padding: 20px;
        text-align: center;
      }
    <body>
    <div </div>
      <div >
        <h1 style="font-size:50px">Elemental</h1>
      </div>
    </body>

CodePudding user response:

try instead use filter property, use:

backdrop-filter: blur();

CodePudding user response:

Works fine if you add the > after . as A Haworth pointed out in the comments.

.bg-image {
        background-image: url("PHT4.jpg");

        filter: blur(8px);
        -webkit-filter: blur(8px);

        height: 40%;

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

      }

      .bg-text {
        background-color: rgb(0,0,0);
        background-color: rgba(0,0,0, 0.4);
        color: white;
        font-weight: bold;
        border: 3px solid #f1f1f1;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 2;
        width: 80%;
        padding: 20px;
        text-align: center;
      }
    <body>
    <div ></div>
      <div >
        <h1 style="font-size:50px">Elemental</h1>
      </div>
    </body>

  • Related