Viewers: in countries Watching now:
In this hands-on course, James Williamson demonstrates the concepts that form the foundation of Cascading Style Sheets (CSS), including styling text, adding margins and padding, and controlling how images display. The course also explores the tools needed to work with CSS, the differences between embedded and external styles, how to use selectors to target elements, and what to do when styles conflict.
Now that we've learned how to display an image as part of an element's background, we need to learn how to control the positioning of that image within the background region. CSS gives us a couple of options for controlling positioning some of which can be a little tricky if you don't understand exactly how they work. So if you are going to do this file with me, go ahead and open up background-position.htm, which can be found on the 05_10 directory, and I just want to go over the structure of this, because it's changed just a tad bit. We have one empty div tag with the class of position, and then we have two empty div tags, one with the class of parent, and then nested inside of that is a div class with the class of percentage.
So we are going to get to these little two a little later on, right now I just want to focus on our initial div tag here with the class of position. So I am going to go up to our position selector right up here and I am going to add a background-image for it. I am going to go ahead and do background -image, and we are going to do URL, so we are going to use the same image we used earlier _images/tweet_90.png, if you are doing this exercise with me, be really careful about the spelling and punctuation and the notation of everything.
If you start testing these and you are not seeing the image show up or things don't look the way that they should, go back and look at the syntax very carefully and make sure you spelled everything correctly, make sure that you do things like _ correctly things like that because those little things really do matter. Okay, I am going to go under the next line and type and type in background-repeat and then no-repeat. So far nothing new, this is exactly what we did in the last lesson, so we are going to introduce a new property here and that is background-position.
This has several different options in terms of the values that we can pass into it. I want to start with the keywords. So I could, if I wanted to, I can say, (left top;) When I am using the keywords, typically you will use two of them, although I am going to show you I have the views one in just a moment. But you separate them with some white space. So left top, means take the top left corner of the image and line it up with the top left-hand corner of the container. So let's Save that and let's preview that in one of our browsers. Sure enough there is our Twitter bird and he is showing up right there in the top left-hand corner of our container.
What if we used other keyword? So, for example, you can use for horizontal, left center and test it, or right. For vertical, you can use top- center or bottom, and you can mix them anywhere that you want. One habit that I really want you to get in the habit of doing is placing the horizontal value first and the vertical value second.
To be honest with you, it doesn't really matter if I said (bottom left;) the browser would know what I was talking about, and it would apply right. But when we get into using units of measurement here, the horizontal value goes first, the vertical value goes second, and if you get in a habit of doing that even with keywords, you're going to be a lot better off when you try to use a unit of measurement. Okay, so what does happen if you only pass one value in? For example, left in, now I tested that. Notice that center is always assumed.
So if I pass in say top, it's going to do top center. So unless you pass in two values, the one value horizontal or vertical that you don't pass in, is always going assume to be center. Now what if we wanted to use units of measurement? So what if I did something like 50 pixels by 50 pixels? If I Save this and test it, I can see it's going 50 pixels over and 50 pixels down. If I were to use values of 0 and 0 and Save that, I notice that that positions it at the top left-hand corner.
So if you want a little bit more control, you can use these units of measurement. You can also use things like ems. I can do 1em over and 3ems down. And if I save that and tested it, you can see it moves 1em over and 3ems down. So that's really up to you in terms of the unit of measurement that you want to use. What happens if you only use 1 unit of measurement? So if I do 25 pixels, for example, notice what happens. It assumes that the value is the horizontal value and then defaults to center for the vertical. So even if I did something like 100 pixels, and saved it, still going to be vertically centered right there.
Speaking of centering it, if you ever do want to place it right in the middle of the element, you can just pass the keyword center. The other vertical is assumed and it's just going to center it right there in the middle of your element. There is another property that we have that can change the positioning value as well, and that is background-attachment. Now background-attachment allows us to have basically two values, fixed or scroll. Scroll is the default, which just basically means that it wants you to position the background image relative to its container, the element that you applied it to.
Fixed on the other hand does something quite different. So I am going to type in fixed. When you use background attachment fixed, you are saying, I want you to fix the background image relative to the viewport, not the containing element. What does that mean? Let's check it out. So I am going to Save this and then Refresh. It looks like it's just merely shifted my element down, but it hasn't. Watch what happens when I resize the browser. That element is staying basically in the very center of the viewport, the very center of the browser window, and if the browser window is really big, I probably would not be able to see it anymore, because it would go outside of my element.
And then as I scroll watch what happens. So that's where the fixed comes in. It keeps it fixed relative to the viewport window right in the center, but only when it's within the containing element can you see it. You can do some really, really creative effects with this. It's not fully supported in some of the older browsers, most notably, Internet Explorer, but it's a neat little feature that you might want to play around with and experiment with, because you can do some really creative stuff with it. Now I used almost every single unit of measurement I could use right here, except for percentages, and the reason that I wanted to wait and talk about percentages all sort of all by themselves, is because they're so hard to wrap your heads around when you are first trying to learn how to use percentages to position a background image.
So if you're going to use background- position with percentages, what happens is, now let's say we passed this first value 25% and 50%. What it does is it finds that point on the image. It goes 25% over on the image, 50% down. It then goes to the element that you are applying it to, it finds the same corresponding point, 25% over, 50% down and that's where it positions the image. If you want to center an image using percentages, you're probably going to pass 50% and 50%. So what it does is it finds that 50% 50% or center of the image and positions that along with the element itself at 50% and 50%.
So this is sort of a necessary way of working. Hopefully this slide has illustrated this a little bit for you, and if it hasn't, we are going to go back into the exercise and we are going to do this in hands on way, so that you can really kind of see this in action. So I am going to go back into my code, and I am really focusing on these little guys right now. So parent and percentage, so what I am going to do in the parent is I am going place a large grid back there, and then for percentage we are going to have a smaller grid that when we move it around within parent, you can kind of see how that positioning is actually working and happening.
So for parent, I am going to come in and I am going to type in background-image: url and here I am going to do _image/300grid.png; don't forget my semicolon there. I am then going to go down and do background-repeat: no repeat. Then I'm going to go down to the percentage one and the first thing I am going to do is get rid of the background color. I am just going to change that to background image as well.
So we want to be able to see through this one, I am just going to come in here and say, url. It's going be _images/100grid. So we are using it with smaller grid there and then on the next line after that, we are going to do another background-repeat; no-repeat; Save that and I am going to test this in my browser. You can kind of see these grids kind of overlaying each other now. These grids have 25% quadrants to them, so 25, 50, 75 or 100, same things down that way.
Okay, so let's do some background positioning and see how this works. So I am going to go down to percentage, and I am going to do background-position: and I am just going to do 25% space 50%. I will Save that, and then I will test it, and you can see what happened here, it went horizontally over 25% on the smaller one, 25% on the bigger one. They found that 50% spot on the larger one vertically and they found that 50% spot on the smaller one and it lined that position up.
Now if I come in, for example, and say, 50 and 50 and Save that, it's going to move this point right to the middle point and I want to reverse the page, you can see it do just that. So go ahead and play around with those values. Try some non-traditional values, something like 13% and 85%, for example, when you do that, it's going to find that spot on your smaller one and it's going to line it up with exact same spot on your larger one. So it definitely takes some time to experiment with those values. I think you will find that the more you use percentages to position your background image, the more that concept will really start to make sense to you.
In order to achieve the desired results that you're going for however, you are going to need to really be able to understand how the positioning keywords work, how horizontal and vertical positioning values are calculated, how percentages work when relating background images where there are containing elements, things like that. Once you have a thorough understanding of those concepts, you'll be able to combine element content in their image backgrounds in any manner that you wish.
There are currently no FAQs about CSS: Core Concepts.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.