Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Of all the different properties involved in CSS layout, perhaps the most important to learn and master is the float property. Floating allows us to alter the normal position of an element by moving it to the leftmost or rightmost edge of its parent. That may not sound like such a big deal, but floating forms the basis for the overwhelming majority of CSS site layouts. Of course there's a little more to it than just moving elements from side to side, and in this chapter we're going to focus on the ins and outs of floating.
We're going to start by examining the property in action. So I have the float.htm file open from the 03_01 directory. I'm just going to give you a brief preview of the structure of the page. If I scroll down into the body of it, you'll notice that we've got a couple of introductory paragraphs, and then we have three empty div tags with the classes elements 1, 2 and 3, and that is who we're going to control. Now if I preview this in my browser, you can see kind of what's going on here.
We've got normal document flow happening. We have element1, element2, and element3 just stacked right on top to each other, so we're using CSS to apply a background color and to give them a defined width and height, that sort of thing, but normal document flow is just stacking them one on top of each other. Now, when we use the float property, we have four possible values. You can see those right here. We've left, right, inherit, and none. Now left is going to take that object and float it to the far leftmost edge of its parent container. Float right is going to do the same thing in the opposite direction.
It is just going to float it all the way over to the rightmost edge of its parent container. Inherit basically just says, hey, whatever my parents is doing, I want to do that as well. And none, will basically turn a float off. So none says, okay, no floating is occurring on this element. For the most part, left is kind of what happens to objects anyway. Now I say for the most part because the default for an object is that it is actually lined up with the leftmost edge of its parent. However, one of the things that floating does that normal document flow does not is it actually removes the object from normal document flow.
Now this is can have some really interesting side effects, which we're going to see as we sort of experiment with the float property. All right, I'm going to go back into my code and if I scroll right up into my styles, I can see that I already have some selectors written for element1, element2, and element3, and that's who we want to focus on, so I'm going to go first up to element1 and I'll add a new line. And I'm just going to do a float property, so just float, and then I'm going to give it a value of right, so we can see it in action. I'm going to save that, go back to my browser, test it, and you can see what happens.
Element1 floats all the way over here to the rightmost edge of its parent, but notice what happened to element2 and element3. Because element1 is removed from normal document flow, element2 and element3 move up to occupy the space where 1 used to be, because they don't see 1 anymore. 1's removed from normal document flow, so they act as if 1 does not exist; they just move straight up to occupy that now-empty space. Now that can have some very interesting consequences, as we're going to see in just a moment.
All right, so I'm going to go back into my code, and I'm going to change float: right to float: left. So I'm going to save that, go back to my browser. Now, remember, this element1, the brown one is element2, the orange one is element3, and if I update my page, something very curious happens. Now we floated element1 to the left, so it looks like its back where it used to be, so it's all the way up against the left edge of its container element, but it looks like element2 has disappeared. Well, in reality it hasn't disappeared. Remember, floating removes it from normal document flow, right, so element2 and element3 moved up to occupy that space.
It's just that element1 happens to be there as well. So that gives us another little piece of the puzzle here. When we float an element and remove it from normal document flow, it is allowed to overlap other elements, and it's also going to be stacked on top of them initially. So in this case element2 is really just sort of hiding, if you will, just underneath element1. Okay let's go back in our code and let's take a look at what happens when we float multiple elements. I'm also going to float element2 to the left, and I'm going to float elements3 to left.
Now what this is going to do, if I save this and test it, you can see that it basically stacks these elements one on top of another. So essentially, 1 is floating all the way as far left as it can get. 2 is also removed from normal document flow. It's floated as far left as it can go now, as far left as it can go is on the rightmost edge of the first element, and element3 follows suit. A lot of people actually use this technique to create horizontal menus or photo galleries or things like that. As a matter of fact, if I go back into the code and for each one of these go ahead and do a margin right, let's just do a margin right of 1em, right--so I'm going to save that and just copy it and paste it for each one of these guy.
So, essentially what I am going to do is I'm giving each one of these guys a right margin, so I'm going to save this, preview it in the browser, and you can see, you can control the spacing between them through the use of margins. So essentially you can just sort of create this nice horizontal menu or horizontal list or photo galleries or things like that. Now I don't know if you caught this or not, but this white background that's part of the body tag used to go underneath these guys and now it does not. So what's happening here is something that's known as container collapse.
So the container, in this case the body tag, that was containing these floats, so the parent-containing element that was containing these floats, no longer is containing them. The reason for that is because remember, each of these items are floated. That means that each one of those is removed from normal document flow, so the parent doesn't see them. It doesn't think that it contains anymore, so it collapses upwards until it finds content again, and sometimes this will result in a parent element collapsing entirely so you can't even see it. So parent container collapse is definitely something we have to deal with, and we're going to talk about a few different ways that we can deal with it in this chapter.
There's one more thing I want to talk about here in regards to floated element, before we get into the individual specifics of them. That's basically the rules that govern how many of these sort of floated elements will fit on one line. It all has to do with the available width of those elements. So I'm going to go back into my code for a second, and you can see, we have our body element here, and our body element has a restricted width of 600 pixels. So if I go in and I just increase the size of my div tag, so let's say I make their width 200 pixels. Okay, so obviously two times three, there are three of them, should be 600 pixels, but remember, we have some margins between them as well.
So if I save this and preview that in the browser, you can see they no longer fit on that one line. So what happens is is once they run out available space, the next floated element just breaks down and resumes floating on another line. In terms of layouts, that's something that can really cause problems. We call that column collapse. Because if each of these were a column in a three- column layout, right now we wouldn't have a three-column layout; we would have one column breaking over here. So that's another issue that we're going to discuss how to deal with it as we get deeper into floats as this chapter goes on.
So in terms of just you know overall concept, floating is pretty easy to grasp; however, there's a lot more involved in creating float-based layouts than just utilizing the float property. Now for the rest of the chapter, we're going to focus on float-related concepts that will help take the mystery out of controlling floated elements, and we're going to start by introducing the clear property in our next movie.
Get unlimited access to all courses for just $25/month.Become a member