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

CSS: Core Concepts

Controlling image positioning


From:

CSS: Core Concepts

with James Williamson

Video: Controlling image positioning

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.
Expand all | Collapse all
  1. 4m 57s
    1. Welcome
      55s
    2. Using the exercise files
      4m 2s
  2. 1h 7m
    1. Exploring default styling
      4m 56s
    2. CSS authoring tools
      2m 29s
    3. CSS syntax
      4m 45s
    4. Writing a selector
      4m 10s
    5. Setting properties
      8m 40s
    6. Common units of measurement
      7m 47s
    7. Inline styles
      5m 1s
    8. Embedded styles
      5m 19s
    9. Using external style sheets
      10m 34s
    10. Checking for browser support
      8m 48s
    11. Dealing with browser inconsistencies
      5m 30s
  3. 2h 15m
    1. Structuring HTML correctly
      2m 51s
    2. Element selectors
      4m 52s
    3. Class selectors
      6m 4s
    4. ID selectors
      3m 27s
    5. Using classes and IDs
      10m 7s
    6. Element-specific selectors
      4m 35s
    7. The universal selector
      5m 42s
    8. Grouping selectors
      4m 49s
    9. Descendent selectors
      7m 32s
    10. Child selectors
      5m 7s
    11. Adjacent selectors
      5m 30s
    12. Attribute selectors
      12m 43s
    13. Pseudo-class selectors
      3m 54s
    14. Dynamic pseudo-class selectors
      8m 29s
    15. Structural pseudo-class selectors
      6m 45s
    16. Nth-child selectors
      13m 10s
    17. Pseudo-element selectors
      12m 40s
    18. Targeting page content: Lab
      8m 56s
    19. Targeting page content: Solution
      7m 59s
  4. 42m 39s
    1. What happens when styles conflict?
      4m 0s
    2. Understanding the cascade
      5m 47s
    3. Using inheritance
      6m 11s
    4. Selector specificity
      6m 55s
    5. The !important declaration
      4m 5s
    6. Reducing conflicts through planning
      3m 33s
    7. Resolving conflicts: Lab
      6m 45s
    8. Resolving conflicts: Solution
      5m 23s
  5. 1h 47m
    1. Setting a font family
      7m 10s
    2. Using @font-face
      9m 18s
    3. Setting font size
      7m 35s
    4. Font style and font weight
      6m 52s
    5. Transforming text
      3m 58s
    6. Using text variants
      2m 49s
    7. Text decoration options
      4m 26s
    8. Setting text color
      3m 2s
    9. Writing font shorthand notation
      8m 49s
    10. Controlling text alignment
      6m 33s
    11. Letter and word spacing
      9m 11s
    12. Indenting text
      4m 30s
    13. Adjusting paragraph line height
      10m 30s
    14. Controlling the space between elements
      6m 41s
    15. Basic text formatting: Lab
      8m 45s
    16. Basic text formatting: Solution
      7m 14s
  6. 2h 1m
    1. Understanding the box model
      16m 53s
    2. Controlling element spacing
      14m 29s
    3. Controlling interior spacing
      10m 49s
    4. Margin and padding shorthand notation
      6m 27s
    5. Adding borders
      8m 57s
    6. Defining element size
      10m 7s
    7. Creating rounded corners
      6m 58s
    8. Background properties
      2m 51s
    9. Using background images
      5m 10s
    10. Controlling image positioning
      10m 25s
    11. Using multiple backgrounds
      7m 5s
    12. Background shorthand notation
      5m 25s
    13. Styling container elements: Lab
      7m 55s
    14. Styling container elements: Solution
      8m 17s
  7. 47m 51s
    1. Color keyword definitions
      5m 4s
    2. Understanding hexadecimal notation
      6m 5s
    3. Using RGB values
      4m 58s
    4. Using HSL values
      5m 17s
    5. Working with opacity
      2m 23s
    6. Using RGBa and HSLa
      3m 8s
    7. Styling drop shadows
      5m 38s
    8. CSS gradients
      6m 32s
    9. Working with color: Lab
      4m 26s
    10. Working with color: Solution
      4m 20s
  8. 1m 58s
    1. Additional resources
      1m 58s

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: Core Concepts
8h 49m Beginner Nov 22, 2011

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.

Topics include:
  • Exploring default styling
  • Writing a selector
  • Setting properties
  • Working with common units of measurement, including ems and pixels
  • Structuring HTML correctly
  • Understanding the cascade and inheritance
  • Setting a font family, font size, text color, and more
  • Understanding the box model
  • Styling container elements
  • Working with RGB vs. HSL values
  • Styling drop shadows
Subject:
Web
Software:
CSS
Author:
James Williamson

Controlling image positioning

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.

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: Core Concepts.

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