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

Understanding floating

From: Dreamweaver CS4 with CSS Essential Training

Video: Understanding floating

We're going to start a discussion of controlling layout through CSS in earnest by first examining floating. Floating is an extremely hard thing to really pin down. Although floating removes an element from normal document flow, the floated element continues to affect the rest of the layout. This places floating somewhere in between positioning and normal document flow, which is why it's such a great place to start the discussion on laying page elements without using CSS. Now on the surface, floating an element seems pretty simple. The float property has two specific values, left or right.

Understanding floating

We're going to start a discussion of controlling layout through CSS in earnest by first examining floating. Floating is an extremely hard thing to really pin down. Although floating removes an element from normal document flow, the floated element continues to affect the rest of the layout. This places floating somewhere in between positioning and normal document flow, which is why it's such a great place to start the discussion on laying page elements without using CSS. Now on the surface, floating an element seems pretty simple. The float property has two specific values, left or right.

A floated element either shifts to the left and to the right until its outer edge reaches the edge of its containing block or another floated element. As you can see in this slide, elements that come after a floated block act as if the element is no longer there and move up to occupy the empty space. So, line boxes such as individual lines of text, they're going to flow around a floated element, even though the lines containing that block actually flows under a 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 an element clears the float.

But for right now, we're going to tackle some of the basics of floating. So, let's examine the slide in a little bit more detail. We have three elements, box1, box2 and box3. These could be paragraphs, these could be Div tags, any block level element would work for this example. If we write a selector that takes box1 and floats it to the right, box1 is going to move over until its outer edge lines up with the outer edge of its containing element. That could be the body tag. It could be something another element it's inside of. It doesn't really matter. Whatever its closest containing element is, it's going to move all the way over.

Now box2 and box3, think that box1 has been removed from normal document flow, so they simply move up to occupy that space. Now, if box2 were wider than box1 or wider that this area, it would actually float up underneath box1. We're going to see an illustration of that in just a moment as we do our exercise. So, understanding how floats work is crucial to making sure that your elements don't overlap in a way that's very unexpected. Another thing that we need to realize is that floats don't affect objects above them in normal document flow.

So, if we had any elements above box1, floating it wouldn't really affect them at all. It only affects the elements that come after it in normal document flow. Now that we've got a little bit of background on floating, let's go in and experiment with floating some of our elements on the page. So, here we are in our 10_04 file and we've got three elements that we're going to experiment floating with and let's go over the document structure so that we can kind of understand who are moving around. So, we have three Div tags and the first one has an ID of box1, the second one has an ID of box2 and the third one has an ID of box3.

They are all contained within a Div with the ID container. So, when we'll start floating these elements around, they are going to extend to the rightmost or leftmost edge of that containing element and will affect that by changing the width of the containing element a little bit later on. Right now the containing element is stretching to the entire width of the page. Now, we do have some existing rules, so if you open up your CSS Styles panel, you can kind of go through these rules and see them. Our box1 has a defined height and width and we've got 10 pixels with a margin on the bottom of the those and a little bit of padding. Box2 also has a defined width, but no height and it also has 10 pixels worth of margin on the bottom.

Finally, box3 is almost exactly the same just a different background color. The container has a background color, but nothing else. So, we'll change its properties later on to see how it affects the interior floated objects. So, I'm going to select box1 and I'm going to add a property to that and we're just going to go ahead and float it. When we choose float, you can see using the pulldown menu that we have two really defined values, left and right, none would turn floating off, and inherit would just say do whatever your parent element is doing. Let's go ahead and float that to the right. Notice that it moves all the way to the right edge of the containing element.

Elements 2 and 3 move up to occupy the space where element 1 used to be. So, they don't see element 1 in normal document flow anymore. They just move right up to occupy that space and element 1 sits over here on the right hand side. Now, that's a lot like absolute positioning, but there are some differences, so I'm going to change the float on box1 from right to left. Now, we see a little bit of difference here. So, what's happening now is that element 1 is against the leftmost edge and element 2 and 3, instead of acting as if it was a normal document flow giving it a spacing, simply move up to occupy that space.

Now, it doesn't look like that's what happened. It looks like just we've had margin collapse, but that's not what happened at all. As a matter of fact, you can see this 10 pixels worth of margin is still being applied. Now element 2 moved up underneath element 1, but the inline elements, such as this text and this inline text is actually going to wrap around the floated element if it has enough room. Let's explore that a little bit. So, I'm going to highlight the #box2 selector and we're going to change the width, give it a width of 300 pixels. What we see now is that the background is indeed going up underneath element 1.

So, it's just moving up to occupy the space that element 1 used to be in. Now, our text still isn't wrapping around on the float, because we just don't have enough width over here. If we increase that width even more to say 500 pixels, notice now those inline level elements can come up and float around the floated element. So, these lines of text are just going to float all the way around this, but the element itself, this entire Div tag is actually moving up to occupy that space. Typically now understanding how inline elements wrap around, floats can cause problems in two or three-column layouts.

If you've got defined widths in your layouts, you really don't need to worry about these inline elements wrapping around it. But if for whatever reason your widths increase, a lot of times the elements underneath this will float up and wrap around it unexpectedly and that can cause your layouts to break. So, you want to be really, really careful about this particular property. Sometimes we use it to our advantage to wrap text around an image, other times it sort of crops up unexpectedly. So, make sure you're aware of what's happening there. Now, I'm going to take box1 and I'm going to increase some margin here. I'm going to add another property and I'm just going to add the margin property.

Currently we have a defined margin for the bottom, but we're going to put margin all the way around it. Let's do 10 pixels. All right now, look what happens. Our element 1's margin is affecting element 2, even though element 2 doesn't see it within normal document flow. So, floating gives you both normal positioning and removes it from positioning all at the same time. Now, I'm going to save this and preview in the browser so that you can really see how this is going to be rendered. You can see it's getting equal spacing all the way around and the element 2 is still wrapping around it. Now let's go back in and do another float on element 2.

So I'm going to select box2, I'm going to add the float property to it and I'm going to float that to the right. So now element 3 continues to come up and occupy the space that element 2 used to be in. So, you could just keep doing that and the elements underneath it are just going to keep coming up to occupy that space. So, element 2 is floated against the right edge, element 1 is floated against the left edge. Now, I'm going to change the containing width, so I'm going to change the container and I'm going to add a width property to it and give it a width of 800 pixels. Now, that shrinks it down a little bit and now our element 1 and element 2 relate to each other a little better.

If I do a Save All and preview that in my browser, you can see that we're close to something that looks almost like a two-column layout. Well, let me show you how easy that look is to achieve. I'm going to go back by my box1 and I'm going to remove that margin. So that margin all the way around that, I'm going to remove that. I'm going to keep the margin on the bottom, but I'm going to remove the margin wrapping all the way around it. Now, the next thing we're going to do is something we haven't really talked about yet and that's clearing. Element 3 continues to be affected by the floats. So, if at any point in your layout, you want to stop the floats from affecting the elements underneath it, all you need to do is invoke the Clear property.

So, I'm going to find box3, I'm going to add a property and I'm going to type in, clear. Now, clearing has several values too: you can clear it to the left, to the right or you can clear both. What this essentially says is don't allow any element to float to the left of me or don't allow any element to float to the right of me. Both simply says don't let any element float to either my left or my right. We're going to choose both and notice that element 3 is now pushed down and it clears element 2 and element 1. Now, if I were to take box3 and remove its width, now that looks off a lot like a footer.

If I save this and preview this in my browser, I can see that I have basically what equates to it, two-column layout. So, floating your elements is a very economical way to arrive at a basic layout. Since the margins of floated elements still affect the items that are around them, a float-based layout is really easy to achieve. This is one of the reasons that the overwhelming percentages of CSS-based layouts are using floats as their main positioning property. Being able to clear your elements at certain key points, just like we did right here with element 3, is vital to controlling floats in a wider context of your layout.

So, in our next exercise, we're going to discuss clearing in a lot more detail, including exploring how it can be help us contain floats within parent elements.

Show transcript

This video is part of

Image for Dreamweaver CS4 with CSS Essential Training
Dreamweaver CS4 with CSS Essential Training

120 video lessons · 41519 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 7m 50s
    1. Welcome
      1m 18s
    2. Do I need to know CSS to use Dreamweaver?
      2m 15s
    3. Using the exercise files
      1m 30s
    4. Setting up a custom workspace
      2m 47s
  2. 1h 2m
    1. Separating structure from presentation
      4m 14s
    2. Adding meaning with ID and class attributes
      7m 50s
    3. Understanding basic selectors
      11m 10s
    4. Understanding complex selectors
      11m 21s
    5. Examining the Cascade
      10m 16s
    6. Understanding order of inheritance
      5m 2s
    7. Understanding specificity
      5m 43s
    8. Using Dreamweaver to resolve conflicts
      7m 4s
  3. 52m 48s
    1. Working with starter pages
      2m 1s
    2. Defining fixed, elastic, liquid, and hybrid
      6m 3s
    3. Understanding starter page structures
      6m 25s
    4. Modifying CSS globally
      8m 58s
    5. Moving CSS between files
      12m 31s
    6. Preparing custom starter pages
      10m 9s
    7. Creating custom starter pages
      6m 41s
  4. 1h 25m
    1. Designing with CSS in mind
      3m 13s
    2. Using Fireworks to create site prototypes
      2m 41s
    3. Defining page structure
      8m 52s
    4. Creating the initial layout
      10m 24s
    5. Page creation and asset sharing
      11m 20s
    6. Using common libraries to create site prototypes
      5m 11s
    7. Building interactive prototypes
      17m 6s
    8. Optimizing images in Fireworks
      11m 47s
    9. Exporting web graphics from Fireworks CS4
      2m 43s
    10. Exporting interactive prototypes
      3m 11s
    11. When to export XHTML and CSS from Fireworks CS4
      8m 34s
  5. 48m 28s
    1. CSS workflows in Dreamweaver
      1m 17s
    2. Using the CSS Styles panel
      5m 12s
    3. Setting CSS preferences
      9m 50s
    4. Understanding Dreamweaver's CSS visual aides
      3m 50s
    5. The Code Navigator
      6m 5s
    6. Controlling CSS with the Properties Inspector
      8m 52s
    7. Using Related Files
      4m 35s
    8. Working with Live view
      4m 12s
    9. Working with the Reference panel
      4m 35s
  6. 1h 13m
    1. Declaring font families
      4m 57s
    2. Creating custom font family declarations in Dreamweaver
      6m 0s
    3. Understanding units of measurement
      6m 14s
    4. Controlling font sizing
      8m 41s
    5. Controlling line spacing
      7m 20s
    6. Controlling vertical margins
      7m 52s
    7. Horizontally aligning text
      3m 16s
    8. Vertically aligning text
      5m 30s
    9. Vertically centering block-level elements
      10m 31s
    10. Setting column width
      3m 33s
    11. Using font shorthand notation
      9m 15s
  7. 1h 10m
    1. Background properties
      4m 33s
    2. Using background images
      5m 16s
    3. Controlling background image tiling
      5m 33s
    4. Positioning background images
      4m 42s
    5. Using percentage values for positioning
      5m 10s
    6. Creating custom list bullets
      5m 23s
    7. CSS drop shadows
      7m 40s
    8. Image replacement techniques
      7m 24s
    9. Adding screen-only content
      7m 51s
    10. Complex background graphics
      9m 10s
    11. Using CSS Sprites
      8m 0s
  8. 38m 48s
    1. Reviewing table tag structure
      4m 48s
    2. Using thead and tbody for styling
      5m 45s
    3. Styling table captions
      4m 30s
    4. Styling headers
      8m 29s
    5. Styling table content
      4m 18s
    6. Creating alternating row colors
      3m 29s
    7. Using pseudo-class selectors for tables
      2m 27s
    8. Creating custom table borders
      5m 2s
  9. 43m 37s
    1. Examining form structure
      3m 26s
    2. Styling fieldset and legend elements
      7m 42s
    3. Styling form elements globally
      6m 31s
    4. Using classes to identify form elements
      5m 55s
    5. Styling user feedback
      6m 10s
    6. Adding user interaction
      2m 52s
    7. Styling Spry form validation fields
      11m 1s
  10. 44m 48s
    1. Using lists for navigation
      1m 40s
    2. Creating horizontal menus pt. 1: Stripping list styling
      3m 28s
    3. Creating horizontal menus pt. 2: Displaying links horizontally
      3m 54s
    4. Creating horizontal menus pt. 3: Styling links
      5m 25s
    5. Creating horizontal menus pt. 4: Rollovers
      3m 48s
    6. Creating horizontal menus pt. 5: Indicating current page
      3m 48s
    7. Creating horizontal menus pt. 6: Modifying cursor usage
      2m 11s
    8. Creating horizontal menus pt. 7: Positioning menus
      3m 8s
    9. Styling vertical menus pt. 1: Vertical menu considerations
      6m 42s
    10. Styling vertical menus pt. 2: Defining width for link elements
      4m 46s
    11. Styling vertical menus pt. 3: Using background graphics with navigation
      5m 58s
  11. 1h 40m
    1. Box model review
      7m 4s
    2. Understanding margin collapse
      7m 15s
    3. Reviewing normal document flow
      11m 0s
    4. Understanding floating
      8m 56s
    5. Containing and clearing floats
      9m 26s
    6. Understanding relative positioning
      5m 30s
    7. Understanding absolute positioning
      5m 29s
    8. Understanding the AP Elements panel
      11m 57s
    9. Understanding fixed positioning
      2m 24s
    10. Using Dreamweaver to define document structure
      10m 11s
    11. Creating a two-column layout
      17m 1s
    12. Using Dreamweaver's Design-Time style sheets
      3m 49s
  12. 28m 59s
    1. Introducing Spry widgets
      2m 18s
    2. Updating the Spry framework
      45s
    3. Examining the default Spry styles
      6m 36s
    4. Modifying tabbed panels through CSS
      5m 18s
    5. Styling Spry widgets
      8m 49s
    6. Organizing Spry style sheets
      5m 13s
  13. 34m 49s
    1. Creating print style sheets
      2m 57s
    2. Assigning media types
      3m 29s
    3. Styling type for print
      9m 21s
    4. Suppressing element printing
      3m 29s
    5. Controlling page breaks
      8m 39s
    6. Using @media blocks
      3m 5s
    7. Creating alternative style sheets
      3m 49s
  14. 35m 22s
    1. Using Dreamweaver's Browser Compatibility Check
      3m 58s
    2. Fixing code errors with Adobe's CSS Advisor
      4m 45s
    3. Strategies for browser compatibility
      5m 8s
    4. Implementing browser compatibility
      8m 18s
    5. Formatting code for deployment
      3m 15s
    6. Creating modular style sheets
      3m 38s
    7. Assembling modular style sheets
      6m 20s
  15. 38s
    1. Goodbye
      38s

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 Dreamweaver CS4 with CSS Essential Training.

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.