Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
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.
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.
There are currently no FAQs about CSS: Page Layouts.
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.