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.
As important as the float property is to creating layouts, it would be practically worthless without the use of the clear property. The clear property allows us to turn floats off and restore normal document flow and together, floating and clearing allows us to exercise precise control over the flow of our layouts. To demonstrate that, I have the clearing.htm file open, and you can find that in 03_02 directory. Now, structurally it's very similar to the file we were using in our last exercise. As a matter of fact, if I scroll down, I can see that the only big difference is that instead of only having three elements, we now have six.
For styling purposes, you'll notice we've got a lot going on just in the regular div rather than installed individually. All of the divs are being floated to the left, and they all have a little bit of margin to the right to give us a little bit of spacing. Okay. So I'm going to save this, and I want to preview this in my browser. Currently, you can see all the elements are floated to the left. Now, they would all appear on one line except for the fact that we have a constraint going on right here with the page. So if the page were a little bit wider, they would all show up on the same line, but because it's constrained at 600 pixels, when we run out of room, the next floated element breaks down to the line and floats resume.
So how does clearing work? Well, let's say that we wanted to have two rows of equal items, so we wanted two rows of three, right? We have a couple of values available to us here when we do clearing: we have left, right, both, inherit, and none. Now, none is turning clearing off. Inherit basically says whatever my parent has, that's what I want, so it's these three that really matter for what we're doing in this exercise. So clear: left, essentially it tells an element nothing can float to my left; clear: right says, all right, nothing is allowed to float to my right; clear: both says nothing is allowed to float to my right or my left.
So if something is cleared, it essentially breaks down to the next line and reestablishes normal document flow. So currently, for example, number two, this two element right here, has one floating to its left, three has two floating to its left, so forth and so on. So if I go back into my code and I go right down here to element number four and I just say, okay, let's go ahead and clear that element to the left. So if I save this, go back in my browser and preview, that is exactly what happens. Four breaks down on the next line and reestablishes normal document flow.
Now, just because normal document flow is reestablished doesn't mean that you can't then at that very moment go ahead and resume floating. We're certainly doing that here. So four is still floated to the left, five and six are still floated to the left, but for just that moment we're clearing those floats and reestablishing that normal document flow on another line. Well, that works great. Right works the same way. I'm sure you can envision and imagine how that works. But what about both? Shouldn't we know whether an element is going to going to float to the right or left; why would we need both? To demonstrate that, I'm going to go back into our code and just modify it slightly.
I'm going to remove the clear that we just applied to 4. Then I'm going to go up to my just default divs. I'm going to remove float to the left, so go ahead and get rid of that. I'm going to remove the margin-right, and then I'm going to change the width to 150 pixels. So now all of my elements are just stacked, normal document flow, they're 150 pixels wide, and we no longer have any right margin on them. Okay. Now, let me go down to the first two elements, and we're going to modify those slightly as well. We're going to take element number one and I'm going to float that to the right.
I'm also going to set its width to 50 pixels. For element number two, I'm going to float that to the left. So we've got one element floated to the right, we've got another element floated to the left, and I'm going to give it a width of 50 pixels as well. Okay. Now, if I save that and go in and refresh this, it looks weird, but bear with me here. So element number 1 is floating all the way over to the right-hand side. element2 is floating over to the left-hand side. 3, 4, 5, and 6 are in normal document flow. Remember, when I float 1 and 2, I removed them from normal document flow, so the remaining elements move up to occupy that space.
So essentially, two is just sort of overlapping three, and that's one of the reasons why we made those other elements wider, so we could sort of see that overlap. So if I go back into my code--and element3 is who we want to focus on right now. If I tell element3 clear to the right, and check that, now what happens is it says, okay, number one, you can't float to my right, so it's going to go down onto the next line, it's going to reestablish normal document flow, and the page layout just sort of continues. But what if I said instead of clear: right, clear: left, what would happen then? So if I save this and preview it, no change. Okay.
So in this case clear: right and clear: left do exactly the same thing, because three basically had one floating to its right and two floating to its left, so really they're both doing the same thing, so why in the world would I need clear: both? Right now we're working with a very static layout in terms of, we know exactly the element dimensions of both of these, but it isn't always that simple. Now, one thing I want to point out here is, do you notice how we've kind of messed with source order a little bit? The first element that any sort of user agent would encounter in the code would be element number one, but visually we're almost making two look like it comes before one, because we typically read from left to right.
So let me go back into the code. Let's say we go up to element one and we change its height to 200 pixels. So I'm going to go ahead and save that and preview it. And what happens is is now element one, because it is 200 pixels tall, can float to the right of all these elements. These elements are simply going to move up and underneath floated elements, but since we have clear: left applied to three, it's breaking down here and reestablishing normal document flow, but we're not preventing elements from floating to its right, in this case, the number one.
If I went back into my code and I told element three, no, no, no, no, clear to the right please, and save that and test it, okay, now it's clearing to the right, it's reestablishing normal document flow, and it's not letting either of those float left or right of it. But again, this all has to do about the height of the elements. Again, if I come back into element two and I change its height so that it's 250 pixels, save that and test it, and you can see, we get overlap again. So to prevent overlap, when you really don't know, you're not really sure if elements are going to be floated to its left, you're not really sure if elements are going to be floated to the right, you're not really sure in terms of the height of the various elements, you can always come in and say instead clear: both.
And now it doesn't matter who is taller. It's simply going to clear both of them. So right now two is taller than one, so it's clearing that and reestablishing normal document flow. But if I take element two, take its height back to, say, 100 pixels, you can see it's still not going to go above where one is either. That's clearing. It gives us a precise amount of control over which elements we allow to float and when we want to stop floating and resume normal document flow. Clearing also allows us to deal with another very tricky problem related to floats, and that's how containing elements will collapse when child elements are floated.
We're going to take a closer look at that in the next movie.
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.