What is float?

float pushes an element to the left or right of its container and lets inline content, such as text, wrap around it, like an image in a magazine article. It was originally designed for that wrapping effect, was misused for page layouts for years, and today is used almost only for wrapping text around media.

Values

float: left, float: right, float: none, and logical inline-start / inline-end.

What a float does

  • The element is taken out of normal flow (partially): following block boxes ignore it, but their text and inline content wrap around it.
  • A floated element becomes block-level (display computes to block), so width and height apply.
  • Its parent does not grow to contain it, which causes the famous "collapsed parent" problem.

Containing floats

  • display: flow-root on the parent (the modern, clean fix).
  • overflow: auto or hidden on the parent (side effects: clipping, scrollbars).
  • The old clearfix hack: .clearfix::after { content: ""; display: table; clear: both; }.

Modern alternatives

For layouts (columns, card rows, navbars), use Flexbox or Grid. Floats remain useful for:

  • wrapping text around images and pull quotes,
  • shape-outside (e.g. circle(50%)) to wrap text along a curved shape,
  • drop caps.

Interview tip

Say clearly that floats are no longer a layout tool and mention display: flow-root; that signals current knowledge.

Code example

html
1<style>
2 body { font-family: system-ui; max-width: 520px; }
3 .article { border: 2px solid #0b1020; padding: 12px; }
4 .contain { display: flow-root; } /* parent now wraps the float */
5 .avatar {
6 float: left;
7 width: 90px; height: 90px;
8 margin: 0 14px 6px 0;
9 border-radius: 50%;
10 shape-outside: circle(50%); /* text follows the curve */
11 }
12 .quote { float: right; width: 40%; margin: 0 0 8px 12px; font-size: 1.1em; font-weight: 700; color: #2b4bff; }
13</style>
14<div class="article contain">
15 <img class="avatar" src="https://picsum.photos/id/64/180/180" alt="Portrait of the author" width="90" height="90">
16 <p class="quote">"Floats are for wrapping, not layout."</p>
17 <p>Text flows around floated elements just like in a printed magazine. This paragraph keeps
18 going so you can see how the lines wrap around the circular image on the left and the pull quote
19 on the right. Thanks to display: flow-root, the bordered container grows to contain both floats.</p>
20</div>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app