Containing Floats with flow-root

suggest change

A floated child is taken out of normal block flow, so a parent that contains only floats may not grow to enclose it. Modern CSS can establish a new block formatting context explicitly:

.media {
  display: flow-root;
}

.media > img {
  float: inline-start;
  margin-inline-end: 1rem;
  margin-block-end: 0.5rem;
}

display: flow-root contains the float without clipping content, creating scrollbars, inserting generated content, or relying on historical browser hacks.

Older clearfix rules used ::after { content: ""; display: table; clear: both; }, and some added zoom: 1 for Internet Explorer's former hasLayout behavior. Those workarounds are unnecessary for modern browsers.

For general page or component layout, use Grid or Flexbox instead of floats. Floats remain appropriate when content should wrap around an image or shape.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents