WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You

WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Accespted CSS Sprites (Part 2/2) Wed Dec 08, 2010 9:14 pm

Guest


Guest
CSS Sprites (Part 2/2)
Using CSS Sprites in your page


Hey there once again.Welcome to the second part of CSS Sprites.In order to undestand whats coming up,i suggest you to read the first part.If you did,then prepare yourself to work Very Happy .I will try to explain everything the best i can so that you clearly undestand how to work with css sprites Smile


Let the tut begin!

0)Introduction

There are many sites using css sprites.Facebook,E-bay,Dragon interactive and our lovely appple Smile .At the previous part we have mentioned the navbar effect on the apple's website.Right now,we are going to make a clone of the navbar in order to undestand how css sprites work Smile


1) Creating the image



We are now going to create the navbar.Actually two,one of them which will contain the mouse over effect.Just to give you a quick example of how we are going to create the navbar,take a look at our graphic designer's Iros, navbar:

CSS Sprites (Part 2/2) Home10CSS Sprites (Part 2/2) Calend10CSS Sprites (Part 2/2) Galler10CSS Sprites (Part 2/2) Faq10CSS Sprites (Part 2/2) Search10CSS Sprites (Part 2/2) Login10

The above are a collection of 6 seperate images.Looks like a single image cause i have placed them with an inline style.This is exactly what we are going to do with css sprites.

Now,i will be giving you a clone image of apple's navbar containing 2 in total navs(the default and the mouseover effect).Click the spoiler for the image.
Spoiler:

2)Working with HTML

We are going to use an unordered list because they are great especially for navigation. So create an unordered list like the one below.We have specified the li tag at every nav link and also placed the identity(id) at each of them because we are going to need it at the css to work on the placement of the sprites.

Code:
<ul id="nav">
          <li id="list1"><a href="#"><span>Apple</span></a></li>
          <li id="list2"><a href="#"><span>Store</span></a></li>
          <li id="list3"><a href="#"><span>Mac</span></a></li>
          <li id="list4"><a href="#"><span>iPod + iTunes</span></a></li>
          <li id="list5"><a href="#"><span>iPhone</span></a></li>
          <li id="list6"><a href="#"><span>Downloads</span></a></li>
          <li id="list7"><a href="#"><span>Support</span></a></li>
</ul>


3) Working with CSS
Read these lines with attention so that we make everything clear.As mentioned in part 2,the first part we will be working at is the div selector for the customisation of the nav.We are going to set as background of our navbar the image i gave you before.Its height is 76px and the width 876 px.

Notice:Remember that we will now be working at the standard navbar.The image contains 2 navbars,so the height of the standard navbar is 38px.Why?Because we do not want the bottom half of the picture to show up until one of the buttons on the nav is hovered over.

So,we are setting the height of the unordered list to 38,which is the half of 76.
___________________

Extra modifications:

Code:
Margin: 0 auto;

That is just to center the Unordered list in your browser window.I suggest you to place this so that there wont appear any bugs at the nav.
___________________

Now,lets let a look at our css so far:

Code:
* { margin: 0px; padding: 0px; }

#nav {
      background: url(http://i54.tinypic.com/2n735mp.png);
      height: 38px;
      width: 876px;
      margin: 0 auto;
}

#nav span {
    display: none;
}

Now,we are going to list the items of our navbar horizontaly because thats the structure of our navigation.So we float the list items to the left so that they line up horizontally(or we set the style to inline; but floating is better for navigations).

We also want to get rid of the bullets of the list cause they are pointless. The next step is to give a height to our anchor tags. The anchor tags determine which part is going to be clickable which we are giving a height of 38px. If we want all of those 38px to be clickable we have to use display: block; .

Lets take a look at the whole css so far:

Code:


/*******Css from previous part******/

#nav {
      background: url(http://i54.tinypic.com/2n735mp.png);
      height: 38px;
      width: 876px;
      margin: 0 auto;
}

#nav span {
    display: none;
}

/*******Thats the css we used in this part****/
#nav li {
   list-style-type: none;
   float: left;
}

#nav a {
   height: 38px;
   display: block;
}


4) Lets start measuring

Thats the trickiest part.We have to measure exactly each of the "buttons" at our navbar are.We now have to go to an image editor like gimp or Photoshop.Open the ruler tool and drag it the dark middle separator between the second button.You can also Zoom so that you be more accurate.Keep repeating this process for all buttons of the navbar.Our image's width is 876 px so,if you add all of the buttons width they must make up 876 px.If not,go back and start measuring again.
Now, we have to get our selectors ready for each button.Here is the code for the apple clone navbar.

Code:

#list1 { width: 112px; }
#list2 { width: 120px; }
#list3 { width: 128px; }
#list4 { width: 125px; }
#list5 { width: 137px; }
#list6 { width: 131px; }
#list7 { width: 123px; }

Now that we have the widths set we need to position the rollover on hover.
We are going to use background positioning. So we choose our first selector which is #list1, and we set the background image as the image i gave you above. When using background positioning you have two choices.The X-axis(horizontal) and the Y-Axis(vertical). Since are rollover image is underneath our active state we need to shift the image up the Y-axis.The hover image has the same height with the standard and its just under the standard nav,so we are going to use a height of 38 px, but we need to use a negative 38 because the negative makes it go up the Y-axis where as a positive 38 would shift it down.
Heres the code:

Code:
#list1 a:hover {
      background: url(http://i54.tinypic.com/2n735mp.png) 0px -38px no-repeat;
}


Now lets work at the other buttons of the hover navbar:

We need to shift it over the amount of the previous list items width (Hopefully that makes sense). So we go back and look at the width of the first list item, which we see is 112px so we add that as a negative number to our x-axis on #list2.

Code:

#list2 a:hover {
      background: url(http://i54.tinypic.com/2n735mp.png) -112px -38px no-repeat;
}

The same process goes on and on,here is how the code will be like:

Code:

#list1 a:hover {
      background: url(http://i54.tinypic.com/2n735mp.png) 0px -38px no-repeat;
}

#list2 a:hover {
      background: url(http://i54.tinypic.com/2n735mp.png) -112px -38px no-repeat;
}

#list3 a:hover {
      background: url(http://i54.tinypic.com/2n735mp.png) -232px -38px no-repeat;
}

#list4 a:hover {
      background: url(http://i54.tinypic.com/2n735mp.png) -360px -38px no-repeat;
}

#list5 a:hover {
      background: url(http://i54.tinypic.com/2n735mp.png) -485px -38px no-repeat;
}

#list6 a:hover {
      background: url(http://i54.tinypic.com/2n735mp.png) -622px -38px no-repeat;
}

#list7 a:hover {
      background: url(http://i54.tinypic.com/2n735mp.png) -753px -38px no-repeat;
}

We are done! Very Happy
All you have to do now is upload the whole css to your stylesheet,and place the html to your body tags Smile .

To sum up:
css Sprites may look tricky,however they have huge benefits:
->Less HTTP Requests
->Less time to load the page
->All your images into a single big image
->Less Bandwith
->No waiting time to load images when hovering

This is how css sprites are working.If you have a basic understanding of css ,you can create your own easily Wink

And as always....

Live Demo


Notice : This tutorial is copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators.

2Accespted Re: CSS Sprites (Part 2/2) Sat Dec 11, 2010 10:12 am

ankillien

ankillien
Administrator
Administrator
Well explained Very Happy

Tutorial Accepted

3Accespted Re: CSS Sprites (Part 2/2) Sat Dec 11, 2010 10:35 am

verrell123

verrell123
Registered Member
Registered Member
Cool nick!

http://www.freshartz.co.cc/

4Accespted Re: CSS Sprites (Part 2/2) Sat Dec 11, 2010 4:52 pm

Guest


Guest
Thanks everyone Smile

If you have any questions you are free to ask Wink

5Accespted Re: CSS Sprites (Part 2/2) Sun Dec 19, 2010 2:33 am

Terry Harvey

Terry Harvey
Registered Member
Registered Member
Is there a way to do it without double images?

6Accespted Re: CSS Sprites (Part 2/2) Sun Dec 19, 2010 2:39 am

Guest


Guest
you can do anything with css sprites Wink

take a look at facebook Wink
Spoiler:

you just have to measure anything correctly with PS or Gimp and then code it with CSS Positioning Wink

7Accespted Re: CSS Sprites (Part 2/2) Sun Dec 19, 2010 12:31 pm

ankillien

ankillien
Administrator
Administrator
McStormify wrote:Is there a way to do it without double images?

Double images? You mean an image containing more that one images in it?

It is not possible without it. Using such an image is the main thing about CSS sprites. It makes the page load faster since it makes lesser http requests.

8Accespted Re: CSS Sprites (Part 2/2) Sun Dec 19, 2010 1:50 pm

COR3 POW3R


Registered Member
Registered Member
How could you apply this to a forum navigation bar ?

9Accespted Re: CSS Sprites (Part 2/2) Sun Dec 19, 2010 2:27 pm

ankillien

ankillien
Administrator
Administrator
The tutorial explains it all. All you should know is the CSS selectors.

Here is a basic tutorial about selectors : http://www.webartzforum.com/tutorials-f7/css-selectors-t1078.htm

You can google for more info Smile

10Accespted Re: CSS Sprites (Part 2/2) Tue Dec 21, 2010 10:37 pm

RockerMan

RockerMan
Technician
Technician
Here's an example of how powerful sprites can be, this is made in photoshop the I pushed all the buttons together and applied some very basic CSS and HTML Smile

http://graphics-post.com/sprite/

http://www.graphics-post.com/

11Accespted Re: CSS Sprites (Part 2/2) Wed Dec 22, 2010 2:52 am

Guest


Guest
RockerMan wrote:Here's an example of how powerful sprites can be, this is made in photoshop the I pushed all the buttons together and applied some very basic CSS and HTML Smile

http://graphics-post.com/sprite/

onother great example Very Happy

will add this tot he first post Very Happy

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum