15. Float, Clear & Legacy Clearfix Layouts

🟢 Beginner

1. Definition

float: left / right shifts an element box to the left or right margin, allowing text inline content to wrap around it. clear: both forces elements below floated boxes. Clearfixes (e.g., ::after { content: ""; display: table; clear: both; }) prevent parent container height collapse.

2. Why It Is Used

Floats were originally created for magazine-style editorial text wrapping around photos. Understanding floats is essential for maintaining legacy codebases.

3. How It Works

A floated element is removed from normal block flow. Parent containers fail to detect floated children heights, causing parent height to collapse to 0px unless cleared using a clearfix or display: flow-root.

4. Syntax

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

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

5. Float & Clear Properties Table

PropertyKeyword ValuesDescription
floatleft, right, noneShifts box to left or right margin
clearleft, right, both, noneDisallows floating elements on specified sides

6. Beginner Example

img.align-left {
  float: left;
  margin-right: 12px;
}

7. Intermediate Example

/* Legacy Clearfix Pattern */
.container::after {
  content: "";
  display: table;
  clear: both;
}

.column-left { float: left; width: 50%; }
.column-right { float: right; width: 50%; }

8. Advanced Modern BFC Clearfix Example

/* Modern Block Formatting Context clear */
.modern-clearfix-container {
  display: flow-root; /* Automatically contains floated children without pseudo-elements! */
}

9. Line-by-Line Code Explanation

  • display: flow-root;: Establishes a new BFC that automatically expands container height to enclose all floated child elements.

10. Expected Terminal / Browser Output

Paragraph text wrapping cleanly around a left-aligned article image, with the parent container retaining full height.

11. Common Mistakes & Pitfalls

  • Using floats for modern page layouts: Building entire grid layouts with floats instead of modern Flexbox or CSS Grid leads to fragile clearing bugs.

12. Important Rules & Limitations

  • Floated elements automatically convert to block-level boxes (display: block).

13. Multilingual Explanation: English

float shifts elements for text wrapping. Use display: flow-root on parent containers to prevent parent height collapse.

14. Multilingual Explanation: Hindi (Roman Script)

Float se image ke aas-paas text wrap hota hai. Parent container height collapse se bachane ke liye display: flow-root ka use karein.

15. Multilingual Explanation: Marathi (Roman Script)

Text wrap karnyasathi float vapara. Parent height zero honya pasun thambvnyasathi display: flow-root vapara.

16. Practice Questions

  1. Why do parent containers collapse to 0px height when all child elements are floated?

17. Practical Coding Challenge

Write CSS for an .article-image floated left with margin-right: 20px inside a display: flow-root container.

18. Related Topics