skip to content

In CSS, when you set float: left on an image, what happens to the block boxes that follow it and to the text inside them?

level: middleimportance: should knowfreq 50%

answer

  1. out of flow for blocks, not for lines
  2. backgrounds slide underneath
  3. line boxes shorten around the margin box
  4. wrap ends past the float's bottom
  5. float blockifies display

basics

~20 s

A float is only partly removed from flow. Following block boxes are laid out as if it were not there, so their backgrounds and borders slide underneath it, but the line boxes inside them shorten to avoid the float's margin box, which is what makes text wrap.

solid answer

~50 s

Floats are removed from flow for block-level layout but not for inline content, which is why they behave unlike anything else. The floated image is shifted to the left edge of its containing block, and the block boxes after it are positioned exactly as if the float did not exist — so a following paragraph's background and border run underneath the image. What changes is the paragraph's **line boxes**: each one is shortened so it does not overlap the float's *margin* box, and once the lines clear the bottom of the float they go back to full width. That gap between image and text is `margin-right` on the float, not padding on the text. Float also blockifies the element — `display: inline` computes to `block` — and it is ignored entirely on flex items, grid items, and absolutely positioned boxes.

code

css · 10 lines
css
img.avatar {
  float: left;
  width: 96px;
  margin-right: 1rem;
}

p.bio {
  background: #eeeeee;
  border: 1px solid #999999;
}

go deeper

for a junior

Be ready to state plainly that float: left pulls the box to the left edge and lets following text wrap beside it, and that the space between them comes from the float's own margin.

for a middle

Explain the two halves precisely: block boxes are positioned as if the float were absent, while their line boxes are shortened around the float's margin box until they clear its bottom.

for a senior

Use the rule to diagnose real symptoms — a background painting under an image, a column dropping at one viewport width — and say when to keep the float versus move the layout to flex or grid.

for a principal

Be able to argue where wrapping-around-content is still a genuine requirement in a design system versus where it is a legacy pattern that should be replaced wholesale.

## "Partially out of flow" is the whole idea Every other out-of-flow mechanism in CSS is all-or-nothing: an absolutely positioned box is invisible to its surroundings, full stop. A float is the odd one. It is removed from flow *for block-level layout* and still present *for inline layout*. Hold those two halves apart and every float surprise becomes predictable. ```css img.avatar { float: left; margin-right: 1rem; } p.bio { background: #eee; } ``` ## Half one: block boxes ignore it The paragraph after the float is positioned as if the float were not in the document. Its top border edge sits where it would have sat anyway, and its background paints the full width of the containing block — **behind** the floated image. Give the paragraph a border and you will see the border line pass under the image. Nothing about the float shrinks the block box. This is also why a container of pure floats has nothing to size against, and why long-lived float layouts always came with a containment trick. ## Half two: line boxes avoid it Inside that full-width paragraph, each line box is shortened so it does not intersect the float's **margin box**. Line one starts to the right of the image and is narrower; so does line two; and the first line whose position is below the float's bottom margin edge goes back to the paragraph's full width. Rendered together, the shortened lines read as text wrapping around the image. Two consequences fall straight out of this: - The gutter between image and text is the float's own `margin-right` (`margin-left` for `float: right`). Padding on the paragraph cannot produce it, because the paragraph is full width already. - Nothing wraps "around" the bottom of the float. The wrap ends the instant the lines drop past it. ## What float does to the floated box itself - It is shifted as far to the left (or right) as it fits inside its containing block's content edge, respecting earlier floats. - Its `display` is **blockified**: `inline` computes to `block`, `inline-flex` to `flex`, `inline-grid` to `grid`. That is why a floated `<span>` suddenly honours `width` and `height`. - A float with `width: auto` shrinks to fit its content rather than filling the line, so floated boxes usually need an explicit width in layout code. ## Multiple floats Floats in the same direction stack along that edge until the remaining space is too narrow, then the next one drops to a new line below the tallest earlier float. This is the classic "float drop" or staircase effect: a single column that is one pixel too wide, or one column that is taller than the rest, and the grid falls apart at a specific viewport width. There is no equal-height behaviour and no distribution of leftover space — two of the reasons floats lost the layout job. ## Where float is simply ignored ```css .flex-parent { display: flex; } .flex-parent > img { float: left; } /* no effect */ ``` `float` does not apply to flex items or grid items — the parent's layout model owns their placement. It is also ignored on absolutely positioned and fixed-position boxes, where the computed value of `float` becomes `none`. If a float "stopped working", one of these three is usually why. ## Interview framing A strong answer names both halves in one breath — "block boxes pretend it isn't there, line boxes make room for it" — and then uses that to explain a concrete symptom: the background running under the image, the gutter coming from the float's margin, the wrap stopping at the float's bottom edge. A weak answer says only "it makes text wrap", which is the visible effect without the rule that produces it.

  • How would you add space between a floated image and the text wrapping beside it?
    Put `margin-right` on the float itself (`margin-left` for `float: right`). The line boxes avoid the float's margin box, so the float's own margin is what pushes the text away. Padding on the paragraph does nothing useful here, because the paragraph is already full width and starts to the left of the image.
  • Why does a floated span suddenly accept width and height when an inline span does not?
    Because floating blockifies the box: a computed `display: inline` becomes `block` when `float` is not `none`. As a block box it takes `width`, `height`, and vertical margins normally. The same blockification maps `inline-flex` to `flex` and `inline-grid` to `grid`.
  • A designer reports that a floated column jumps below the others at one specific window width. What is happening?
    Float drop. Floats stack along the edge only while they fit; when the sum of widths, margins, and borders exceeds the containing block's content width — often by a fraction of a pixel from percentage rounding — the next float wraps to a new line below the tallest earlier float. Flex or grid with `gap` removes the failure mode.

saying these in an interview costs you the question

  • Says the following paragraph is shrunk to the space beside the float
  • Thinks the gutter comes from padding on the text
  • Believes floats are removed from flow exactly like absolute boxes
  • Expects text to wrap around the bottom of the float
  • Cannot say why a floated inline element accepts width

context