Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use css sprites? [closed]

Tags:

css

sprite

I read somewhere that css sprites are better than using simple images. How is that ? also I am new to Html so can anyone tell me how css works and give me a simple example for css sprites?


2 Answers

Here is the complete code. Copy and paste in a notepad and save it as abc.html. Save the two images below in the same folder as: image.gif for big one and trans.gif for small one.

<html>
<head>
    <title>Image Sprites</title>
    <style type="text/css">
        img.sprite
        {
            width:46px;
            height:44px;
            background:url(image.gif) 0px 0px;
        }        

        .sprite:hover
        {
            background: url(image.gif) 0px 44px;
        }
     </style>
</head>

<body>
    <img class="sprite" src="trans.gif" />
</body>
</html>
  • image.gif on imgur
  • trans.gif on imgur (1x1 pixels)

In simple words, CSS sprites use only one image instead of many. So instead of many image requests from server only a single request is sent.

like image 114
Hisham Muneer Avatar answered Dec 21 '25 03:12

Hisham Muneer


One trip to the server to get the images vs. multiple ones. I found SpritePad (online app) for building them, it will also generate the relevant boiler plate css (which can be a bit messy if your're starting out).

like image 27
vector Avatar answered Dec 21 '25 03:12

vector



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!