Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There will be times when creating float- based layouts that you want the floated elements to no longer affect the elements that follow them. In our own layout for example, the mainContent region and the sidebar are using floats to achieve a two-column layout. We don't want or even need the footer to be overlapped by our floated elements. Towards that end we will apply a clear to our layout. Clearing floats is an essential technique for achieving reliable float-based layouts. It's your way of saying, "that's it." "Those floated elements shouldn't float any element past this point." The clear CSS property has three possible values, left, right and both.
By clearing to the left or to the right, you're essentially saying that nothing can float to the left or right of the cleared element. A clear value of both says that no element may float the cleared element, whether it's to the right or to the left. Once floats are cleared, normal document flow continues. And cleared elements will continue to push down in response to the elements above them, even floated elements. In this sample layout we are going to clear the footer element, which will move it down below our floated elements into the desired location.
So let's take a moment to review our page structure. And here I have the clearing.htm file open. So we have a wrapper div tag, which wraps all the other content on the page. We are using auto margins, so that the wrapper is centered on the page regardless of the size. Now inside the wrapper div tag, we have a div with an ID of header. It's followed with a div with an ID of mainContent. Further down the page we have a div with an ID of sidebar. And then finally we have a div with an ID of footer.
Now as you can tell we have some very basic CSS properties set here. We have some height set, we have background colors, we have some text colors, but other than that we really aren't doing anything with the layout. So I am going to scroll back up to the top and we are going to start our layout by floating the mainContent. Now in this case we want the mainContent to be on the right-hand side and we want the sidebar to be on the left-hand side. Now there are a couple of different ways that we can do this. As a matter of fact if you look at two float-based layouts that are exactly the same, chances are their CSS might be significantly different.
So there are a lot of different techniques for doing what we are about to do here. I am going to select the #mainContent selector over here in the CSS Styles panel and I want to add a property to that. I am going to go ahead and the float property and I am going to float that to the right. That is going to float the mainContent over to the right-hand side. And you can see the sidebar moves up to occupy the space that the mainContent used to be in. Now one thing that you're really not seeing here is that if the mainContent's background color was transparent, you would actually see that sort of light periwinkle, I guess, sidebar color go up underneath the mainContent.
So there is actually overlap going on here now. Now one thing I neglected to mention earlier is that our wrapper div tag is 800 pixels wide. So now when we put our mainContent on the right-hand side and our sidebar on the left-hand side, one of the things that we want to do is set a width for those that is going to calculate out to that 800 pixels wide of our wrapper. That way we know exactly how much area we have in the mainContent and the sidebar for any content that we may place in there later.
So I am going to go back to our mainContent and I am going to add a property to this and I am going to add the width property. And in this case I'm going to make it 500 pixels wide. So that extends the mainContent out a little bit. And now we see the sidebar on the left side, the mainContent on the right-hand side. Now I like to point out one more thing too. If you're going is floating for layouts, whenever you float an element a best practice is to establish a width for the element as well. That is going to prevent a number of problems in certain browsers.
All right next I want to go down to my sidebar. I am going to select my sidebar and I am going to click to add a property here. I am going to add the float property. And this time instead of floating to the right, I am going to float the sidebar to the left. It moves it over to left-hand side of the page. And now the sidebar is on the left all the way up against the edge of its parent element, which in this case is the wrapper. The mainContent is floated to the right all the way up against its parent element edge, which in this case is the wrapper. Now the sidebar is a lot narrower than mainContent.
But we still need to increase its width a little bit just so we don't have this big gap in the middle, as we have here. Now you might be wondering, hey when is he going to say something about that footer? Yeah, the footer does not look right at all. Let's finish off our sidebar and we will talk about what is going on with that footer. So I am going to add a property here and I want to go ahead and add a width value and I am going to give it 280 pixels worth of width. Those values were arrived at in thinking about what I wanted for this particular layout. We have 800 pixels worth of space from side to side here.
By giving 500 pixels here and 280 pixels here we are left with a total of 20 pixels. That 20 pixels becomes the gutter between the sidebar and the mainContent region. So in this case there is no need for us to worry about padding or margins to try to keep those two elements away from each other. The width is going to do it for us. Now back to that poor footer. Okay, so now it's even worse because the footer doesn't have any room to show up. When we scroll down the text of the footer is actually down here. You can't see it because it's white and it is on the white background of the wrapper div tag.
However, the sort of brown background color of the footer is readily apparent. So what happens? Well the footer is going to move up because two elements above it are floated. So it moves up to occupy the space that those elements used to occupy in the normal document flow. So our normal document flow now just says header and footer. It doesn't say header mainContent sidebar footer because mainContent and sidebar are floated and thus removed from the normal document flow. So in order to get our footer to locate in the right place, that would be at the very bottom of the layout down here, we need to re-establish that normal document flow.
And the way to do that is by using clearing. So I am going to go to my footer selector over here in CSS Styles. I am going to add a property to that and I am going to add a clear property. Now if I grab the pull down menu for clear, I can see that I have left, right and both. Now the mainContent is floated to the right and the sidebar is floated to the left. So I really can't choose one over the other. I need to go ahead and in this case choose both. So I am going to choose both and he,y look what happens.
Our footer reappears at the bottom of the page because now we have re-established normal document flow. The footer is no longer allowing any element to float to its left. And it's no longer allowing any element of float to its right. Because of that normal document flow is re-established. So essentially what happens is the header is in normal document flow. mainContent and sidebar temporarily interrupt that as one floats to the right and one floats to the left. But then the footer re-establishes normal document flow by clearing the floats and saying no, I'm not going to be affected by either of those two guys above me.
Now the really nice thing about this particular technique is that if the height of the mainContent region or the height of the sidebar increases, the footer is going to continue to push down the page, because it will never allow one of the two of them to float. So for example, if I go to mainContent and I make its height say 600 pixels, and if we scroll down we can see that the mainContent is pushing the footer down. This is exactly what we need for a two- column layout that has a header and a footer. So by clearing floats we can re- establish normal document flow, contain floats, and help our layout take shape.
If you become familiar with these techniques, you'll understand how the overwhelming majority of CSS-based layouts are created. Now keep in mind this is a very simple, very basic layout, but also an amazingly common one. A header, two columns of content, and a footer has become a standard for many types of web pages. Using floats and clearing them when necessary is a quick and easy way to achieve most of your layout requirements. Of course we've just scratched the surface of what you need to know when floating elements.
For more information on floats and clearing and containing those floats please consult the other CSS titles in the lynda.com Online Training Library. I will cover floats and controlling them in much more detail in my Dreamweaver CSS Essential titles. Now that we've discussed floats, we are going to discuss using the CSS position property to tweak our layouts. And increase the control we have over individual elements within our pages.
Get unlimited access to all courses for just $25/month.Become a member
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.
Your file was successfully uploaded.