Start learning with our library of video tutorials taught by experts. Get started

CSS: Page Layouts

Working with CSS sprites


From:

CSS: Page Layouts

with James Williamson

Video: Working with CSS sprites

Resource management is becoming increasingly more important in web design, especially when designing for mobile devices. One of the ways that we can improve the mobile performance of our sites is to make fewer HTTP requests. CSS sprites, which is one way of having multiple icons or images in a single file, is one of the ways that we can do this. So I'm previewing the page that we are going to be working on. I am previewing the finished version of that page in a browser right now. We have got a menu right over here with these different icons in it. And I'm guessing that unless you had seen CSS sprites before or worked with them before, you would probably think that those are four separate icons that you're looking at, but they are not.
Expand all | Collapse all
  1. 4m 20s
    1. Welcome
      54s
    2. How to use the exercise files
      3m 26s
  2. 1h 39m
    1. Box model review
      8m 47s
    2. Calculating element dimensions
      11m 11s
    3. Understanding margin collapse
      7m 59s
    4. Calculating em values
      7m 41s
    5. Calculating percentage values
      7m 51s
    6. Normal document flow
      13m 3s
    7. Controlling element display
      8m 53s
    8. Using CSS Resets
      7m 11s
    9. Fixed, fluid, and responsive layouts
      9m 9s
    10. CSS debugging tools
      6m 46s
    11. Using the Firebug Inspector and the WebKit Web Inspector
      11m 5s
  3. 53m 15s
    1. Page design workflow
      3m 6s
    2. Page design tools
      4m 56s
    3. Determining page structure
      7m 18s
    4. Creating image assets
      8m 58s
    5. Creating initial page structure
      7m 3s
    6. Adding meaning with classes and IDs
      5m 23s
    7. Structuring content with HTML5
      6m 6s
    8. Building internal structure
      10m 25s
  4. 1h 36m
    1. Floating elements
      7m 50s
    2. Clearing floats
      7m 28s
    3. Containing floats
      7m 50s
    4. Clearfix technique
      10m 38s
    5. Floating inline elements
      14m 34s
    6. Two-column floated layouts
      8m 17s
    7. Three-column floated layouts
      11m 30s
    8. Column height considerations
      7m 3s
    9. Creating equal-height columns
      10m 42s
    10. Floats: Lab
      5m 25s
    11. Floats: Solution
      5m 21s
  5. 51m 42s
    1. Relative positioning
      7m 59s
    2. Absolute positioning
      8m 59s
    3. Fixed positioning
      4m 23s
    4. Controlling stacking order
      8m 31s
    5. Clipping content
      8m 21s
    6. Controlling content overflow
      5m 38s
    7. Positioning elements: Lab
      3m 59s
    8. Positioning elements: Solution
      3m 52s
  6. 48m 46s
    1. Design considerations for fixed layouts
      3m 28s
    2. Establishing the layout grid
      7m 57s
    3. Defining column spacing
      9m 30s
    4. Applying the grid through CSS
      8m 56s
    5. Creating grid-based assets
      8m 26s
    6. Grid design resources
      6m 22s
    7. Building fixed layouts: Lab
      4m 7s
  7. 44m 35s
    1. Designing for flexible layouts
      2m 30s
    2. Calculating percentage values
      8m 45s
    3. Setting flexible width values
      6m 6s
    4. Making images flexible
      8m 10s
    5. Setting minimum and maximum widths
      7m 24s
    6. Building flexible layouts: Lab
      4m 53s
    7. Building flexible layouts: Solution
      6m 47s
  8. 49m 36s
    1. Responsive layout overview
      3m 49s
    2. Using media queries
      7m 16s
    3. Organizing styles
      8m 39s
    4. Making content responsive
      8m 33s
    5. Mobile design considerations
      7m 32s
    6. Building responsive layouts: Lab
      4m 23s
    7. Building responsive layouts: Solution
      9m 24s
  9. 1h 22m
    1. Creating multi-column text
      6m 36s
    2. Using borders to enhance design
      13m 59s
    3. Rounding corners
      6m 56s
    4. Adding drop shadows
      10m 35s
    5. Working with opacity
      6m 8s
    6. Utilizing the background property
      15m 5s
    7. Working with CSS sprites
      7m 58s
    8. Enhancing page design: Lab
      6m 22s
    9. Enhancing page design: Solution
      8m 38s
  10. 6m 25s
    1. Additional resources
      6m 25s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
CSS: Page Layouts
8h 57m Beginner Feb 07, 2012

Viewers: in countries Watching now:

CSS: Page Layouts introduces basic layout concepts, gives advice on how to create properly structured HTML based on prototypes and mockups, and goes into critical page layout skills such as floats and positioning. Author James Williamson shows how to combine these techniques to create fixed, fluid, and responsive layouts. Designers are also shown how to enhance their pages through the creative use of CSS techniques like multi-column text, opacity, and the background property. Exercise files are included with this course.

Topics include:
  • Reviewing the box model
  • Calculating em and percentage values
  • Controlling how elements display
  • Creating fixed, fluid, and responsive layouts
  • Structuring content with HTML5
  • Floating elements
  • Using relative, absolute, or fixed positioning
  • Defining column spacing
  • Creating grid-based assets and layouts
  • Considering mobile-design-specific issues
  • Working with multi-column text
  • Enhancing page design CSS Sprites
Subjects:
Web Web Design
Software:
CSS
Author:
James Williamson

Working with CSS sprites

Resource management is becoming increasingly more important in web design, especially when designing for mobile devices. One of the ways that we can improve the mobile performance of our sites is to make fewer HTTP requests. CSS sprites, which is one way of having multiple icons or images in a single file, is one of the ways that we can do this. So I'm previewing the page that we are going to be working on. I am previewing the finished version of that page in a browser right now. We have got a menu right over here with these different icons in it. And I'm guessing that unless you had seen CSS sprites before or worked with them before, you would probably think that those are four separate icons that you're looking at, but they are not.

They are actually one single file. If I switch over to Photoshop with that file open, you can see that each of these icons is part of a single file. They don't look great right now, partly because I drew them, and partly because I am zoomed up on them, but I zoomed up on them so that you could see in terms of how I was positioning these icons. So if I measure from like the middle point of one to the midpoint of what another, it's right around 100 pixels. So I designed them to be about 100 pixels apart from each other, just to make it a little bit easier for me to position these.

So essentially what you do with CSS sprites is you take this and you position it using background images, and then you change the background position for each separate individual item that you want to display a different icon for. And you can use that for icons. You can do interface elements with this. There are a lot of different things that you can use sprites for. And there are sprite files out there that are huge. This is a very are small one. Now if you are wondering, well, why did you put them closer together? Well, there are two reasons for that actually. One, is that you can do some very easy math now, because they are 100 pixels apart from each other, so it's very easy for me to understand the offsets that I need to do.

And the second thing is this particular set of sprites is used in both a horizontal and a vertical menu, and if you use this, say, in a vertical menu and there's too much spacing between elements, if these icons are close together, you would actually see multiple icons at a time. So that's why I have little bit extra space there too, to keep those elements away from each other when I using them within a menu structure. I am going to go into our file that we are going to be working on for this exercise, and this is the sprites.htm file. You can find this in your exercise files, in the 08_07 directory.

If we scroll down into the code here, we have an unordered list with some links inside of it. Notice they all have class attributes of Gallery, Gear, Contact, and Shop. You can see just above that we have some anti-selectors, and then we have the link selector inside the section, and that's what I am going to start with. So currently right now the only background declaration we have is the color. So what I'm going to do is I am going to add--remember, we can do multiple backgrounds--so I am going to add a sprite to this. So I am going to go to the existing background declaration and right after the color, I am going to add a url, and the image that I am going to add to this is _images/icon_sprites/25.png.

Now the same rules that we were working with with multiple backgrounds before don't necessarily apply here. You notice we don't need a comma because we are not separating different background images, and anytime you have a color declaration within a background, it's always painted at the very bottom of the stacking order, so the images are going to show up on top of this. Now, I don't want this background graphic to repeat, so I am going to do no-repeat. Save that and preview that in my browser. There we go. We are getting our icons showing up, and right now we only have the first icon in there, but one thing at a time.

The other thing I notice is it's not exactly lined up exactly where I want that, and that has more to do with the fact that it's lining up in its default position of the top left-hand corner. Anytime you are going to build a menu structure like this, typically you are going to add padding or line height to make your text sit in the middle of that link, and then you are probably going to put a little padding obviously on left-hand side to keep it away from the icon. So there's always that little bit of adjustment that you have to do. Typically, I'll just do that visually. I will just come in and I will just type in some offsets until I get it where I want it. In this case if I go back into my code, directly after the no-repeat declaration, I am just going to do a quick little offset here.

I am going to 5 pixels for horizontal. That's going to move it over away from the left edge of the link. And then after that I am going to do 7 pixels' worth of vertical offset. So if I save that and go back in and test it, you can see that that just snugs it right into where we want that. I didn't just come up with those numbers magically; obviously as I was developing the exercise, I just kind of messed with those values until I got it where I wanted to. I had an idea, sort of what it was going to be, but you always have to sort of mess with them a little bit, especially depending upon the icons themselves.

You know, you'll probably end up having to do the same. What we want to do is we want to show each of the individual icons for the different links. Galleries is correct, but Gear, Contact, and Shop need to change. So I am going to go back into my code and I am just going to take this offset off. I am just going to cut those values, because you'll notice we have some individual declarations for each of those links. And what I am going to do is instead of declaring a new background every single time, I am just going to do background-position.

So I am going to use that stand-alone property, and I am just going to paste in the offset for gallery, because we know that offset works. Now I am going to copy this property, because I am going to use it over and over again, and for Gear I am just going to paste that in. The horizontal offset is not going to change, but the vertical offset will. Here's what you need to know about moving items up and down. A positive value, a vertical value is going to move you down. It's going to move your image down. You can see it was at the very top of it, but because we did positive 7, it's now moved down a little bit. So if you remember from looking in the Photoshop file, these icons are actually down. So if I took my CSS sprite and I moved it in this direction, we wouldn't see anything.

So in order to show off these icons, I am going to need to move it in the opposite direction, which means I am going to have to go to the negative. Now remember, these icons are about 100 pixels apart from each other. If I come in and if I were just to do a -100-pixel offset, I would see it, but it wouldn't be centered the same way the previous one is. So what I have to do is I basically just have to subtract 7 from each of my offsets in order to get everything to line up precisely where I need it. And typically you are going to have to do a little bit of tweaking like that.

So I am going to go ahead and paste those for each of these, and for every single one of them, I am just going to add 100 to the offset, so that will be -193, and the last offset will be -293. So now if I save this, go back in my browser and refresh, you can see the icons that we wanted to have in place are now in place. It's the same file. It limits the amount of requests we are making. And by just moving the background-position property, again, we are saving a little bit overhead doing that as well. Now there are plenty of people that will change the background-position property based on somebody hovering over something.

So if you want to make the icon change or animator or move based on hovering, you certainly have that capability as well. This example in this sprite file is a fairly simple example, but the sprite themselves can get really, really complex. So if you're interested in this, if you just go out to Google and you do a search for Amazon or Yahoo sprites, those are two that I would recommend you go look at. Go look for Amazon sprites or Yahoo sprites and you can just do the Google image search and you'll find them. And they have just an incredible array of UI elements and icons and all that stuff crammed into one little file.

So they are very impressive, but I would hate to see the math behind positioning all those little icons wherever they need to. Theoretically, really there is no limit to the size that you can make your CSS sprites and how many objects you want to put in it, but it is worth noting that it seems like anything over 5000 pixels and dimensions seems to have a slight blurring problem on Android devices, so that's something that's good to remember if you're targeting mobile devices with your sprites.

There are currently no FAQs about CSS: Page Layouts.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Page Layouts.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked

Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.