Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Daniel Robertson
Daniel Robertson
7,069 Points

Mike's Catalog of Shirts - Can anyone explain which bit of the CSS file relates to the icons on the navigation bar?

From the PHP project Mike's Catalog of Shirts.

The image file nav-sprite.png displays the icons vertically aligned yet on the site they are displayed horizontally. Which bit of CSS code relates to the positioning of these icons?

1 Answer

Jason Montoya
Jason Montoya
6,550 Points

Dan,

Let's see if I can help. First off, let's clarify why he is using a sprite image. In case you don't know, sprites are used to reduce the server requests when loading a page. Instead of having multiple images for each image element on the page, a developer compiles all of the images onto one large master image.

Once the developer has their single master image, they can now use a positioning property to move around the single master image to best suit what is supposed to be displayed in that image element.

Here is a bit of sample code from: http://www.w3schools.com/css/css_image_sprites.asp that might help understanding the concept a little easier.

img_one {
    width: 46px;
    height: 44px;
    background: url(nav-sprite.png) 0 0;
}

Notice how the last thing processed is the position of image? This will show the image from the left and right position of 0, and it will also only show about 46 square pixels. So, if the large master image is say 500px, only a fragment of the image is showing.

In your case of the vertical image being used for a horizontal navigation of images, the position property is being used to only show a small fragment of the larger image.

Hope this helped.

Around line 514 in the supplied css file is where it starts.