Lesson 12 of 50
cssFloating & Clearing
8 mins
Course progress24%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 12 of 50
css8 mins
Keep these ideas in mind as you work through the lesson.
Historically, float was the only way to make text wrap around an image. Today it lives on for very specific use‑cases (image‑wrap, magazine‑style layouts).
| Property | Effect |
|---|---|
float:left / float:right | Element is taken out of normal flow and pushed to the left/right, allowing inline content to flow around it. |
clear:left/right/both | Forces the element to start below any preceding floated elements. |
<img src="portrait.jpg" alt="Portrait" style="float:left; margin:0 1rem 1rem 0; width:150px;" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin gravida...</p>
When you have a container with floated children, the container collapses to zero height. The classic solution:
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* Apply the class to the parent */
<div class="clearfix">
<img src="…" style="float:left;">
<p>…</p>
</div>
Most layout problems that once required floats are now solved with Flexbox or Grid. Use float only when you need the classic image‑wrap effect.