Previous task
Backgrounds4/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-position property

The background-position property controls the position of the background image. The property value consists of two parts separated by a space: x y.

x specifies the horizontal position, and y the vertical position.

The keywords left, center, right, percentage and pixel values can be used as the x value.

The keywords top, center, bottom, percentage and pixel values can be used as the y value.

Examples of property usage:

background-position: 50% 50%;
background-position: right bottom;
background-position: 50px 100px;
background-position: 0 100%;
background-position: left bottom;

This is how the code from the examples will work:

  1. the picture will be centered;
  2. bottom right corner;
  3. indent 50px from the left edge and 100px from the top;
  4. left bottom corner;
  5. left bottom corner.
  • index.html
  • style.css
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Background-position property</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="block block1"></div> <div class="block block2"></div> <div class="block block3"></div> <div class="block block4"></div> </body> </html>
CSS
body { padding: 10px; text-align: center; } .block { display: inline-block; width: 192px; height: 192px; margin: 5px; border-radius: 5px; box-shadow: 1px 1px 3px #999999; } .block1 { background-color: #2ecc71; background-image: url("cat_box.png"); background-repeat: no-repeat; } .block2 { background-color: #3498db; background-image: url("cat_fish.png"); background-repeat: no-repeat; } .block3 { background-color: #e74c3c; background-image: url("cat_drunk.png"); background-repeat: no-repeat; } .block4 { background-color: #e67e22; background-image: url("cat_tied.png"); background-repeat: no-repeat; }

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

    Set the background position for the blocks:

    1. 50% 50% — to the first block,
    2. 100% 0 — to the second block,
    3. 0 100% — to the third block,
    4. 100% 100% — to the fourth block.

    © 2023-2024, codehero.pro