Search code examples
htmlcssbackground-imagebackground-color

Overlay color and background image on the same div


Is this possible? To have a background-color overlay and background-image all on one div? As in:

div {
  color: white;
  background-color: hsla(15, 92%, 13%, 0.79);
  height: 500px;
  background-image: url('http://via.placeholder.com/500x500');
}
<div>
  <p>Content goes here</p>
</div>

Thanks!


Solution

  • You can use pseudo element like this :

    div {
      position: relative;
      color: white;
      background-image: url('http://via.placeholder.com/500x500');
      height: 500px;
    }
    
    div:before {
      content: "";
      position: absolute;
      top: 0;
      bottom: 0;
      right: 0;
      left: 0;
      background: hsla(15, 92%, 13%, 0.79);
      z-index: 0;
    }
    
    p {
      position: relative;
      z-index: 2;
    }
    <div>
      <p>Content goes here</p>
    </div>