To overlay text on an image using CSS


How to overlay text on an image using CSS?

<!DOCTYPE html>
<html>
<head>
  <style>
    .card {
      position: relative;
      width: 300px;
      color: white;
      font-family: sans-serif;
    }

    .card img {
      width: 100%;
      display: block;
    }

    .card .overlay {
      position: absolute;
      top: 0; left: 0;
      width: 100%; height: 100%;
      background-color: rgba(0,0,0,0.5);
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 20px;
    }
  </style>
</head>
<body>
  <div class="card">
    <img src="https://via.placeholder.com/300x200" alt="Image">
    <div class="overlay">Hello World</div>
  </div>
</body>
</html> 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.