Float & Clear — CSS

float: left or float: right moves an element, usually an image, to the left or right edge, and the text flows beside it. Give the element a margin on the side…

Open a magazine page. The photo is on the left edge. The words flow down the right side of the photo, then continue below it. In CSS, a layout like that magazine page is made with the float property.

float moves an element, usually an image, to the left or right edge. The text around it then flows beside that element. An element with float is also called a float. The clear property is the partner of float: clear makes an element stop flowing beside a float, and start below that float.

Image on the left, text on the right

This example has one image, one paragraph below it, and the CSS rule for the image:

<img src="lake.jpg" alt="A crater lake">
<p>This lake was formed by an ancient volcanic eruption. An island now sits in its middle.</p>

<style>
img {
  float: left;
  margin-right: 16px;
}
</style>

The result: the image on the left and the text on the right, like in a magazine. Without float, the whole paragraph in this example stays below the image.

In the exercise, the img { } rule is already there. So far it only holds a comment: /* write CSS here */. Replace that comment with your CSS lines, inside the same curly braces.

Left, right, or not at all

clear: start below the float

A second paragraph, if there is one, also flows beside the image while there is room. To make the second paragraph start below the image, give that paragraph clear. This rule is for a second paragraph that has the class second-paragraph:

.second-paragraph {
  clear: both;
}

clear: both means: don't sit beside any float, left or right. There is also clear: left (only move down past floats on the left) and clear: right.

The collapsing parent

When the browser calculates the height of a parent, a floated element is not counted. For example, a box with a border holds one floated image and one line of text. The box collapses: it is only as tall as the text, and the image comes out of the bottom of the border.

The fix: give that parent display: flow-root. In this example the parent has the class card:

.card {
  display: flow-root;
}

Now the border goes around the image too. In older code you will find a more complicated way, called clearfix: the parent gets an empty ::after element that has clear: both (::after has a lesson of its own). overflow: auto on the parent works too. As a side effect, it makes the parent go around the floats inside it.

⚠️ Float in the wrong place

TL;DR: float: left or float: right moves an element, usually an image, to the left or right edge, and the text flows beside it. Give the element a margin on the side that faces the text. An element that has float is called a float. clear: both makes an element start below a float, not beside it. A parent's height does not count a floated child, so the parent can collapse: it becomes shorter than the image inside it. The fix: give that parent display: flow-root (the old way is called clearfix).

⚡ Image left, text right

Write:

img {
  float: left;
  margin-right: 16px;
}

Meaning: float: left moves the image to the left edge. The paragraph below the image then moves up, and its text flows down the right side of the image. margin-right adds a gap so the text doesn't touch the image.

In the exercise, replace the comment /* write CSS here */ in the img { } rule with your CSS lines.

⚡ Start below the float

Write:

.second-paragraph {
  clear: both;
}

Meaning: A float is an element that has float, like the image in the first step. clear: both means: don't sit beside any float, left or right. So the paragraph with the class second-paragraph starts below the image, not beside it.

⚡ The collapsing parent

Guess first: a box with a border holds a floated image and one line of text. How tall is the border?

Only as tall as the text. When the browser calculates the height of a parent, a floated element is not counted. So the image comes out of the bottom of the border.

Meaning: A parent that is shorter than the image inside it has collapsed. Give that parent display: flow-root, and the border goes around the image too.

What you will learn