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 the left of
it's 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 is how it works.
Remember that in normal document flow, block-level elements simply stack one on top of another.
They are commonly refer to as boxes, and that's actually really good way of
thinking about them.
When you float an element, it moves as far over as it can, either it to the
right or the left, depending upon 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 pretty easy way to create a two-column layout.
But you can probably guess, there is 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.
Now, the default element alignment is to the left edge of their containing
element, so if you float one of those top boxes to the left, it doesn't
appear to move it all,
since it's already aligned to the left of the page.
However, it's still removed from normal document flow.
And that means, the element below it would move up to occupy what they see as empty space.
The result? Well, those elements would actually go underneath to 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 are seeing here is how those 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 have 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 these
series of boxes that have all been floated to the left, you end up with the
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.
I also want to make sure that we talk about clearing floats.
And 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 have got this content element floated to the right, we
have the sidebar element floated to left.
However, based on the way floats work, any remaining content on the page, like our
footer here, would move up an 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 will 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 will 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.
Floats are by far the most popular CSS layout technique currently in use, so
there are plenty of examples in tutorials out there for you to learn from,
including many of the CSS titles here in the lynda.com Online Training Library.