Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Floating & Clearing | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 12 of 50

·css

Floating & Clearing

8 mins

Course progress24%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • `float` removes the element from the normal document flow.
  • Use the **clearfix** hack (`.clearfix::after { content:""; display:table; clear:both; }`) when many floats are stacked.
  • Prefer Flexbox/Grid for layout; use float only for media‑wrapping.

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).

PropertyEffect
float:left / float:rightElement is taken out of normal flow and pushed to the left/right, allowing inline content to flow around it.
clear:left/right/bothForces the element to start below any preceding floated elements.

Example – Image With Text Wrap

<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>

The clearfix Hack

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>

Modern Alternative

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.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonPositioningContinue to lesson 13Overflow & ScrollbarsBack to CSS Basics to Advanced curriculum