CSS Image Sprites

Introduction

 
Using image sprites you can divide the portion of one image into other small images. Every single image works standalone. In simple terms, you can say one image is a collection of other small images.
 
The main benefit of using image sprites is that it reduces the HTTP request and size of an image (see the following figures).
 
If your web page has many images then it will take more time to load your web page on the server and also generate multiple HTTP requests. But when image sprites are used it take less time to load page as well as take less time to generate HTTP server requests.
 
image sprites        image sprites
image sprites
image sprites
image sprites
image sprites
 
image sprites image sprites
 
Code:
 
HTMLPage.htm
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <link href="StyleSheet.css" rel="stylesheet" type="text/css" />  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <div class="firstmain">  
  9.         <ul id="FirstUL">  
  10.             <li id="first"><a href="white.htm"></a></li>  
  11.             <li>  
  12.                 <div id="second">  
  13.                     <a href="blue.htm"></a>  
  14.                 </div>  
  15.             </li>  
  16.              
  17.         </ul>  
  18.         <ul id="SecondUL">  
  19.         <li id="third"><a href="red.htm"></a></li>  
  20.         <li id="forth"><a href=yellow.htm></a></li>  
  21.         </ul>  
  22.     </div>  
  23.      <div class="secondmain" >  
  24.         <a class="fifth" href="green.htm"></a>  
  25.     </div>  
  26. </body>  
  27. </html>  
StyleSheet.css
  1. #FirstUL  
  2. {  
  3.     margin0;  
  4.     padding0;  
  5.     width450px;  
  6.     floatleft;  
  7.     height160px;  
  8. }  
  9. #FirstUL ul  
  10. {  
  11.     list-stylenone;  
  12.     margin0;  
  13.     padding0;  
  14. }  
  15. #FirstUL li  
  16. {  
  17.     padding0;  
  18.     margin0;  
  19.     floatleft;  
  20.     list-stylenone;  
  21.     top: 0;  
  22.     list-stylenone;  
  23. }  
  24. #FirstUL a  
  25. {  
  26.     height157px;  
  27.     width157;  
  28.     displayblock;  
  29. }  
  30. #first  
  31. {  
  32.     background-imageurl('image/all balls.jpg');  
  33.     background-position0 0;  
  34.     background-repeatno-repeat;  
  35.     width157px;  
  36.     height157px;  
  37. }  
  38. #second  
  39. {  
  40.     margin-left29px;  
  41.     background-imageurl('image/all balls.jpg');  
  42.     background-position-184px 0;  
  43.     background-repeatno-repeat;  
  44.     width157px;  
  45.     height157px;  
  46. }  
  47. #SecondUL  
  48. {  
  49.     margin0;  
  50.     padding0;  
  51.     width850px;  
  52.     floatleft;  
  53.     margin-top154px;  
  54.     margin-left-420px;  
  55.     height157px;  
  56. }  
  57. #SecondUL ul  
  58. {  
  59.     list-stylenone;  
  60.     margin0;  
  61.     padding0;  
  62. }  
  63. #SecondUL li  
  64. {  
  65.     padding0;  
  66.     margin-right100PX;  
  67.     height160PX;  
  68.     floatleft;  
  69.     list-stylenone;  
  70.     top: 0;  
  71.     list-stylenone;  
  72. }  
  73. #SecondUL a  
  74. {  
  75.     height157px;  
  76.     width157px;  
  77.     displayblock;  
  78. }  
  79. #third  
  80. {  
  81.     margin-left29px;  
  82.     margin-top20PX;  
  83.     background-imageurl('image/all%20balls.jpg');  
  84.     background-position-9px -174px;  
  85.     background-repeatno-repeat;  
  86.     width157px;  
  87.     height157px;  
  88. }  
  89. #forth  
  90. {  
  91.     background-imageurl('image/all balls.jpg');  
  92.     background-position-293px -152px;  
  93.     background-repeatno-repeat;  
  94.     width157px;  
  95.     height157px;  
  96. }  
  97. .firstmain  
  98. {  
  99.     margin0px;  
  100.     padding0px;  
  101.     height330px;  
  102.     width900px;  
  103. }  
  104. .fifth  
  105. {  
  106.     background-imageurl('image/all%20balls.jpg');  
  107.     background-position-164px -290px;  
  108.     background-repeatno-repeat;  
  109.     padding0px;  
  110.     width157px;  
  111.     height157px;  
  112. }  
  113. .secondmain  
  114. {  
  115.     margin-left200px;  
  116.     margin-top-27px;  
  117.     width157px;  
  118.     height157px;  
  119. }  
  120. .secondmain a  
  121. {  
  122.     margin0px;  
  123.     padding0px;  
  124.     height157px;  
  125.     width157;  
  126.     displayblock;  
Output
 
Internet Explorer
 
image sprites
 
Chrome
  
image sprites
 
Fire Fox
 
CSS-image.gif
 
Safari
 
image sprites
 
When you click on the red ball you will be navigated to the Red.htm page.
 
Output
 
image sprites