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

Start learning with our library of video tutorials taught by experts. Get started

CSS: Page Layouts
Illustration by

Clearing floats


From:

CSS: Page Layouts

with James Williamson

Video: Clearing floats

As important as the float property is to creating layouts, it would be practically worthless without the use of the clear property. The clear property allows us to turn floats off and restore normal document flow and together, floating and clearing allows us to exercise precise control over the flow of our layouts. To demonstrate that, I have the clearing.htm file open, and you can find that in 03_02 directory. Now, structurally it's very similar to the file we were using in our last exercise. As a matter of fact, if I scroll down, I can see that the only big difference is that instead of only having three elements, we now have six.
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

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
CSS: Page Layouts
8h 57m Beginner Feb 07, 2012

Viewers: in countries Watching now:

CSS: Page Layouts introduces basic layout concepts, gives advice on how to create properly structured HTML based on prototypes and mockups, and goes into critical page layout skills such as floats and positioning. Author James Williamson shows how to combine these techniques to create fixed, fluid, and responsive layouts. Designers are also shown how to enhance their pages through the creative use of CSS techniques like multi-column text, opacity, and the background property. Exercise files are included with this course.

Topics include:
  • Reviewing the box model
  • Calculating em and percentage values
  • Controlling how elements display
  • Creating fixed, fluid, and responsive layouts
  • Structuring content with HTML5
  • Floating elements
  • Using relative, absolute, or fixed positioning
  • Defining column spacing
  • Creating grid-based assets and layouts
  • Considering mobile-design-specific issues
  • Working with multi-column text
  • Enhancing page design CSS Sprites
Subjects:
Web Web Design
Software:
CSS
Author:
James Williamson

Clearing floats

As important as the float property is to creating layouts, it would be practically worthless without the use of the clear property. The clear property allows us to turn floats off and restore normal document flow and together, floating and clearing allows us to exercise precise control over the flow of our layouts. To demonstrate that, I have the clearing.htm file open, and you can find that in 03_02 directory. Now, structurally it's very similar to the file we were using in our last exercise. As a matter of fact, if I scroll down, I can see that the only big difference is that instead of only having three elements, we now have six.

For styling purposes, you'll notice we've got a lot going on just in the regular div rather than installed individually. All of the divs are being floated to the left, and they all have a little bit of margin to the right to give us a little bit of spacing. Okay. So I'm going to save this, and I want to preview this in my browser. Currently, you can see all the elements are floated to the left. Now, they would all appear on one line except for the fact that we have a constraint going on right here with the page. So if the page were a little bit wider, they would all show up on the same line, but because it's constrained at 600 pixels, when we run out of room, the next floated element breaks down to the line and floats resume.

So how does clearing work? Well, let's say that we wanted to have two rows of equal items, so we wanted two rows of three, right? We have a couple of values available to us here when we do clearing: we have left, right, both, inherit, and none. Now, none is turning clearing off. Inherit basically says whatever my parent has, that's what I want, so it's these three that really matter for what we're doing in this exercise. So clear: left, essentially it tells an element nothing can float to my left; clear: right says, all right, nothing is allowed to float to my right; clear: both says nothing is allowed to float to my right or my left.

So if something is cleared, it essentially breaks down to the next line and reestablishes normal document flow. So currently, for example, number two, this two element right here, has one floating to its left, three has two floating to its left, so forth and so on. So if I go back into my code and I go right down here to element number four and I just say, okay, let's go ahead and clear that element to the left. So if I save this, go back in my browser and preview, that is exactly what happens. Four breaks down on the next line and reestablishes normal document flow.

Now, just because normal document flow is reestablished doesn't mean that you can't then at that very moment go ahead and resume floating. We're certainly doing that here. So four is still floated to the left, five and six are still floated to the left, but for just that moment we're clearing those floats and reestablishing that normal document flow on another line. Well, that works great. Right works the same way. I'm sure you can envision and imagine how that works. But what about both? Shouldn't we know whether an element is going to going to float to the right or left; why would we need both? To demonstrate that, I'm going to go back into our code and just modify it slightly.

I'm going to remove the clear that we just applied to 4. Then I'm going to go up to my just default divs. I'm going to remove float to the left, so go ahead and get rid of that. I'm going to remove the margin-right, and then I'm going to change the width to 150 pixels. So now all of my elements are just stacked, normal document flow, they're 150 pixels wide, and we no longer have any right margin on them. Okay. Now, let me go down to the first two elements, and we're going to modify those slightly as well. We're going to take element number one and I'm going to float that to the right.

I'm also going to set its width to 50 pixels. For element number two, I'm going to float that to the left. So we've got one element floated to the right, we've got another element floated to the left, and I'm going to give it a width of 50 pixels as well. Okay. Now, if I save that and go in and refresh this, it looks weird, but bear with me here. So element number 1 is floating all the way over to the right-hand side. element2 is floating over to the left-hand side. 3, 4, 5, and 6 are in normal document flow. Remember, when I float 1 and 2, I removed them from normal document flow, so the remaining elements move up to occupy that space.

So essentially, two is just sort of overlapping three, and that's one of the reasons why we made those other elements wider, so we could sort of see that overlap. So if I go back into my code--and element3 is who we want to focus on right now. If I tell element3 clear to the right, and check that, now what happens is it says, okay, number one, you can't float to my right, so it's going to go down onto the next line, it's going to reestablish normal document flow, and the page layout just sort of continues. But what if I said instead of clear: right, clear: left, what would happen then? So if I save this and preview it, no change. Okay.

So in this case clear: right and clear: left do exactly the same thing, because three basically had one floating to its right and two floating to its left, so really they're both doing the same thing, so why in the world would I need clear: both? Right now we're working with a very static layout in terms of, we know exactly the element dimensions of both of these, but it isn't always that simple. Now, one thing I want to point out here is, do you notice how we've kind of messed with source order a little bit? The first element that any sort of user agent would encounter in the code would be element number one, but visually we're almost making two look like it comes before one, because we typically read from left to right.

So let me go back into the code. Let's say we go up to element one and we change its height to 200 pixels. So I'm going to go ahead and save that and preview it. And what happens is is now element one, because it is 200 pixels tall, can float to the right of all these elements. These elements are simply going to move up and underneath floated elements, but since we have clear: left applied to three, it's breaking down here and reestablishing normal document flow, but we're not preventing elements from floating to its right, in this case, the number one.

If I went back into my code and I told element three, no, no, no, no, clear to the right please, and save that and test it, okay, now it's clearing to the right, it's reestablishing normal document flow, and it's not letting either of those float left or right of it. But again, this all has to do about the height of the elements. Again, if I come back into element two and I change its height so that it's 250 pixels, save that and test it, and you can see, we get overlap again. So to prevent overlap, when you really don't know, you're not really sure if elements are going to be floated to its left, you're not really sure if elements are going to be floated to the right, you're not really sure in terms of the height of the various elements, you can always come in and say instead clear: both.

And now it doesn't matter who is taller. It's simply going to clear both of them. So right now two is taller than one, so it's clearing that and reestablishing normal document flow. But if I take element two, take its height back to, say, 100 pixels, you can see it's still not going to go above where one is either. That's clearing. It gives us a precise amount of control over which elements we allow to float and when we want to stop floating and resume normal document flow. Clearing also allows us to deal with another very tricky problem related to floats, and that's how containing elements will collapse when child elements are floated.

We're going to take a closer look at that in the next movie.

There are currently no FAQs about CSS: Page Layouts.

 
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.
Upgrade now


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 Upgrade now

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

Notes cannot be added for locked videos.

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.