2

I want to show transparent background overlay between image & text so that text is readable.

I tried in the below code for some reason its not working for container or image

.container {
  max-width:1000px;
  position: relative;
  text-align: center;
  color: white;
}

.bottom-left {
  position: absolute;
  bottom: 8px;
  left: 16px;
}

.top-left {
  position: absolute;
  top: 8px;
  left: 16px;
}

.top-right {
  position: absolute;
  top: 8px;
  right: 16px;
}

.bottom-right {
  position: absolute;
  bottom: 8px;
  right: 16px;
}

.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.container > img::before{position:absolute; background-color:red; width:100%; height:100px;}

/*.container ::before{position:absolute; background-color:red; width:100%; height:100px;}

.container::before{position:absolute; background-color:red; width:100%; height:100px;}*/
<div class="container">
  <img src="https://www.w3schools.com/howto/img_snow_wide.jpg" alt="Snow" style="width:100%;">
  <div class="bottom-left">Bottom Left</div>
  <div class="top-left">Top Left</div>
  <div class="top-right">Top Right</div>
  <div class="bottom-right">Bottom Right</div>
  <div class="centered">Centered</div>
</div>
Learning
  • 17,618
  • 35
  • 153
  • 314

2 Answers2

1

:before and :after only work with non-replaced elements.

You can clear from here : CSS :after not adding content to certain elements

And here is your solution.

I just wrapped image with div and apply pseudo on it.

Hope it may help you.

.img_container{
    position: relative;
    z-index: 0;
}
.img_container:before {
    position: absolute;
    background-color: rgba(255, 0, 0, 0.5);
    width: 100%;
    height: 100%;
    content: "";
 
    z-index: 1;
}
.container {
  max-width:1000px;
  position: relative;
  text-align: center;
  color: white;
}

.bottom-left {
  position: absolute;
  bottom: 8px;
  left: 16px;
}

.top-left {
  position: absolute;
  top: 8px;
  left: 16px;
}

.top-right {
  position: absolute;
  top: 8px;
  right: 16px;
}

.bottom-right {
  position: absolute;
  bottom: 8px;
  right: 16px;
}

.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<div class="container">
  <div class="img_container"><img src="https://www.w3schools.com/howto/img_snow_wide.jpg" alt="Snow" style="width:100%;"></div>
  <div class="bottom-left">Bottom Left</div>
  <div class="top-left">Top Left</div>
  <div class="top-right">Top Right</div>
  <div class="bottom-right">Bottom Right</div>
  <div class="centered">Centered</div>
</div>
1

inline element don't accept Pseudo classes. Instead of using img you can use container div for this. Check snippet below..

.container {
  max-width:1000px;
  position: relative;
  text-align: center;
  color: white;
}

.bottom-left {
  position: absolute;
  bottom: 8px;
  left: 16px;
}

.top-left {
  position: absolute;
  top: 8px;
  left: 16px;
}

.top-right {
  position: absolute;
  top: 8px;
  right: 16px;
}

.bottom-right {
  position: absolute;
  bottom: 8px;
  right: 16px;
}

.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.container::before{position:absolute; background-color:rgba(255,0,0,0.5); width:100%; height:100%; content:''}

/*.container ::before{position:absolute; background-color:red; width:100%; height:100px;}

.container::before{position:absolute; background-color:red; width:100%; height:100px;}*/
<div class="container">
  <img src="https://www.w3schools.com/howto/img_snow_wide.jpg" alt="Snow" style="width:100%;">
  <div class="bottom-left">Bottom Left</div>
  <div class="top-left">Top Left</div>
  <div class="top-right">Top Right</div>
  <div class="bottom-right">Bottom Right</div>
  <div class="centered">Centered</div>
</div>
Super User
  • 8,642
  • 3
  • 24
  • 43