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

Containing floats

From: CSS: Page Layouts

Video: Containing floats

Earlier in this chapter, we saw one of the unfortunate side effects of floating elements. Remember, floated elements are removed from normal flow. That means that the height of their parent elements then collapse as if the floated elements inside them simply weren't there. This can cause a whole host of undesirable results, such as disappearing backgrounds and overlapping content. Now, fortunately, we have a few ways to prevent this from happening. So we have the containing.htm file open, and you can find that in the 03_03 folder.

Containing floats

Earlier in this chapter, we saw one of the unfortunate side effects of floating elements. Remember, floated elements are removed from normal flow. That means that the height of their parent elements then collapse as if the floated elements inside them simply weren't there. This can cause a whole host of undesirable results, such as disappearing backgrounds and overlapping content. Now, fortunately, we have a few ways to prevent this from happening. So we have the containing.htm file open, and you can find that in the 03_03 folder.

If I scroll down just a little bit, you can see that we have a section. Inside that we have three divs, with the classes One, Two, and Three. If I were to preview just the default styles in a browser, you can see these elements are just stacking, normal document flow, one on top of each other: One, Two, and Three. Well, currently the height of the section is being controlled by the elements inside of it. That's actually quite common. Height is generally considered unless it's set explicitly to be auto. That means the contents of an element define its height.

So once we float these elements and remove them from normal flow, then section really doesn't have any height and it's just going to collapse. So I am going to go back into my code, and what I am going to do is, each one of these div tags, One, Two, and Three, I am just going to go ahead and float those to the left. So I am going to do that for each of them, so that we can observe the parent collapsing. So I am going to go back into the browser, refresh the page, and you can see that's exactly what's happening now: One, Two, and Three are floating.

They're removed from normal document flow. Section doesn't see it inside of it anymore, and so it collapses. And in fact, the only thing we're seeing here right now, guys, is the padding that's set inside the section. So let's take a look at a couple of ways that we can help contain those floated elements inside the section. Okay. So if I go back into my code, well, one of the ways to do this is if you have an element that's underneath your floated elements and you clear that to restore normal document flow, then the parent container will go all the way down to contain it as well.

So if I go down here and right past the last div tag I add another paragraph, and inside of that paragraph I can just say "Clearing floats help to contain them," so if I save this, and I can just come right up here and create another selector for the paragraph and just tell that to clear: both. We added a new element underneath our floated elements, and then we told that element to clear, meaning reestablish normal flow.

So if I go back into my browser and refresh that, you can see what happens. Our paragraph shows up down here. It's cleared, meaning normal flow is reestablished, and our section is now extending down to contain that, so that's one way of containing it. Of course to do that we have to have another element below our floated elements, and that's not always going to be the case with your layouts. So there are a couple of other methods that you can use. Let's take a look at those. So if I come back into my code, I am going to get rid of that paragraph, selector and the paragraph itself, there we go. And what I am going to do is I am going to go up to the section selector, the one that's controlling its styling, and I am going to use the overflow property.

Now, the overflow property is a property that basically tells the browser what to do when the contents of a box no longer fit inside of it. So we have a few options available to us. Hidden is one. Hidden just says, hey, clip off any content that overflows. Auto would create scrollbars, either horizontally or vertically, based upon the flow of the content. And Scroll will force scrollbars in. Visible, by the way, just causes that content to be visible and overlap. Well, there is a side effect to using overflow.

So I am going to type in overflow: hidden. Now, anything other than Visible will work with it, so it could be Hidden, Auto, or Scroll. Now, obviously Scroll is going to force scrollbars to show up, and you don't want that. Auto will work, and it will work almost all the times with no side effects, but occasionally you're going to have a time when scrollbars show up for just weird reasons, because the floated elements inside of it perhaps are forcing a certain amount of auto flow. So by saying overflow: hidden, we're essentially saying, hey, we want to set overflow on this element, but we don't want any scrollbars to show up.

So if I save this and test it, now even without the extra content in the div tag, the float is being contained and that's the overflow property that is doing that for us. That's going to work for you about say 90% of the time, but there are going to be certain times when that overflow: hidden property is going to come back to bite you. Let me show you what I mean. So if I go back into my code, let's say I take the third div tag here, and I apply something relatively new, like box-shadow. So if I do box-shadow--now, when you're doing a box-shadow, that's just really, it's sort of a drop-shadow for this div tag.

So I am going to give 10-pixel offset to the X, 10-pixel Y offset, give it a 10-pixel blur, 10-pixel spread, and then the color for the shadow I will just go ahead and make that black, so #000. If I save this--and those are really high offsets, but I really wanted you to be able to see this, if I go back into the browser and load that up, you can see what's happening. Our drop shadow is showing up, but because the section has overflow set to hidden, it's going to hide this portion of the box shadow.

It's not going to expand to fill that. So there are times when overflow: hidden, even though it will contain floats for you, is going to give you some negative side effects. We do have one more technique that can work for us. So I am going to go ahead and get rid of the box-shadow. I am going to go ahead and get rid of overflow: hidden, and I want to modify one last little thing here. You will notice, for example, that body has a background color of white and a width of 600 pixels. So I am going to take the section and I am going to set its width to say 480 pixels.

Now, why am I doing that? Well, if I save this, and if I preview this in my browser, you can see now the section is actually not as wide as body, and you can also see that body sort of has that white background color here. Why am I doing that? Well, another method that you can use to contain floats is that if you float the parent element, meaning the container element, if you float that one as well, all floats inside of that will also be contained. So if I take section and I apply float: left to it as well, save that, and preview that in the browser, you can see what happens.

The float is now contained. But it sort of has a chain reaction. You'll notice, for example, that the body tag is now collapsing as well. We no longer see that white background because all of the content inside of it is now floated, and so it collapses. So a lot of times that works, but you end up having to go back through your layout and clear all these other items that you wouldn't have to clear previously. So it's not always the right answer either. So we've seen three ways that allow us to prevent container collapsing. We can clear child elements that appear below floated elements.

We can set the overflow property of the parent container to either Hidden, Auto, or Scroll. Or we can set a float value on the containing element itself. Now, as we've seen, each one of these methods have their pros and their cons, and you're going to have to very carefully consider when they're appropriate within your own layouts. Now, personally, I use either one of two methods: I will either use the overflow: hidden method, which is going to contain my floats, or I am going to use a clearing method for containing all the elements that have floated children, so clearing an element below it.

Now, specifically, I use a method called clearfix, because the method I showed you in this particular example requires there an element to be underneath it, but the clearfix method doesn't. Now, since the syntax of clearfix needs a little bit of explanation, we're going to go ahead and discuss that in a separate movie next.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 41401 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
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Page Layouts.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.