New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Floating elements

From: CSS: Page Layouts

Video: Floating elements

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.

Floating elements

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.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 40101 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 20s
    1. Welcome
      54s
    2. How to use the exercise files
      3m 26s
  2. 1h 39m
    1. Box model review
      8m 47s
    2. Calculating element dimensions
      11m 11s
    3. Understanding margin collapse
      7m 59s
    4. Calculating em values
      7m 41s
    5. Calculating percentage values
      7m 51s
    6. Normal document flow
      13m 3s
    7. Controlling element display
      8m 53s
    8. Using CSS Resets
      7m 11s
    9. Fixed, fluid, and responsive layouts
      9m 9s
    10. CSS debugging tools
      6m 46s
    11. Using the Firebug Inspector and the WebKit Web Inspector
      11m 5s
  3. 53m 15s
    1. Page design workflow
      3m 6s
    2. Page design tools
      4m 56s
    3. Determining page structure
      7m 18s
    4. Creating image assets
      8m 58s
    5. Creating initial page structure
      7m 3s
    6. Adding meaning with classes and IDs
      5m 23s
    7. Structuring content with HTML5
      6m 6s
    8. Building internal structure
      10m 25s
  4. 1h 36m
    1. Floating elements
      7m 50s
    2. Clearing floats
      7m 28s
    3. Containing floats
      7m 50s
    4. Clearfix technique
      10m 38s
    5. Floating inline elements
      14m 34s
    6. Two-column floated layouts
      8m 17s
    7. Three-column floated layouts
      11m 30s
    8. Column height considerations
      7m 3s
    9. Creating equal-height columns
      10m 42s
    10. Floats: Lab
      5m 25s
    11. Floats: Solution
      5m 21s
  5. 51m 42s
    1. Relative positioning
      7m 59s
    2. Absolute positioning
      8m 59s
    3. Fixed positioning
      4m 23s
    4. Controlling stacking order
      8m 31s
    5. Clipping content
      8m 21s
    6. Controlling content overflow
      5m 38s
    7. Positioning elements: Lab
      3m 59s
    8. Positioning elements: Solution
      3m 52s
  6. 48m 46s
    1. Design considerations for fixed layouts
      3m 28s
    2. Establishing the layout grid
      7m 57s
    3. Defining column spacing
      9m 30s
    4. Applying the grid through CSS
      8m 56s
    5. Creating grid-based assets
      8m 26s
    6. Grid design resources
      6m 22s
    7. Building fixed layouts: Lab
      4m 7s
  7. 44m 35s
    1. Designing for flexible layouts
      2m 30s
    2. Calculating percentage values
      8m 45s
    3. Setting flexible width values
      6m 6s
    4. Making images flexible
      8m 10s
    5. Setting minimum and maximum widths
      7m 24s
    6. Building flexible layouts: Lab
      4m 53s
    7. Building flexible layouts: Solution
      6m 47s
  8. 49m 36s
    1. Responsive layout overview
      3m 49s
    2. Using media queries
      7m 16s
    3. Organizing styles
      8m 39s
    4. Making content responsive
      8m 33s
    5. Mobile design considerations
      7m 32s
    6. Building responsive layouts: Lab
      4m 23s
    7. Building responsive layouts: Solution
      9m 24s
  9. 1h 22m
    1. Creating multi-column text
      6m 36s
    2. Using borders to enhance design
      13m 59s
    3. Rounding corners
      6m 56s
    4. Adding drop shadows
      10m 35s
    5. Working with opacity
      6m 8s
    6. Utilizing the background property
      15m 5s
    7. Working with CSS sprites
      7m 58s
    8. Enhancing page design: Lab
      6m 22s
    9. Enhancing page design: Solution
      8m 38s
  10. 6m 25s
    1. Additional resources
      6m 25s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.