Search code examples
htmlcssinheritanceparallax

opacity on parallax images


I have a parent div that i am using for a parallax img scrolling feature. I would like the opacity of the parallax image to be around opactiy: 45%; and all the content within to be opactiy: 100%; I have been looking but have not found a way to do this? is their a way of setting opacity directly on the background-image?

html

<div class="img1">
        <div class="container img1-container">
            <!-- <div class="card"> -->
            <img id="head-shot" src="./assets/images/john-headshots-5.jpg" alt="head-shot">
            <br><br>
            <h1 class="intro-text web-dev"> Front-End Web Developer</h1>

            <h4 class="intro-text john-sass">John</h4>


            <!-- </div> -->
        </div>
    </div>

css

.img1 {
    background-image: url(/assets/images/skyline-1402050_1920.jpg);
    min-height: 100%;
    min-width: 100%;
    opacity: 45%;
}
/* headshot img */
#head-shot {
    text-align: center;
    border-radius: 100%;
    height: 300px;
    width: 350px;
}

Solution

  • Here is a suggested solution to this, currently I don't know of a solution that can be implemented to have only the container opacity to 45%, only way I usually implement is by having a separate <img /> with its own CSS position set to absolute and opacity 45%, like in the following snippet:

    .img1 {
      min-height: 100%;
      min-width: 100%;
      position: relative;
      overflow: hidden;
    }
    
    .parallex-bg {
      position: absolute;
      left: 0px;
      top: 0px;
      right: 100%;
      bottom: 100%;
      z-index: -1;
      opacity: 45%;
    }
    
    
    /* headshot img */
    
    #head-shot {
      text-align: center;
      border-radius: 100%;
      height: 300px;
      width: 350px;
    }
    <div class="img1">
       <img src="https://www.w3schools.com/css/img_forest.jpg" class="parallex-bg" />
       <div class="container img1-container">
          <!-- <div class="card"> -->
          <img id="head-shot" src="./assets/images/john-headshots-5.jpg" alt="head-shot">
          <br><br>
          <h1 class="intro-text web-dev"> Front-End Web Developer</h1>
          <h4 class="intro-text john-sass">John</h4>
          <!-- </div> -->
       </div>
    </div>