Search code examples
htmlcss

How to display text over image without background-color showing


tl;dr - How to avoid background-color showing on text in front of images in css.

Background

The w3Schools site for displaying text over an image (link here) uses a relative-container, absolute-child setup. Under normal circumstances this works fine (see code and image below).

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.container {
  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%);
}
</style>
</head>
<body>

<h2>Image Text</h2>
<p>How to place text over an image:</p>

<div class="container">
  <img src="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>

</body>
</html> 

Image of text over image

However, if your file has a background-color set, that background color will bleed through on the absolute positioned elements (see code image below).

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
   background-color: black;
}

.container {
  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%);
}
</style>
</head>
<body>

<h2>Image Text</h2>
<p>How to place text over an image:</p>

<div class="container">
  <img src="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>

</body>
</html> 

Text over image with black background color

Question

How can one have text overlay an image without this "background color bleed through" issue coming up? The goal is to have the absolute positioned elements have no background color, or a transparent one (setting the absolute element's background-color to transparent doesn't seem to work).

I'm aware that absolute positioning elements are treated differently than in-line elements, but I'm unclear on how to resolve this issue. The accepted solution for this post seems to work if trying to set the absolute element to have a specific color, but my goal is to have no background color so that only the text is shown in front of the image.


Solution

  • You are setting the background color of everything (including divs) to black. If you want to change the background color of the body, replace * with body:

    body {
      background-color: black;
    }
    
    .container {
      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%);
    }
    <h2>Image Text</h2>
    <p>How to place text over an image:</p>
    
    <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>