# Float, Clear & Legacy Display

> Master magazine-style text wrapping and understand the crucial difference between hiding an element versus removing it entirely.

Subject: CSS | Lesson 19 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/css/float-clear-and-legacy
Previous: [Z-index & Stacking Context](https://codewithanimation.in/css/z-index-and-stacking.md) | Next: [Flexbox Fundamentals](https://codewithanimation.in/css/flexbox-fundamentals.md)

## What it is

Before Flexbox and Grid existed, developers used the `float` property to build entire website layouts. Today, `float` has returned to its original, intended purpose: making text wrap around an image (like a magazine article).

We also use legacy display properties like `display: none` (which completely removes an element from the layout) and `visibility: hidden` (which hides the element but leaves a gaping hole where it used to be).

## Why it matters

Even though we don't build full layouts with floats anymore, you will inevitably inherit older codebases that do. Furthermore, `float` is still the *only* way to achieve genuine text-wrapping around an object. Understanding how to 'clear' a float is essential to prevent layouts from breaking.

## How it works

1. **Float**: Applying `float: left` pushes an element to the left edge of its container, and forces inline elements (like text) to wrap around its right side.

2. **Clear**: If you want a specific element to drop *below* the floated object rather than wrapping around it, you apply `clear: left` (or `clear: both`).

3. **Display None**: `display: none` turns the element off completely. The browser acts as if it doesn't exist.

4. **Visibility Hidden**: `visibility: hidden` makes the element invisible, but it still takes up its exact physical dimensions on the page.

## Common mistake: The Collapsing Container

The most infamous issue with floats is the 'collapsing container'. If a parent element contains *only* floated children, the parent's height collapses to zero.

Because floated elements are taken out of the normal vertical flow, the parent thinks it is empty. To fix this, developers use a 'clearfix' hack (or simply set the parent to `display: flow-root`).

## Tip: Accessibility warning

Be careful with `display: none` and `visibility: hidden`—both of these properties completely hide the content from screen readers! If you want to hide something visually but keep it available for assistive technology, you must use a 'sr-only' (screen-reader only) utility class that visually clips the element without hiding it.

## Try it: Try it

Examine how the text wraps around the floated image. Then, compare what happens to the layout when a block is set to `display: none` versus `visibility: hidden`.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/float-clear-and-legacy

## Check yourself

### Question 1

What was the original, intended purpose of the `float` property in CSS?

- A. To wrap text around images
- B. To center elements horizontally
- C. To build multi-column grid layouts

Correct answer: A

Float was designed to replicate print/magazine layouts where a block of text flows around an image.

- Why not B: Floats cannot center elements; they only push left or right.
- Why not C: While developers hacked it to build grids for a decade, its true intended purpose was text wrapping.

### Question 2

What happens to a parent container if all of its children are set to `float: left`?

- A. Its height collapses to zero
- B. It expands to fill the screen
- C. It automatically clears the floats

Correct answer: A

Because floated elements are removed from normal vertical flow, the parent container acts as if it has no content, causing its height to collapse to 0.

- Why not B: It shrinks, not expands.
- Why not C: Parents do not automatically clear floats unless you apply a clearfix or display: flow-root.

### Question 3

What is the primary difference between `display: none` and `visibility: hidden`?

- A. visibility: hidden leaves an empty physical space where the element was
- B. display: none leaves an empty physical space
- C. They do exactly the same thing

Correct answer: A

Visibility: hidden turns the element invisible but preserves its width and height in the layout. Display: none removes the element entirely, as if it was deleted from the HTML.

- Why not B: display: none completely removes the element, causing surrounding elements to fill its space.
- Why not C: One leaves a hole, the other doesn't.

## Remember this

- `float` pushes an element left/right and wraps text around it.
- `clear: both` forces an element to drop below all floated items.
- A parent containing only floated children will collapse to zero height.
- `display: none` removes the element entirely.
- `visibility: hidden` hides the element but leaves a physical empty space.
