Background Attachment

Exercise

  1. Download the image from this GitHub repository:
  2. https://github.com/learn2buildapps/CSS_Background_Attachment/blob/main/images/another-game.jpg

  3. Place the image you downloaded into the "images" folder.
  4. 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 Attachment</h1>
  <div id="big-game-image">
    <p>Big Game Image</p>
  </div>
  <div id="another-game-image">
    <p>Another Game Image</p>
  </div>
  <div id="big-game-image">
    <p>Big Game Image</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: 100vh;
  color: white;
  border: 2px solid yellow;
}

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

#another-game-image {
  background-image: url('../images/another-game.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
}
  1. Save.
  2. In the browser, you should see the following:

  3. Exercise 2 Result

Code Walkthrough and Explanation


background-attachment

In CSS, the background-attachment property specifies whether the background image is fixed or scrolls along with the content. It can take one of the following values:

By default, background-attachment is set to scroll.


Parallax Effect

A parallax effect is a visual technique used in web design where the background of a webpage moves at a slower pace than the foreground, creating an illusion of depth and dimensionality. This effect is achieved by applying different scrolling speeds to different layers of content on the webpage. As the user scrolls down the page, the foreground elements move faster than the background elements, creating a dynamic and engaging visual experience.


Experiment with the Code


Video and Code References


Questions? Subscribe and ask in the video comments:




GitHub and Other References