Home > Blockchain >  Text on an image with vignette effect CSS
Text on an image with vignette effect CSS

Time:05-17

I am trying to create a page banner which is an image with text over it. There are two things I am facing trouble with two things.

  1. I need a smooth vignette effect on these images so that the text is clearly visible. I have tried the following but am not happy with the end result. I would like to get something as shown in the following image which shows a kind of smooth transition.

  2. The text hides below the vignette effect. I tried using z-index but it does not work.

body {
            margin: 0px;
        }

        #page-banner img {
            width: 100%;
            object-fit: cover;
            height: 48vh;
        }

        #page-banner .vignette:after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            width: 100%;
            height: 100%;
            -webkit-box-shadow: inset 20em 5em 15em black;
            -moz-box-shadow: inset 20em 5em 15em black;
            box-shadow: inset 20em 5em 15em black;
            z-index: 0;
        }

        #page-banner .text {
            width: 50%;
            right: 50%;
            z-index: 1;
        }

        #page-banner .text-over-image {
            position: relative;
        }

        #page-banner .text-over-image p {
            font-size: 60px;
            color: white;
            margin: 0;
            position: absolute;
            top: 50%;
            left: 15%;
            right: 45%;
            transform: translate(-10%, -50%);
        }
<section id="page-banner">
    <div >
        <img src="https://wallpaperaccess.com/full/5117570.jpg">
        <div >
            <p>I am trying to learn adding vignette to images</p>
        </div>
    </div>
</section>

enter image description here

CodePudding user response:

You could use a radial-gradient instead with transparent center and black toward the outside or whatever colors you want to use. See: https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/radial-gradient

You can get more granular with the gradient with steps, I just used a simple two step for this example.

body {
  margin: 0px;
}

#page-banner img {
  width: 100%;
  object-fit: cover;
  height:100%;
}

#page-banner .vignette:before {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 100%;
  content: radial-gradient(transparent, black);
  z-index: 0;
}

#page-banner .text {
  width: 50%;
  right: 50%;
  z-index: 1;
}

#page-banner .text-over-image {
  position: relative;
}

#page-banner .text-over-image p {
  font-size: 60px;
  color: white;
  margin: 0;
  position: absolute;
  top: 50%;
  left: 15%;
  right: 45%;
  transform: translate(-10%, -50%);
}
<section id="page-banner">
  <div >
    <img src="https://wallpaperaccess.com/full/5117570.jpg">
    <div >
      <p>I am trying to learn adding vignette to images</p>
    </div>
  </div>
</section>

CodePudding user response:

Dont' use box-shadow. Instead give your ::after this:

background: hsla(0, 0%, 0%, 0.50);

then give your text z-index = 1;

Just play with your color's alpha to get the desired look.

  • Related