Image Sprites - How to get portion of an image

/ Published in: CSS
Save to your folder(s)

One of the great methods to load and work with icons in web pages is to load one image that is a collection of several icons and use any icon as needed, that reduces the number of server request and save bandwidth.

Copy this code and paste it in your HTML
  1. <style>
  2. div.img1{
  3. width:32px; /* width of selected icon */
  4. height:32px; /* height of selected icon */
  5. background:url(http://mysite/image.png) -130px -16px; /* the position of icon in the main image */
  6. }
  8. div.img2{
  9. width:29px;
  10. height:27px;
  11. background:url(http://mysite/image.png) -35px -2px;
  12. }
  13. </style>
  15. <body>
  16. <div class="img1"></div>
  17. <div class="img2"></div>
  18. </body>


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.