Background Image Overlay

Exercise

  1. In the "index.html" file, replace the code with the following:
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS</title>
  <link rel="stylesheet" href="./css/styles.css">
</head>

<body>
  <h1>Background Image Overlay</h1>
  <div id="big-game-image">
    <p>Big Game Image</p>
  </div>
  <div id="big-game-image-overlay">
    <p>Big Game Image with Overlay</p>
  </div>
</body>

</html>
  1. Save.
  2. If "Live Server" is not already running, right-click anywhere in the editor and select "Open with Live Server" from the context menu.
  3. In the "styles.css" file, replace the code with the following:
div {
  height: 50vh;
  color: white;
  border: 2px solid yellow;
  text-align: center;
  font-size: 50px;
}

#big-game-image {
  background-image: url('../images/big-game.jpg');
  background-repeat: no-repeat;
  background-size: cover;
}

#big-game-image-overlay {
  background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('../images/big-game.jpg');
  background-repeat: no-repeat;
  background-size: cover;
}
  1. Save.
  2. In the browser, you should see the following:

  3. Exercise 2 Result

Code Walkthrough and Explanation


Background Image Overlay

Background image overlay is a technique used in CSS to add a semi-transparent layer on top of a background image. This technique is often used in web design to improve the readability of text placed over a background image. There are a few ways to achieve this effect. In the exercise, we used the linear-gradient property with two rgba colors.


Experiment with the Code


Video and Code References


Questions? Subscribe and ask in the video comments:




GitHub and Other References