Previous task
Backgrounds14/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
Task list
  • Courses
  • Sign up
  • Log in

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

  • Theory
  • Theory

Sprites

A sprite is one large image that contains many small images. Sprites usually include icons and various small decorative images.

Sprites are used to reduce the number of requests to the server. Each small picture is a separate request, and the fewer requests, the better. Therefore, small pictures are "glued" into one big one.

Sprites can be raster or vector. We will consider the simplest bitmap sprites.

Bitmap sprites are images in bitmap formats. Mostly JPG and PNG.

The image of a bitmap sprite is usually much larger than the element to which you want to set the background. Therefore, only part of the sprite is always displayed inside the element. The background image is shifted using the background-position property to make the desired part of the sprite visible.

See for yourself how it works.

  • index.html
  • style.css
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Sprites</title> <link rel="stylesheet" href="style.css"> </head> <body> <button class="btn btn-add" type="button"> <span class="icon icon-add"></span> Add </button> <button class="btn btn-edit" type="button"> <span class="icon icon-edit"></span> Edit </button> <button class="btn btn-remove" type="button"> <span class="icon icon-remove"></span> Remove </button> <!-- Whole sprite picture on a dark background --> <div class="full-sprite"></div> </body> </html>
CSS
body { padding: 20px; font-family: "Tahoma", sans-serif; } .btn { display: block; width: 180px; margin: 20px auto; padding: 12px; color: white; font: inherit; text-align: left; border: 0; background-color: #34495e; border-radius: 5px; } .btn-add { background-color: #27ae60; } .btn-remove { background-color: #c0392b; } .icon { display: inline-block; width: 15px; height: 15px; margin: 0 5px; vertical-align: bottom; background-image: url("bootstrap-sprites.png"); background-repeat: no-repeat; border: 1px dotted white; } .icon-add { } .icon-edit { } .icon-remove { } .full-sprite { height: 200px; margin: 50px auto; background: #34495e url("bootstrap-sprites.png") no-repeat 50% 50%; border-radius: 5px; }

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 background position:

    1. -407px -96px — for the element with class icon-add,
    2. -96px -72px — for the element with class icon-edit,
    3. -456px 0 — for the element with class icon-remove.

    © 2023-2024, codehero.pro