0

Take this Code for example

#backgroundimage {
  background: url("https://www.solidbackgrounds.com/images/640x480/640x480-aero-solid-color-background.jpg");
  height: 50px
}

#container {
  background-color: #4C4C4C;
  opacity: 0.5;
}

#text {
  color: #ffffff;
  opacity: 1;
}
<div id="backgroundimage">
  <div id="container">
    <p id="text">
      Some text here
    </p>
  </div>
</div>

I want to make the text color white while keeping the background image and container opacity intact. Look at this image as Reference

connexo
  • 53,704
  • 14
  • 91
  • 128

3 Answers3

3

Have a look at the following example. Instead of opacity use rgba. According to docs:

opacity applies to the element as a whole, including its contents, even though the value is not inherited by child elements. Thus, the element and its children all have the same opacity relative to the element's background, even if they have different opacities relative to one another.

#backgroundimage {
  background: url("https://www.solidbackgrounds.com/images/640x480/640x480-aero-solid-color-background.jpg");
  height: 500px
}

#container {
  background-color: rgba(76,76,76,.5);
}

#text {
  color: #ffffff;
}
<div id="backgroundimage">
  <div id="container">
    <p id="text">
      Some text here
    </p>
  </div>
</div>
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
0

When you set the opacity of an element, it affects its children (and if you set an opacity of a child, it can't override the opacity of the parent.

What you need to do is not to set the opacity of the element, but set opacity only to the background - using rgba.

#backgroundimage {
  background: url("https://www.solidbackgrounds.com/images/640x480/640x480-aero-solid-color-background.jpg");
  height: 500px
}

#container {
  background-color: rgba(76, 76, 76, 0.5); /* #4C4C4C;*/
  /*opacity: 0.5;*/
}

#text {
  color: #ffffff;
  opacity: 1;
}
<div id="backgroundimage">
  <div id="container">
    <p id="text">
      Some text here
    </p>
  </div>
</div>
Programmer
  • 1,973
  • 1
  • 12
  • 20
-2

Make the background image (640x480-aero-solid-color-background.jpg) transparent and save it as .png or .gif format.

remove "opacity: 0.5" from "#container" class and "opacity: 1" from "#text" class