CSS float and overflow: Keep Text Boxes Clear of Floats
Text lines can wrap around a float while their block still extends behind it. To make the entire block avoid the float, establish a block formatting context (BFC) with display: flow-root. On an ordinary block, overflow: auto also creates one.
Text wrapping versus box placement
In the default block, lines avoid the float and can become wider again below it. The block’s border and background may continue behind the float. A block that creates a BFC keeps its border box clear of the outside float’s margin box. If there is too little space beside the float, the block may move below. CSS 2.2 · Floats
Compare five cases in one file
Save the code as float-overflow.html and open it in a browser. The first three cases use the same paragraph with default layout, overflow: auto, and display: flow-root. The last two show whether a parent includes its own float in its height. Each .case has its own BFC to keep the experiments separate.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Independent layout beside a float</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
h1 { font-size: 24px; }
.case { display: flow-root; max-width: 500px; margin-bottom: 24px; }
.outside { float: right; width: 100px; height: 120px; margin-left: 12px; background: #168a91; }
.text { background: #edf3fa; border: 2px solid #6f88a4; padding: 10px; }
.text p, .inside p { margin: 0; }
.auto { overflow: auto; }
.flow { display: flow-root; }
.inside { padding: 8px; border: 2px solid #6f88a4; background: #edf3fa; }
.inside .tile { float: left; width: 80px; height: 100px; margin-right: 12px; background: #168a91; }
</style>
</head>
<body>
<h1>Compare float and overflow</h1>
<section class="case">
<h2>Default layout</h2>
<div class="outside" aria-hidden="true"></div>
<div class="text">
<p>The text flows around the green box. Look at where the text block’s border and background end. The same text can take more lines when its space is narrower.</p>
</div>
</section>
<section class="case">
<h2>overflow: auto</h2>
<div class="outside" aria-hidden="true"></div>
<div class="text auto">
<p>The text flows around the green box. Look at where the text block’s border and background end. The same text can take more lines when its space is narrower.</p>
</div>
</section>
<section class="case">
<h2>display: flow-root</h2>
<div class="outside" aria-hidden="true"></div>
<div class="text flow">
<p>The text flows around the green box. Look at where the text block’s border and background end. The same text can take more lines when its space is narrower.</p>
</div>
</section>
<section class="case">
<h2>Inner float: ordinary parent</h2>
<div class="inside">
<div class="tile" aria-hidden="true"></div>
<p>Short text</p>
</div>
</section>
<section class="case">
<h2>Inner float: flow-root parent</h2>
<div class="inside flow">
<div class="tile" aria-hidden="true"></div>
<p>Short text</p>
</div>
</section>
</body>
</html>
The default case’s blue border reaches behind the green float. The following two blocks avoid the float. Resize the window and compare the blue box edges as well as the lines of text.
Include an inner float in the parent’s height
In the last two cases, the ordinary parent wraps the short paragraph while the tile can stick out below. The flow-root parent includes the tile in its automatic height. Remove only the flow class from the last box, compare the heights, then restore it. To place a following element below the float, apply clear: both to that next element. CSS Display · flow-root
Choose flow-root, overflow, or clear
Use overflow: auto when overflowing content should be scrollable. hidden can clip a shadow or menu. overflow: clip alone does not create a BFC, so switching from hidden can change the layout. Use flow-root to isolate the layout without changing overflow handling. CSS Overflow · overflow




