Sam Teng Wong Sam Teng Wong - 13 days ago 6
CSS Question

Add opacity on an image

I'm going straight to the point here.
I want to create a simple window within an image.
outside the window will have a opacity like on the sample picture.

I'm not really good when it comes to css so please bear with me.



.section2{
}

.section2 .row{
margin: 0;
}
.the-container{
position: relative;
width: 100%;
height: 450px;
}
.the-container .text-center{
background: #fff;
opacity: .9;
}

.img-canvas{
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
width: 100%;
height: 100%;
background-image: url(https://www.aman.com/sites/default/files/styles/1371x706/public/amanpulo-location-1200-x-825.jpg?itok=4BQy9j-X);
background-size: 100% 100%;
background-position: 50% 50%;
background-attachment: scroll;
z-index: -1;
}
.window{
position:absolute;
width:50%;
height:50%;
background-size: cover;
top:0;
left:25%;
z-index: -1;
opacity: 1;
}

<section class="section2" style="height:100vh;">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="the-container">
<div class="img-canvas"></div>
<div class="window"></div>
</div>
</div>
</div>
</section>




something like this:
enter image description here

and here's a fiddle for you to manipulate the code:
https://jsfiddle.net/Lk21vL01/

thanks in advance.

Answer

You were very close, you just needed to apply similar styling to your .window element and use background-attachment:fixed

see this updated jsfiddle

.section2{
}

.section2 .row{
  margin: 0;
}
.the-container{
  position: relative;
  width: 100%;
  height: 450px;
}
.the-container .text-center{
    background: #fff;
    opacity: .9;
}
.window,
.img-canvas{
    position: absolute;
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 0px;
    width: 100%;
    height: 100%;
    background-image: url(https://www.aman.com/sites/default/files/styles/1371x706/public/amanpulo-location-1200-x-825.jpg?itok=4BQy9j-X);
    background-size: 100% 100%;
    background-position: 50% 50%;
    background-attachment:fixed;
    z-index: -1;
    opacity: 0.5;
}
.window{
  position:absolute;
  width:50%;
  height:50%;
  top:0;
  left:25%;
  z-index: -1;
  opacity: 1;
}