Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In our previous movie we saw how effective background images can be in enhancing your visual design. In this movie, we are going to explore you can exert more control of your background images through positioning images within their container elements. This allows us to create images that change based on things like user interaction or as we are going to be doing in this example, using a single image for several different elements on the page. So here, I have the index.htm from the 07_09 folder opened up. I have Live View turned on, and I scrolled down to the last series of Info boxes in the sidebar here and I have Connect with us.
Now if I hover over these, this one is Follow us! That must be Twitter, Google Plus, that isn't like Google Plus. Be our friend that must be Facebook and then this one is supposed to say LinkedIn Let's connect. So for some reason whatever reason we're only seeing one icon there and I need to change that. Well, the technique that we are going to be using here is a technique called CSS Sprites. And what that is basically is, we take a larger image file and we use it as a background graphic for an element that's rather small in nature. These elements are each 45 pixels by 45 pixels.
So we are only seeing a portion of the image each time we look at it. Now the file that we are going to be working with here, I have the original Photoshop file which is a social_sprite. psd and if I open this up in Photoshop, you can kind of see what I'm talking about here. So in Photoshop you can see that this is actually a much larger image file, and it has several icons inside of it, it has Twitter, it is Google Plus, Facebook and LinkedIn. And those icons are stacked one on top of each other, so we have sort of a default view of it and then we have this sort of almost beveled view that we are going to be using for any type of a hover interaction when people hover over it.
So this technique allows us to do a couple things. Number one, it allows us to use a single image and multiple elements on the page and get a different view from it and that's a little bit more effective and efficient. But the second thing and the most noteworthy thing that this does for us, is it saves server requests. So this image is downloaded once, it's used all over the site and it's used for multiple images. So instead of say four requests per page being made for this particular image only one is being made. That makes a tremendous difference, not only desktop but really on mobile devices.
So this is a technique that's gaining a lot of widespread support in the design community, it has being driven in large way by mobile devices. Although the technique itself has been around and in use for quite some time. Alright, so I am going to go back into my file and we need to start modifying some of our CSS. And we are going to be concentrating on the background position property because all we are doing, we are not going to redefine a different image we are going to use the same image each time. We are just going to move it, we are just going to reposition it within those small elements. Okay, so I'm going to go into my CSS Styles and I am going to scroll down and I am going to find these styles right here, connect twitter, google, Facebook and linked.
Now the first thing I need to do however is I need to revisit my Preferences. So I am going to up to Edit and I am going to choose Preferences. This is one of the reasons why you want to make sure you know exactly what your preferences are doing for you. If I go to CSS Styles, if you remember from earlier in the title, we came in and turned on Shorthand Notation for all these properties. Well, the Background properties what we are going to be working with here. And so I am going to turn that off, the reason I am going to turn this off is if switch over to my main.css and I scroll down through my CSS to find these specific rules, I notice in looking at these that they are empty.
I have sort of a parent rule, if you will up here, that is setting the background image itself. Notice, that we are not setting any positioning values, we are just telling it which item to use and then not to repeat. So the positioning information is going to go in these selectors. However, if I had the Background property applying Shorthand Notation, what it would do is it would kind of overwrite the Background Property up there and the original one. So I don't want to do that, all I want to do is pass in background positioning information. So I am going to go back to Design view, and I am going to find these and I am just going to double-click on Twitter to start off with.
Now if I go my Background properties, I notice that I have the option of setting an X position and a Y position. And so what I can do, is I can grab this X position pulldown menu and I can choose from some keywords. So for the X position which is a horizontal position I can choose left, center and right. I am going to choose left and for the Background-position the Y axis I am going to choose top. So we have three options there top, center and bottom. And what that's going to is, it's going to use the top left-hand corner of the background image and it's going to position it with the top left hand corner of the element.
So when I click OK, I see absolutely no change occur. Now the reason I see no change occurs is because that's the default position for background images. So if I want that default position I don't necessarily have to come out and explicitly say left top, it's going to do that every single time. Now I might go to the next one which is google, I am going to double-click on that and when I go to my Background position with Google I need to do something a little bit different. These icons are each 45 pixels wide, meaning I basically just horizontally need to move over by 45 pixels. Well, you can see there isn't really a keyword that allows me to do that.
But one of the nice things about this is I can enter in a value if I want. So what I am going to do is I am going to enter a -45 pixels for Background-position for X. Now, again you may be wondering why negative? Well, it's basically the direction that you move along that axis. If you move to the left, you will move positive values, if you want to move from the corner to the right you use negative values. You may think that well since the Y position doesn't change, I don't need to enter anything there, but if you do that it's only going to give one value for Background-position and then that value is going to apply equally to both of them.
So you do for Background-position, you want to come in and type in a zero. Now I can double-check it to make sure I have give it the right values and I am going to click Apply. I can see that now I'm seeing Google Plus section of the image not the Twitter section, that's awesome. Alright, so very quickly, we are going to do the next two. For Facebook, I am going to use a Background-position -90, again, I am going just 45 pixels moreover. And a Y position of zero and click OK, I see Facebook show up. And for LinkedIn, now I could keep doing some math, but I was an art major and I hate math.
So now when I go into Background-positioning and since this is all the way over against the right edge, I could come in and say Background-position right, Background-position Y top. Oops I am sure, do top instead of center. Now I click OK and it gives me the same value as if I'd come in and given the numeric value for that. So now, we have our images and these are links by the way. So I when I hover over them, you know I do see the hand show up, but people are often used to seeing visual feedback when they hover over something that is clickable. So if you remember from the Photoshop file, we do have sort of overstate if you will for these icons.
And all I have to do is write a selector that when I hover over this changes the Y position. Now a lot of times it's very easy to come over the CSS Styles panel and set Properties, but sometimes it is a little faster to do things by hand. So what I am going to do is, I am just going to go Code view or it's probably a little easier to use Split Screen view. I am going to collapse my panels down the icons, and I want to scroll over a little bit so I can keep an eye on these social media icons that we have got here. And I am just going to scroll down in my code, until I find this sort of positioning that we had set.
You can see there it is for Twitter, there it is for Google. So I am going to highlight that code, notice I am getting both the opening and closing braces. For some reason there is little bit of extra space there too, so you can feel free to get rid of that. I want to copy this, I am just going to paste it just below it. And then I am going to modify the Selector by adding with no spaces, a :hover to it. Basically what this tells a selector is, hey when somebody is hovering over this element, go ahead and do this and this time, I am going to change the background position from left top to left bottom.
So I am going to save that and then I am just going to go through and do each one of these. And remember, what we're doing is we are changing the Y coordinates this time not X coordinates. So the X coordinate is going to stay the same, so for each one of these I just add a hover and it's called Pseudo-class selector. And then I am going to change my background positioning. Now the same way that we use negative values to move to the right you will also use negative values to move down. So in this case I want to -45 for the Y position. And if you're wondering the horizontal axis comes first in these numbers, the vertical axis comes after that.
Alright, I am going to do same thing for Facebook. I am just copy it, paste it, add the hover. And then, I am just going to mirror this to -90 pixels. And then finally we will do our LinkedIn one, and we will copy that selector. So we can see how easy it is to work in code, this is one of the reasons why after while most designers do gravitate over working with code. Just going to do hover and this time instead of saying right top just like we did for Twitter, I am going to say right and bottom.
Alright, if I save this, go back to Design view, make sure, I am looking in Live View, now when I hover over these, it's changing background position. Now you don't always get everything right the first time out, you can see the little Facebook icon is going away. So I know I made some sort of syntaxial error in there and I am betting that as I was typing that there was at least 90% of you that were saying no, no, no oh yeah he did it. So you know what, no one gets everything right a 100% of the time and yup indeed, I can see automatically what's going on with Facebook, it's not cumulative on the Y axis it's only 45 pixels down for all of them.
So that was me letting my designer self get ahead of me. So once again, I am going to click back in Live view and Design view after saving this. And now Facebook works just fine just like the rest of them awesome. So this is a great technique, but you're not limited to those by learning to control not only background images, but their positioning as well, you can become much more creative with how your background images are integrated into your visual design, you can add compelling techniques like CSS based image rollovers like we did here and be much more efficient with how you're serving your images to different devices and screens.
Get unlimited access to all courses for just $25/month.Become a member