Previous task
Backgrounds6/14
Back to the list of tasks
  1. 1. Background-color property
  2. 2. Background-image property
  3. 3. Background-repeat property
  4. 4. Background-position property
  5. 5. A little more background-position
  6. 6. Background-attachment property
  7. 7. Background property
  8. 8. JPEG format
  9. 9. PNG-8 format
  10. 10. PNG-24 format
  11. 11. GIF format
  12. 12. Nested elements with backgrounds
  13. 13. Effects with repeating backgrounds
  14. 14. Sprites
Nest task
  • Courses
  • Sign up
  • Log in

Loading...
It will be ready in a few seconds.

  • Theory
  • Theory

Background-attachment property

Usually the background image scrolls along with the content of the block. This is clearly visible in the mini-browser. Scroll down the window and you will see the background rise to the top.

With the background-attachment property, you can lock the background in place and it will not move when scrolling.

Values of the property:

  • scroll — the background is scrolled along with the content. This is the default value.
  • fixed — the background does not scroll, it is fixed in one place.

This is the case when it is easier to show than to explain: perform the task and see how the background behavior changes as you scroll.

  • index.html
  • style.css
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Background-attachment property</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="block"></div> <div class="block"></div> <div class="block"></div> <div class="block"></div> </body> </html>
CSS
body { background-color: black; background-image: url("moon.jpg"); background-position: 50% 0; background-repeat: no-repeat; } .block { width: 192px; height: 256px; margin: 20px auto; background-color: #34495e; background-repeat: no-repeat; background-position: 50% 50%; border-radius: 5px; } .block:nth-child(1) { background-image: url("cat_clean.png"); } .block:nth-child(2) { background-image: url("cat_fight.png"); } .block:nth-child(3) { background-image: url("cat_hiss.png"); } .block:nth-child(4) { background-image: url("cat_poo.png"); }

The code has changed, click "Update" or enable autorun.

You have moved to another page

Click inside the mini-browser to highlight this window.

100%
GoalsCompleted
0
    1. Set fixed background image mode for the body.

    Check how the background image behaves by scrolling the mini-browser window.

    © 2023-2024, codehero.pro