Using CSS Sprites On Blogger - Introduction

CSS-Sprites
I'm sure many of you would have visited websites like Yahoo!, YouTube, Facebook, etc.. Have you ever wondered how quick these websites load though they possess a lot of images in the background? How is it possible? Is it possible only for websites - not for blogs on blogger? Definitely not, we can have these effects on blogger too. But how? Answer: Using CSS Sprites!!!

WHAT ARE CSS SPRITES? ARE THEY USEFUL?
Well, the name might be little misleading. Sprites are not small tiny pictures as you imagine. A sprite is one big image - usually a cluster of small images used on a website. By using CSS Sprites, we achieve the following advantages.
  • Total image size is generally less when compared to the size of the combined individual images. So, a considerable bandwidth is saved. 
  • Few images for the browser to load which means fewer HTTP requests (per page view). Multiplying this with the number of visitors per day, per week, per month, per year. Definitely a lot of saving... Isn't it?
  • Most important - no JavaScript, no jQuery - only CSS!!!
Well, I guess the question of "Whether Sprites are useful?" has got erased from your mind now. Moreover, we recently heard from Google that page speed will be reflected on the respective blog's PageRank. So, if you wish to have a better PR by increasing the page speed of your blog, then Sprites would come in handy!

SIMPLE HOVER EFFECT
In this article, I will just illustrate the very basics of CSS Sprites - to get hold of "What's CSS Sprites". In the upcoming articles, you will find a detailed explanation about how sprites could be applied to all the images in a blog. Here, we restrict ourselves to one simple trick - Hover Effect Using Sprites. Take a look at the demo before we proceed to the tutorial. Hover your mouse onto the tweety bird to the right. Cool Effect! Isn't it?












TUTORIAL
Wondering where to use this effect? It's most effective on navigation bars. Other areas include highlighting a link (or) usage in the header of your blog - anywhere you need a hover effect. I would like to illustrate this effect with my very own example - tweety bird. Before we get into the coding part, make sure you have identical images - same width and height.

The first image (PREVIEW) which I used is just the outline of the tweety bird while the second one (PREVIEW) is filled with colors. Later, I clubbed both the images onto a single file using Photoshop. Clubbed??? It's just placing one image below an another. You place the images horizontally one after another (or) place them one below another. I chose to place them one below another. Have a look at the final image - PREVIEW. The final image is the one which will be used for the hover effect using CSS Sprites.

HTML PART
The following piece of code is the HTML part for the above effect.
<a href='#' id='logo-link1'/>
Note: Make sure the ID used is unique (code highlighted in pink). You may place it anywhere in between your body tags to invoke the effect. I have used it in my post area in this article. You may point to any URL by replacing # with the link your wish to point to - it's pretty self-explanatory.

CSS PART
1. Navigate to "Design>>Edit HTML" and make a backup of your template by clicking on the "Download full template" link at the top of the page. Once the back up is over, search for the following piece of code.
]]></b:skin>
2. Place the following piece of code just above the line mentioned in step 1.
#logo-link1
{
width:271px;
height:288px;
text-decoration:none;
display:block;
background-image:url(https://lh6.googleusercontent.com/-pAJTOcbwUh8/TfilybD4R_I/AAAAAAAABGo/L1OQ5951gi8/s800/tweety-sprites-tutorial.png);
background-position:0px -288px;
}
#logo-link1:hover,#logo-link1:active
{
background-image:url(https://lh6.googleusercontent.com/-pAJTOcbwUh8/TfilybD4R_I/AAAAAAAABGo/L1OQ5951gi8/s800/tweety-sprites-tutorial.png);
background-position:0px 0px;
}
Note: When the browser loads the image, I want the outline alone. For this reason, I set the background position of the image to 0px horizontally & -288px vertically (code highlighted in green). On hover, I need the colored version. So, I just move the vertical position from -288px to 0px (code highlighted in orange). That's it!
3. Save your template. That's it!

Pretty simple!!! Isn't it? Since this is an introductory article on sprites, it looks very easy.  Moreover, we have dealt with only one image. Imagine when it multiplies for all the images on your blog. It will be very difficult to exactly position your images and get the padding right. We will illustrate with better examples in the upcoming articles. Share your opinions or clear your doubts about sprites via comments!

Enjoyed the article?
Subscribe to this Blog via Email:

Click here to subscribe to FREE email updates from "Everything About Blogging", so that you do not miss out anything that can be valuable to you and your blog!

    Other Recommended Posts

  • CSS Sprites, CSS Tricks, Web Design
Shaw Philips

Nice tips... will try these soon.
Nice footer. Is it misaligned???

REPLY
Mukund said on June 17, 2011 at 12:14 AM :

@Shaw Philips: Yup! Let us know once you try them out! Nope! It is aligned. Do you see a misalignment any where? If so, let me know via email with a screenshot. Thanks for your comment by the way!

REPLY
Sagar said on June 17, 2011 at 2:17 AM :

This effect is really cool buddy.. waiting advanced customization's with this effect.

REPLY
Mukund said on June 17, 2011 at 11:06 AM :

@Sagar: Yup! I will soon post about the usage of Sprites on blogs. This is just one basic effect with Sprites. Anyway, thanks for the comment Sagar!

REPLY
vikash shankar said on June 17, 2011 at 7:18 PM :

hi Thanks for reply
how can i manage the networked blog app. in facebook to publish my post directly on my facebook's page wall.please tell me the steps..

REPLY
Mukund said on June 17, 2011 at 7:28 PM :

@vikash shankar: Sure! I will soon publish an article about the same! It's quite easy to do that! If you need it in a quick time, ping me via gtalk or email me. I will help you out!

REPLY
Amit said on June 18, 2011 at 12:30 PM :

Hey, nice article mukund, You said that you would place two images one on top of other using photoshop. But there are lots of sites that generate sprites readily for you - more easier to do...

REPLY
Mukund said on June 18, 2011 at 3:53 PM :

@Amit: Well, please understand the post Amit! You are talking about the actual implementation! I have talked only about the hover effect. To have the hover effect, you will need to two images. When it comes to designing a website, yes you are right! The sprite generators would do the job! Thanks for your comment buddy!

REPLY
Shaw Philips

@Mukund See the image > goo.gl/e3VAl
Your template looks fine in firefox, but not in chrome!

REPLY
Mukund said on June 19, 2011 at 6:29 PM :

@Shaw Philips: I'm not sure why it shows that way! I set the width to 960px but it elongates more than that! I verified the template on three major platforms - Firefox, IE and Opera! A friend of mine did say it looks perfect on Chrome! I'm not sure why it shows up a little different for you! Anyway, I will check out the problems! Thanks for the info!

REPLY
gamep01nt said on July 19, 2011 at 5:41 PM :

I have this image, http://bit.ly/nV0GnM

As u can see, i have 2 images, the top one is grey in color, i want to put that image on normal condition (when the image is not hovered)

And how do i put these 2 image at my sidebar?

REPLY
Mukund said on July 19, 2011 at 6:16 PM :

@gamep01nt: Pretty simple! But I suggest you to leave a small spacing between the Grey version and the colored version. I can't share the code here because the comment section on blogger will not allow me to do that! Just ping me via gtalk and I'll help you out!

REPLY
gamep01nt said on July 19, 2011 at 7:50 PM :

@Mukund

How about i contact u through your facebook fanpage?

REPLY
Mukund said on July 19, 2011 at 8:32 PM :

@gamep01nt: Well no issues! But, I'm always available on gtalk. Facebook I check once in a day! So, the choice is left to yours!

REPLY

Use the tab below to comment. No spam please!!!

[netinsert=0.0.1.11.14.1]