- [Voiceover] Any discussion on positioning elements
would be incomplete without mentioning floating.
Floating is by far the most popular CSS layout technique,
however it's also one
that many new web designers struggle with.
That's because how the technique works changes
based upon the relationship of the elements
that are being floated.
Let's take a closer look at floating and how it works.
So what's a float?
Essentially, it's an element that is shifted
to the right or to the left of its current position.
You assign floating, logically enough,
by using the float property.
You can use the left, right, or none keywords
to define the direction of the float.
Here's how it works.
Remember that a normal document flow,
block level elements simply stack
one on top of another.
They're commonly referred to as boxes,
and that's actually a really good way
of thinking about them.
When you float an element,
it moves as far over as it can,
either to the right or the left,
depending on the property that's been assigned to it.
The elements below it then move up
to occupy the space
that the floated element used to occupy.
In that sense, floating an element
removes it from normal document flow.
As you can see, floating an element to the right
and then allowing the content below it
to move up beside it,
it's a pretty easy way to create a two-column layout.
As you can probably guess,
there's a lot more to floats than just this.
For example, let's take a look at what happens
when you float an element to the left.
And the default element alignment is to the left edge
of their containing elements, so if you float
one of those top boxes to the left,
it doesn't appear to move at all,
since it's already aligned to the left of the page.
However, it's still removed from normal document flow,
and that means that the elements below it
would move up to occupy what they see
as empty space.
The result?
Well, those elements would actually go
underneath the floated element,
at least partially.
You see, individual parts of an element
will react differently to floated elements.
To best demonstrate this,
let's take a look at another example.
Let's say we have an image contained within a paragraph.
If you were to float the image to the left,
it looks as if the paragraph is wrapping around the image
instead of going underneath it.
Kind of weird.
The truth is, what we're seeing here
is how the inline boxes, or those lines of text,
react to floated elements.
Inline boxes, such as the lines of text within a paragraph,
will wrap around floated elements if they have enough room.
Now, if the paragraph had a background color,
you would still see that the paragraph itself
did indeed move up and underneath the image.
Once you've learned the ins and outs of floats,
you can do all kinds of cool things with them.
For example, if you float a series of elements
to the same side, like this series of boxes
that have all been floated to the left,
you end up with a horizontal display of elements.
This technique is used frequently to transform
a list of links into a horizontal menu, for example.
You can also use floats to create
multiple columns of elements,
drop caps in paragraphs,
pull quotes that have text wrapped around them,
product description details,
and a whole lot more.
You know, I also want to make sure
that we talk about clearing floats.
Now if you remember from earlier,
I discussed how floated elements
affect all the elements below it in normal flow.
At times, that's less than ideal.
You might, for example, create a two-column layout
by floating one containing element to the left
and one to the right, as you see here.
So we've got this content element floated to the right.
We have the sidebar element floated to the left.
However, based on the way floats work,
any remaining content on the page,
like our footer here, would move up
and underneath the floated elements.
By using the clear property, you can tell the browser
to stop floating elements
and go back to normal document flow.
The clear property can be set to prevent elements
from floating to the left or right of an element,
or set to both.
So you can use the left, right, or both values
to prevent any float from affecting the cleared element.
The best way to think of clearing
is just think of it as your way of telling the browser,
hey, thanks, but I need to start going back
to stacking elements again.
Now obviously, mastering how browsers
handle floated elements requires a little bit of time.
You'll need to become comfortable with how floated elements
affect their parent elements,
how clearing works,
how margins work with floated elements,
and how the width of parent elements
can impact the behavior of floated elements,
for example, making them break down
to the right or left instead of floating up beside them.
You'll then need to take that a bit further
and explore the best practices
that have evolved over the years
regarding using floats for layout.
Now, the good news about that is that
a fair amount of ground has already been covered for you.
If floats are by far the most popular CSS layout technique
currently in use, so there are plenty of examples
and tutorials out there for you to learn from,
including many of the CSS titles here
in the lynda.com online training library.