🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Float

AI & DATA SCIENCE // float

The float property removes an element from normal flow and shifts it to the left or right side of its container, allowing text and inline content to wrap around it.

Syntax

float: left | right | none;

Deep Dive Course

Historically, float was the primary tool for building multi-column page layouts before flexbox and grid existed, but today it's mainly used for its original, narrower purpose: letting an image or other element sit to one side while text wraps naturally around it, similar to how images are placed in a printed magazine or newspaper column. A floated element is taken out of normal document flow, which can cause its parent container to collapse to zero height if all of that parent's children are floated, since the parent no longer sees any in-flow content to size itself against, a classic float-related bug typically fixed with a clearfix technique or by giving the parent a modern layout context like display: flow-root.

1Understanding Float

Historically, float was the primary tool for building multi-column page layouts before flexbox and grid existed, but today it's mainly used for its original, narrower purpose: letting an image or other element sit to one side while text wraps naturally around it, similar to how images are placed in a printed magazine or newspaper column. A floated element is taken out of normal document flow, which can cause its parent container to collapse to zero height if all of that parent's children are floated, since the parent no longer sees any in-flow content to size itself against, a classic float-related bug typically fixed with a clearfix technique or by giving the parent a modern layout context like display: flow-root.

💡

For actual page layout, columns, grids, and overall page structure, use flexbox or grid instead of float — float today is best reserved specifically for its original purpose of wrapping text around an image or similarly floated element.

editor.html
img {
  float: left;
  margin-right: 16px;
}
localhost:3000

2Practical Example

Here is a real-world application of Float showing how it is used in production CSS code.

editor.html
.parent {
  /* no explicit height or layout context */
}
.child-one, .child-two {
  float: left;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Float:

1. Reserve float specifically for wrapping text around an image or similar element, rather than for building overall page layout, which flexbox or grid handle far more reliably today

2. Apply display: flow-root, or a clearfix technique, to a parent container whose height is collapsing because all of its children are floated

3. Use the clear property on an element that should stop wrapping and instead move below a preceding floated element entirely

⚠️

Tip: For actual page layout, columns, grids, and overall page structure, use flexbox or grid instead of float — float today is best reserved specifically for its original purpose of wrapping text around an image or similarly floated element.

editor.html
img {
  float: left;
  margin-right: 16px;
}
localhost:3000

Examples

Example 01Basic Usage
img {
  float: left;
  margin-right: 16px;
}
Example 02Advanced Example
.parent {
  /* no explicit height or layout context */
}
.child-one, .child-two {
  float: left;
}

Best Practices

  • Reserve float specifically for wrapping text around an image or similar element, rather than for building overall page layout, which flexbox or grid handle far more reliably today
  • Apply display: flow-root, or a clearfix technique, to a parent container whose height is collapsing because all of its children are floated
  • Use the clear property on an element that should stop wrapping and instead move below a preceding floated element entirely

Interview Question

Why can a parent container collapse to zero height when every one of its child elements is floated, even though the children themselves are still clearly visible?

Hint: Think about what a parent container's height is normally calculated based on, and whether a floated element still counts as part of that calculation.

A parent container's height, when not explicitly set, is normally calculated automatically based on the combined height of its in-flow children, the elements that participate in normal document flow — floating an element specifically removes it from that normal flow entirely, which means the parent no longer counts a floated child's height as contributing to its own automatic height calculation at all, even though the floated child is still rendered and clearly visible on the page. If literally every child inside a parent has been floated, the parent is left with no remaining in-flow content whatsoever to base its automatic height calculation on, so it collapses down to zero, or near-zero, height, visually appearing to have no content at all even though its floated children are still fully visible, just no longer counted for the purposes of determining their former parent's own height. This exact scenario, a collapsing parent around floated children, is precisely the classic bug that clearfix techniques, or the modern display: flow-root property, are specifically designed to fix, by giving the parent a way to properly account for its floated children's height again.

Exercises

MediumPractice using Float in a real scenario.
View Solution
img {
  float: left;
  margin-right: 16px;
}

Frequently Asked Questions

Why can a parent container collapse to zero height when every one of its child elements is floated, even though the children themselves are still clearly visible?

A parent container's height, when not explicitly set, is normally calculated automatically based on the combined height of its in-flow children, the elements that participate in normal document flow — floating an element specifically removes it from that normal flow entirely, which means the parent no longer counts a floated child's height as contributing to its own automatic height calculation at all, even though the floated child is still rendered and clearly visible on the page. If literally every child inside a parent has been floated, the parent is left with no remaining in-flow content whatsoever to base its automatic height calculation on, so it collapses down to zero, or near-zero, height, visually appearing to have no content at all even though its floated children are still fully visible, just no longer counted for the purposes of determining their former parent's own height. This exact scenario, a collapsing parent around floated children, is precisely the classic bug that clearfix techniques, or the modern display: flow-root property, are specifically designed to fix, by giving the parent a way to properly account for its floated children's height again.

Related Functions

ClearPositionDisplay-flex