Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Opacity suppresses background color if child element has same background color

I would like to create a modal. I want the parent element to be slightly transparent and dark to make the modal object more visible. I want the modal to have a black background. However, the model disappears with a black background. And I wonder why does it disappear? And what do I have to do to make the modal have a black background?

What I expect

enter image description here

Working example with a color other than black. working fine

<div id="area" class="area">
  <div class="cover"></div>
</div>

.area {
  position: relative;
  flex: 1 0 auto;
  margin: 10px 15px;
  padding: 10px;
}

.area .cover {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: black;
  opacity:0.8;
}

.note {
  width: 50%;
  background: green;
  margin: 0px auto;
  color: white;
  display: flex;
  justify-content: center;
  margin-top:10%;
  border: 1px;
  border-radius: 8px;
  height: 200px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  padding:10px;
  box-sizing: border-box;
  font-size: 2rem;
}
<div id="area" class="area">
  <h1>lorem ipsum</h1>
  <div class="cover">
    <div class="note">xx</div>
  </div>
</div>

Not working when the background-color is black

<div id="area" class="area">
  <div class="cover"></div>
</div>

.area {
  position: relative;
  flex: 1 0 auto;
  margin: 10px 15px;
  padding: 10px;
}

.area .cover {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: black;
  opacity:0.8;
}

.note {
  width: 50%;
  background: black; /* not working with black*/
  margin: 0px auto;
  color: white;
  display: flex;
  justify-content: center;
  margin-top:10%;
  border: 1px;
  border-radius: 8px;
  height: 200px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  padding:10px;
  box-sizing: border-box;
  font-size: 2rem;
}
<div id="area" class="area">
  <h1>lorem ipsum</h1>
  <div class="cover">
    <div class="note">xx</div>
  </div>
</div>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That is because you set opacity on the whole of modal, and that includes the .note as well.

You should use background-color:rgba(0,0,0,0.8); for the background of the modal. This way the background color is semi-trasparent and not the whole element.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading