Notice how the text will wrap around the boxes. You will also notice that even though the floating divs were created inside the article they will extend outside of that block if there is not enough room.
You have seen in newspapers and magazines how they have pictures with the text wrapping around the image. This was a very early feature that was available in CSS known as floats. The float property is telling CSS to float the object meaning that it will be moved to the left or right (depending on the settings) and text will wrap around the object. In addition to left and right you have inline-start and inline-end which are the same as left and right but honor the direction of the text so if you are using a language that is right-to-left then inline-start would be right and inline-end would be left.
Let's set up some objects to float (normally you would use images, but solid blocks of color will work for demonstration purposes).
Next our left and right floats.
And our content.
Notice how the text will wrap around the boxes. You will also notice that even though the floating divs were created inside the article they will extend outside of that block if there is not enough room.
This also applies to the right. But when the new article was posted, we had not cleared the height of the previous boxes so there is some overlap. The right, however, does honor the order of the right is rightmost first. You should also note that the 1em margin for the paragraph is ignored by the divs.
You will notice that floats aren’t taken into account when margins and padding are added. This means that if you want margins around the float will need to have them as part of the float.
It is also possible to skip to the end of a float (useful for a new section) by using the clear attribute. I tend to just use embedded styles for clear, but having classes for clearing is certainly an option. You can clear left, right, or both.
Now that we have some margins in our floats, our text have some margin to them. To keep the margins only to the material being added, just that side has a margin.
Also notice that floats start on the row they are added.
By using clear we can skip over left, right, or both floats. Here I am just skipping the left float.
Floats are often all you will need for images you want to wrap text around. Of course it would be nice if we had a bit more control over floating elements, which we discuss in the next section.
This chapter's summary cheat sheet.
Why layout is complicated for web pages.
The basic display types.
A box that contains other components and sizes them to fit the available space.
A flexible grid where you can lay out your design.
Using templates to make laying out components in a grid easy.
Using the columns attributes to easily allow for multiple columns.
Having images (or other blocks) have text wrap around them.
There are other ways of placing elements by using the position attribute.
Altering CSS based on the properties of the device the page is being rendered on.
The project for this chapter.
Solution for this chapter's project