Float and Clear ยท Astro Tech Blog

Float and Clear

float pushes an element to one side, allowing text and inline elements to wrap around it.

Demo: Float Example
HTML
<div class="float-box">Floated</div>
<p>This text wraps around the floated box. Float was originally designed for this โ€” wrapping text around images. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
CSS
.float-box {
float: left;
width: 100px;
height: 80px;
margin: 0 12px 8px 0;
background: #6366f1;
color: white;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
border-radius: 6px;
}
p { margin: 0; color: #334155; }
Live Output Window

Float Values

ValueDescription
leftFloat to the left side
rightFloat to the right side
noneNo float (default)

Clear

clear prevents elements from wrapping around floats:

Demo: Clearfix
HTML
<div class="float-box left">Left</div>
<div class="float-box right">Right</div>
<div class="cleared">Cleared โ€” starts below both floats</div>
CSS
.float-box { width: 80px; height: 60px; display: flex; align-items: center; justify-content: center; color: white; font-weight: bold; border-radius: 4px; }
.left { float: left; background: #6366f1; }
.right { float: right; background: #059669; }
.cleared {
clear: both;
margin-top: 8px;
padding: 8px;
background: #fef9c3;
border: 1px solid #f59e0b;
border-radius: 4px;
font-weight: bold;
}
Live Output Window

Clear Values

ValueStops Wrapping
leftNo wrapping on left side
rightNo wrapping on right side
bothNo wrapping on either side

Note: For modern layouts, prefer Flexbox and Grid over float. Float is best kept for its original purpose โ€” text wrapping around images.