Skip to content
WJunction - Webmaster Forum

CSS - Sprites (Web 3.0) Tutorial

Status
Not open for further replies.
Heya all,

ive been redesigning litewarez over the lsat few weeks and ive started to implement a new feature with css called sprites.

What it does:

Basically what you do is create on image containing more than one image then using css you will select the location of a particular image within your main image

this way you can have all your images with just one request this saves alot of bandwidth and speed for your users.

Heres an example of of what the image looks like HERE

here is an example of me using sprite for my download icons

Code:
/*This will load the image into the class name sprite_dlIcons*/
.sprite_dlIcons{background:url('../images/sprite_icons.png');}
			
/*Icons*/
	/*This will set the width of every icon*/
	.Icon{width:16px;}
		
		/*This is individual classes for each ic.. and there positions*/
		.tv{ height:16px; background-position:-0px -0px;}
		.template{ height:16px; background-position:-0px -16px;}
		.xxx{ height:16px; background-position:-0px -30px;}
		.other{ height:16px; background-position:-0px -46px;}
		.music{ height:16px; background-position:-0px -63px;}
		.movie{ height:16px; background-position:-0px -80px;}
		.game{ height:16px; background-position:-0px -98px;}
		.ebook{ height:16px; background-position:-0px -112px;}
		.app{ height:16px; background-position:-0px -130px;}

Here is the image that's used for the sprite
sprite_icons.png


Ok so now the html for the icons

Code:
<div class="sprite_dlIcons Icon tv"></div>
<div class="sprite_dlIcons Icon game"></div>
<div class="sprite_dlIcons Icon ebook"></div>
<div class="sprite_dlIcons Icon app"></div>

now as you can see were setting multiple classes for the div.

the first to are going to be be loaded for all divs using the sprite and the last one is dynamic because they are select different pixal locations for the icons

if you wish to follow this up any further there;s plenty of information on Google :D
hope you enjoy my tip.

Helpfull Links:
Sprite Generator : Provided By -=MOD=-
 

14 comments

this only downfall would be when creating new aimages, to add to that image lol. other then that nice tut.
i use the rollover and the possition for my navbar
 
Nice tutorial i didn't know what you was on about before but now i do! i may try this one time :)
 
the first css block is the width of all your images unless they vary you can place them within the individual css icon blocks so if you want to got H instead of V you will do summat like this

Code:
.Icons{width:20px;} // will set the width of each button to 20px;
//  |__20px__||__20px__||__20px__||__20px__|
// so by having for icons in a sprite that would mean the image is 4x20 = 80px wide

//Then you would change the individuals so do the following
.tv{ height:30px; background-position:-0px -0px;} //iamge 1
.app{ height:30px; background-position:-20px -0px;} // image 2

//what this does is sets the hight of the image to 30px and the first background-position is how many pixals from the left you want the image to start so and so on

hope this help you
 
This will work for different heights right?

If i do it like this

.ebook{ height:200px; background-position:-0px -112px;}

.app{ height: 200px; background-position:-0px -214px;}

?
 
yes that will work perfect, the way multiple dhtml class'es work is that when you add summat like this

<div class="bold underlined">InnerHtml</div>

OUTPUT : InnerHtml

it will join the too css rules and merge them to gether so you could have the rules like so

#blod{font-waight:bold;}
#underline{text-decoration:underline}

see what im saying here
 
Status
Not open for further replies.

About the author

L
Active Member · Joined
I am a skilled programmer within PHP,HTML,CSS,MYSQL and also the owner of several websites Programming, Football...
1,367
Messages
1
Reactions
38
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom