๐ŸŒŠ CSS Floating & Clear

Master float and clear properties for wrapping text and creating layouts

โ† Back to Content Next Topic: Navigation Bar โ†’

๐ŸŽฏ What is Float?

The float property was originally designed to wrap text around images (like in magazines). While modern layouts use Flexbox and Grid, float is still useful for text wrapping and is important to understand for legacy code.

Float Use Cases:

  • โœ… Wrapping text around images
  • โœ… Creating magazine-style layouts
  • โœ… Pull quotes and side notes
  • โœ… Understanding legacy layouts
  • โœ… Simple multi-column layouts

โš ๏ธ Modern Note:

For modern layouts, prefer Flexbox or Grid over floats. However, floats remain the best choice for text wrapping around images.

๐ŸŠ Float Property

The float property removes an element from normal document flow and shifts it to the left or right, allowing content to wrap around it.

Float Values:

/* Float to the left */
.left {
    float: left;
}

/* Float to the right */
.right {
    float: right;
}

/* No float (default) */
.none {
    float: none;
}

/* Inherit from parent */
.inherit {
    float: inherit;
}

โฌ…๏ธ Float Left

Floats the element to the left side, allowing text and inline elements to wrap around its right side.

/* Classic example: Image with text wrapping */
img {
    float: left;
    margin-right: 20px;
    margin-bottom: 10px;
    width: 200px;
}

Float Left Example:

Image

Lorem ipsum dolor sit amet, consectetur adipiscing elit. The image floats to the left, and this text wraps around it naturally. This is how magazine layouts work, with images integrated into text content. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

โžก๏ธ Float Right

Floats the element to the right side, allowing text and inline elements to wrap around its left side.

/* Float image to right */
.profile-pic {
    float: right;
    margin-left: 20px;
    margin-bottom: 10px;
    width: 150px;
    border-radius: 50%;
}

Float Right Example:

Image

Lorem ipsum dolor sit amet, consectetur adipiscing elit. The image floats to the right, and this text wraps around its left side. This creates an elegant layout where text flows naturally around visual elements. Ut enim ad minim veniam, quis nostrud exercitation.

๐Ÿ”„ Multiple Floats

Multiple floated elements stack next to each other horizontally until they run out of space.

/* Three-column layout with floats */
.column {
    float: left;
    width: 33.33%;
    padding: 10px;
}

/* Four boxes side by side */
.box {
    float: left;
    width: 25%;
    height: 100px;
}

Multiple Floats Example:

Box 1
Box 2
Box 3

โš ๏ธ The Float Collapse Problem

When all children of a container are floated, the container's height collapses to zero because floated elements are removed from normal document flow.

Problem Demonstration:

Float
Float

Notice: The red container has collapsed (very thin height) because both children are floated.

๐Ÿงน Clear Property

The clear property controls how elements behave next to floated elements. It forces an element to move below floated elements.

Clear Values:

/* Clear left floats */
.clear-left {
    clear: left;
}

/* Clear right floats */
.clear-right {
    clear: right;
}

/* Clear both left and right floats */
.clear-both {
    clear: both;  /* Most commonly used */
}

/* No clearing (default) */
.clear-none {
    clear: none;
}

Clear Example:

Float Left
Float Right
This element uses clear: both, so it appears below all floats.

๐Ÿ”ง Clearfix Technique

The clearfix is a technique to fix the float collapse problem by making the container recognize its floated children.

Method 1: Clearfix with ::after Pseudo-element (Best)

/* Modern clearfix */
.clearfix::after {
    content: "";
    display: table;
    clear: both;
}

/* Usage */
.container {
    /* container will now contain floated children */
}

<div class="container clearfix">
    <div class="float-left">...</div>
    <div class="float-right">...</div>
</div>

Method 2: Overflow Method

/* Simple overflow fix */
.container {
    overflow: auto;  /* or overflow: hidden */
}

/* This creates a new block formatting context
   which contains the floats */

Method 3: Float the Container

/* Float the parent */
.container {
    float: left;
    width: 100%;
}

/* Parent contains floated children
   but parent itself is floated */

Clearfix in Action:

Float
Float

Success! The green container now has proper height because we used overflow: auto.

๐Ÿ’ผ Practical Real-World Examples

Example 1: Article with Image

/* Magazine-style article layout */
.article-image {
    float: left;
    width: 300px;
    margin: 0 20px 20px 0;
    border-radius: 8px;
}

.article-content {
    text-align: justify;
}

.article-container::after {
    content: "";
    display: table;
    clear: both;
}

Example 2: Simple Two-Column Layout

/* Two columns with float */
.sidebar {
    float: left;
    width: 25%;
    padding: 20px;
}

.main-content {
    float: right;
    width: 75%;
    padding: 20px;
}

.container::after {
    content: "";
    display: table;
    clear: both;
}

Example 3: Card Grid

/* Card grid using floats */
.card {
    float: left;
    width: calc(33.33% - 20px);
    margin: 10px;
    padding: 20px;
    background: white;
    border: 1px solid #ddd;
}

.card-container::after {
    content: "";
    display: table;
    clear: both;
}

Example 4: Pull Quote

/* Pull quote floated to side */
.pull-quote {
    float: right;
    width: 40%;
    margin: 0 0 20px 20px;
    padding: 20px;
    background: #f5f5f5;
    border-left: 4px solid #667eea;
    font-style: italic;
    font-size: 1.2em;
}

Example 5: Button Group

/* Buttons floated in a row */
.button-left {
    float: left;
}

.button-right {
    float: right;
}

.button-group::after {
    content: "";
    display: table;
    clear: both;
}

๐Ÿ†š Float vs Modern Layouts

When to Use Float:

  • โœ… Text wrapping around images
  • โœ… Pull quotes and side notes
  • โœ… Magazine-style layouts
  • โœ… Legacy browser support needed

When to Use Flexbox/Grid Instead:

  • โœ… Modern page layouts
  • โœ… Navigation menus
  • โœ… Card grids
  • โœ… Centering and alignment
  • โœ… Responsive designs
  • โœ… Complex multi-column layouts
/* Float for text wrapping (GOOD) */
img {
    float: left;
    margin-right: 20px;
}

/* Flexbox for navigation (BETTER) */
nav {
    display: flex;
    justify-content: space-between;
}

/* Grid for page layout (BETTER) */
.page {
    display: grid;
    grid-template-columns: 250px 1fr;
}

๐ŸŽจ Live Examples

Float with Text Wrapping:

Floated

This is a demonstration of how floating works with text. The colored box on the left is floated, and this text wraps around it naturally. This technique is commonly used in blogs, articles, and magazine-style layouts where you want images integrated with text content. Notice how the text flows around the floated element smoothly.

Multiple Floats Side by Side:

Column 1
Column 2
Column 3

๐Ÿ“ Test Your Knowledge - MCQ

Q1: What does the float property do?

Q2: What is the clearfix technique used for?

Q3: Which clear value clears both left and right floats?

Q4: What is the modern alternative to floats for layouts?

Q5: When should you still use floats today?

๐Ÿ’ช Practice Questions

  1. Create an image that floats to the left with 20px right margin so text wraps around it.
  2. Write the clearfix code using ::after pseudo-element to fix container collapse.
  3. Create a two-column layout: sidebar (25% width, float left) and main content (75% width, float right).
  4. Add a clear: both element below floated elements to push content below them.
  5. Fix a collapsed container using the overflow method instead of clearfix.

๐Ÿ“Œ Summary

  • float: Removes element from flow, shifts left or right
  • float: left: Floats element to left, text wraps on right
  • float: right: Floats element to right, text wraps on left
  • Float Collapse: Container height becomes zero when all children float
  • clear: Forces element below floats (left, right, both)
  • clear: both: Most commonly used, clears all floats
  • Clearfix: ::after { content: ""; display: table; clear: both; }
  • Overflow Fix: overflow: auto on parent contains floats
  • Best Use: Text wrapping around images and pull quotes
  • Modern Alternative: Use Flexbox/Grid for layouts
  • Legacy: Understanding floats helps with older codebases
โ† Back to Content Next Topic: Navigation Bar โ†’