New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Relative positioning

From: CSS: Page Layouts

Video: Relative positioning

When people talk about controlling page layout with CSS, often the property that they are actually referencing is the position property. In fact, way back in the day when designers first started crafting layouts without tables, they even referred to the CSS involved as CSS-P, or CSS positioning. Now the positioning property is indeed an important one, and in this chapter, we are going to go over the various properties and values associated with positioning and how we can use those properties to control elements within our layout.

Relative positioning

When people talk about controlling page layout with CSS, often the property that they are actually referencing is the position property. In fact, way back in the day when designers first started crafting layouts without tables, they even referred to the CSS involved as CSS-P, or CSS positioning. Now the positioning property is indeed an important one, and in this chapter, we are going to go over the various properties and values associated with positioning and how we can use those properties to control elements within our layout.

Now we are going to start with what is perhaps the most commonly used positioning value, and that's relative positioning. To demonstrate this, I have the relative. htm file open from 04_01 folder. And in fact, if I scroll down you can see, we have a fairly simple page. I've got a little bit of text referencing the positioning property, and then I have these three elements right here that we are going to focus on. There are three div tags, and they have the class attributes of elements One, Two, and Three. And in fact, if I preview this in my browser, you can see there are elements One, Two, and Three, and they are just stacking one on top of another.

Now that means that these elements are in normal document flow. I have been talking about normal flow for pretty much the whole title, but we are finally in a position where we can discuss the three different positioning schemes of CSS. And we are going to discuss them in the context of the positioning property. Now you can see the positioning property has five possible values, although we can--almost everybody has inherent. So let's focus on the four values here. Static and relative, both of those values are considered to be in normal flow.

In fact, static is kind of the default positioning of all the elements on your page, if you want to look at it that way. Absolute and fixed, on the other hand, are in the second positioning scheme, which is absolute. Now we are going to talk about those in just a moment. The third positioning scheme is floats, and we covered floats in our last chapter. So, of the three position schemes we have, an element is considered to be either a normal flow, floated, or absolutely positioned. So it's considered to be one of those three things.

Elements with positioning values of static or relative are considered to be within normal flow. And that not only controls the behavior of that element, but it also controls the behavior of the elements around it. And to demonstrate that, let's go ahead and experiment with relative positioning. I'm going to go back into my code, and I am going to scroll up until I find the selectors that are controlling elements 1, 2, and 3. We are going to start with element1, and I am just going to come up and I'm going to do position: relative. There we go. So if I save that, and refresh my browser, you can see, ah, nothing happened. Absolutely nothing.

Well, that's not entirely true. Actually, element number one now has positioning, and if you put quotes around that when I said it--I did little air quotes, you can't see that obviously, but, element1 now air quotes has positioning. Now what that means is that element is considered to be positioned, which we will talk more about later on in the chapter as we look into absolute positioning, and also gives us the ability to offset this element now. So once you have an element as positioned, you can offset it from its normal position within the layout.

So if I go back into my code, I am going to go right here into the element1 selector and I am just going to do some offsets here. I will do top of 10 pixels and left of 10 pixels. So what this is going to do for us is it's going to go to the top edge and the left edge of our element. You could actually think of a point on the box here in the top left-hand corner, because that's the point it's going to move it from. An offset from the top pushes it down from the top. An offset from the left pushes it from the left-hand side, which actually moves it to the right.

So if I save this, go back into my browser, and refresh, you can see what happens to our element. It moved over by 10 pixels and then down by 10 pixels, so if offset from this point over 10 and down 10. Now again, you can use all four sides. You can also use right and bottom. Positive values are going to move it away from that edge. Negative values are going to move it towards that edge. So, if instead of 10 pixels to the left, I did -10 pixels to the left--let me save that-- you can see that it moves in the opposite direction.

It moves it to the right. In fact, -10 pixels to the left is exactly the same as a positive 10 pixels to the right, so if I save that and preview it, see the element doesn't move at all. So these offsets allow us to sort of tweak the position of our element based on which sides we are going to move it from. You have to be really careful with this. I mean, what would happen if I told it to go 10 pixels to the right and then 10 pixels to the left at the same time? Not much. And I'm also being very cruel to my browser. So don't do that.

Usually you are just going to use one or two of the offsets, okay. You may have also noticed something pretty curious too. The other elements, elements 2 and 3, didn't move at all. To demonstrate that a little bit more, let's make these offsets a little bigger. So I am going to change the first offset to left, and I am going to give it a left offset of 200 pixels, so now I am really going to offset it. And then I am going to give it a top offset of 100 pixels. So that's going to move it 200 pixels to the right, 100 pixels down. If I save this and refresh it, you can see what it did.

But more importantly, you can see what it didn't do. Elements 2 and 3 are not affected at all, and that's due to the fact that one is considered to still be part of normal flow, so even though it's offset from its normal position, elements 2 and 3 think that 1 is still here. So they don't respond at all. That's not to say that these elements still aren't related to each other and they still won't respond to each other; they will. In fact, if we go back into our code, and we change something about element1, let's say we change element1's height to 200 pixels, so if I save this, and preview it, you can see what happens.

Element1 gets twice as tall and then elements 2 and 3 move down as if element1 were still there. This is a very different behavior than what we saw in the last chapter with floats. Remember, if we floated something to the right, these elements would move up to occupy its space unless we cleared that float. So when people first see this, they are like, "Oh wait a minute. This is cool. I can move elements around and not have all that trouble that I had in the last chapter with floats with stuff moving all over the place." But there is a bigger problem.

We have this empty space here where one would occupy, and in order to move something into space, I have to offset those values too. So if you try to do a layout based purely on relative positioning, you have a lot to keep track of. I have never seen it done. Somebody probably did it just as an exercise, but it would be so difficult to remember where everything was supposed to be normally, to understand how much you are supposed to offset it, it wouldn't be worth the trouble. And then if you change one element then the whole layout flexes and changes as well, because everybody is responding to everybody else, because they are assumed to still be in normal document flow.

So obviously, relative positioning is not going to be used to create an entire layout, so what is it for? Well, overall relative positioning is used mainly to do two things. First, just provide slight tweaks for your layout, when you need to offset an element in a specific direction. So if you just need to move something up a little bit or over a little bit to make sure your layout is doing what you needed to do, relative positioning is great for that. And second, it provides a positioning value for container elements.

So why in the world would you need to do that? Well, that has everything to do with absolute positioning, which is something we are going to explore next.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 40100 viewers

James Williamson
Author

 
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

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

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.