Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Most CSS-based layouts are float-based layouts. That is, the majority of the elements in the layout are floated to achieve element positioning. Although floated elements are removed from normal document flow, they still affect the elements that come after them. Something that comes in very handy when building layouts. The float property has two specific values, left and right. A floated elements shifts either to the left or to the right, until its outer edge reaches the edge of its containing block or another floated element.
Elements that come after a floated element act as if it's no longer there and will move up to occupy the now empty space. In some cases this can lead to the floated element actually overlapping other items on the page. Line boxes such as individual lines of text will flow around a floated element. Even though the containing block that the lines are inside of actually flows under the floated element. This is best illustrated by imagining text wrapping around an image. Floats will continue to affect any element that comes after them until the float is cleared.
Now we'll discuss clearing later. For right now let's tackle some of the basics of floating. So here I have the floating.htm file open and this is found in the 10_03 folder. Let's take a quick look at the structure of our page. Inside of our page we have three div tags. One has an ID of box1, the other has an ID of box2, and the last one has an ID of box3. As you can see we already have some styles that are defining the width and the height of the boxes.
And we also have some margins placed on the bottom of them to hold boxes apart from each other. Well, let's go ahead and float our first element. So I am going to go up to my CSS Styles and highlight the selector #box1. That's our first div tag. Go ahead and add a property here and you want to add the float property. And we are just going to float that to the left. And if you grab the pull down menu you can see that you have left, right, none or inherit. So really those two values are what you are going to be choosing between, left and right. I want to go ahead and choose left.
And as soon as I do that something really, really odd happens to our layout. Box1 doesn't seem to move at all, in fact it's just where it was before. Box2 on the other hand seems to have gone missing. Well at least part of it has gone missing and box3 has moved up as well. So what happened here? Well what happened was, box1 was removed from normal document flow. Think of somebody just taking something off the page and picking it up so that it hovers over the page. Box2 and 3, no longer seeing a box above them, moved up to occupy that space.
So why are we still seeing the text for box2? Well we are seeing the text because the containing element went up underneath the parent. But the text itself would wrap around the floated element. And that's what we were talking about earlier when we were talking about what happened to those in-line boxes. Well let's change it to float right and see if it looks any different. So go back to the same selector, choose box1, float that to the right. And yeah that looks a little different. Now box2 and box3 are still moving up to occupy that space.
But because they are no longer touching or interacting with box1 at all, the in-line boxes move up as well and they're not trying to wrap around the element. Now let's make another more drastic change. I am going to take box1 and float it back to the left. Go ahead and highlight the selectors for box2 and box3 and for both of those add a float left property as well. Now what's happening is it looks like we have a single line of elements when in fact we know from the structure that we have three block level elements one after another.
What's happening is that remember that floated elements are going to go all the way over until they reach the edge of the containing element, or until they reach another floated element. So in this case there is plenty of room for box1, box2 and box3 to all fit on the same line. Now this is very similar to some techniques that we do in building menus for example. We will take the menu items and float them to left to form a horizontal menu bar. You could also think of these boxes as being larger, taller elements and we're very close to having a three-column layout.
So you see how flexible floats are when creating different types of layout elements on the page. Now let's change this a little bit more to reflect a layout that you guys might be a little bit more used to seeing. I am going to go up to box1 and I am going to take the float property and I am going to get rid of it. The easiest way to do this is to simply click on the float itself, make sure that's highlighted in blue and then click the trashcan icon in the CSS Styles panel. Now the reason that I cautioned you to make sure this was highlighted in blue is that if you don't, you could delete the whole rule.
And you don't want to do that. You want to make sure you're just deleting that property. Now what happens is box2 and box3 continue to float but box1 is left alone. Floats only affect the elements below them. So with box1 being part of normal document flow and box2 and box3 floating, box2 and box3 are affected by the floats. Box1 could care less. I am going to type box1 and change its width from 100 pixels to 600 pixels. Now I am going to highlight box2. I am going to leave its width at a 100 pixels but I'm also going to give it a margin to the right.
So I am going to add a property here and I am going to say margin-right 25 pixels. Notice how that's pushing one floated element away from the other one. So instead of going all the way to the edge of the floated element, it hits the margin first and it creates sort of a gutter between those two elements. I am going to change its height to 600 pixels. Finally, I am going to go to box3. And here we are going to change its height to 600 pixels as well. And I am going to change its width to 475 pixels. So now this should seem fairly familiar to you.
This is a very basic CSS-based layout. We have a single area up top that would typically be used as a header. Then we have a left column and a right column. The left column could be a sidebar or navigation; the right column would be the majority of your content. This is a very common layout technique. And although there are certainly variations on it, this is very widely used in web sites all across the web. So in this movie, we've discussed the very basics of floating. However this is by no means all you need to know in order to control floats.
In any float-based layout you'll find yourself quite often needing to restore the normal document flow. And that's where clearing comes in. In our next movie we will move a step closer to building simple page layouts by discussing clearing, and how it is often used in the context of page layouts.
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.