Previous task
Backgrounds2/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-image property

The background image can be set using the CSS property background-image. It is done in the following way:

selector {
  background-image: url("image-address");
}

The address of the image must be enclosed inside url("..."). For example, if the image address is cat.png, you can connect it as a background image like this:

background-image: url("cat.png");

You can set both the background color and the background picture at the same time. In this case, the picture will be displayed on top of the background color. For example:

.block-with-color-and-image {
  background-image: url("cat.png");
  background-color: red;
}
  • index.html
  • style.css
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Background-image 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; } .block2 { background-color: #3498db; } .block3 { background-color: #e74c3c; } .block4 { background-color: #e67e22; }

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

    Add background images to the blocks:

    1. cat_box.png — for the first block,
    2. cat_fish.png — for the second block,
    3. cat_drunk.png — for the third block,
    4. cat_tied.png — for the fourth block.

    © 2023-2024, codehero.pro