Viewers: in countries Watching now:
In Dreamweaver CS4 with CSS Essential Training, web developer and Adobe Master Instructor James Williamson teaches the fundamentals of CSS while focusing on how to use Dreamweaver to efficiently create and manage styles. He shows how to use Dreamweaver to resolve style conflicts; how to use new CSS-related features such as Live View; and the best way to create lightweight, site-wide style sheets.
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.
Find answers to the most frequently asked questions about Dreamweaver CS4 with CSS Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.