CSS Tutorial
CSS Float
float wraps text around a box. Clear and a clearfix still matter on old layouts.
Text around a box
float pulls a box to the left or right. Later content in the same block wraps beside it. Newspapers used this for a photo next to a paragraph. Harbor studio still uses it for a small tide chip beside a notice.
A floated box leaves normal flow for wrapping, but it is not quite the same as position: absolute. It still belongs to its parent. If every child is floated, the parent can collapse to zero height — that is why clear and a clearfix exist.
float: left and right
left tucks the box to the start of the line. right tucks it to the end. Text that follows fills the remaining side. A little margin keeps the wrap from kissing the chip.
Example
<style>
.chip {
float: left;
width: 5.5rem;
margin: 0 0.75rem 0.5rem 0;
padding: 0.6rem 0.5rem;
background: teal;
color: white;
text-align: center;
}
.notice {
color: #475569;
}
</style>
<p class="notice">
<span class="chip">Slip 4</span>
Harbor studio opens 8–16 on the north quay. The sky gallery tour leaves from this slip. Bring a jacket
if the teal dock is wet. Walk-ins wait at the lantern desk after 13:00.
</p>clear stops the wrap
clear: left, right, or both tells a box not to sit beside a float. It drops below the float instead. Use both when you do not care which side the float used.
Example
<style>
.photo {
float: right;
width: 6rem;
height: 4rem;
margin: 0 0 0.5rem 0.75rem;
background: #e0f2fe;
border: 2px solid teal;
color: #0369a1;
text-align: center;
line-height: 4rem;
}
.hours {
clear: both;
background: #ccfbf1;
color: teal;
padding: 0.5rem 0.75rem;
}
</style>
<p class="photo">Sky</p>
<p>The sky gallery faces the water. Seats are unnumbered.</p>
<p class="hours">Hours 8–16 — this bar clears the float.</p>Clearfix when the parent collapses
If a parent contains only floats, its height can collapse and the next section slides up. A clearfix forces the parent to wrap those floats. The modern one-liner is display: flow-root. Older sheets use an ::after with clear: both, or overflow: auto.
Example
<style>
.row {
display: flow-root;
background: #e0f2fe;
border: 2px solid teal;
padding: 0.75rem;
}
.berth {
float: left;
width: 6rem;
margin-right: 0.5rem;
padding: 0.5rem;
background: teal;
color: white;
}
</style>
<div class="row">
<div class="berth">Slip 4</div>
<div class="berth">Slip 7</div>
</div>
<p>This sentence sits below the row because the parent contains the floats.</p>Float versus modern layout
| Job | Old tool | Prefer now |
|---|---|---|
| Text beside a photo | float | Still fine, or a small grid |
| Two columns of cards | Floated divs plus clearfix | Flex or grid with gap |
| Full-page layout | Floated sidebar | Grid template areas |
| Vertical centering | Not what float is for | Align with flex |
In /css/try, change float: left to right, then addclear: both on the last paragraph. Notice how wrap and drop are two separate controls.
Worked examples
The short listings above show one property. These sheets paint documents you would actually ship: a lab dashboard, a clinic form, a marks table, a nav bar.
CSS does not invent meaning. HTML already said what the pieces are. Cascade, specificity, and the box model decide how they look. The numbers are classroom values — current, pH, pulse, 3-4-5 — so the style has something real to sit on.
Preview them in the CSS editor at /css/try. Change one property and watch the page paint. The tags stay the same.
Physics
Text wrapping a strobe still
float: left pulls the image out of normal flow; following text wraps. clear: both on a later block stops the wrap. Flex and grid replaced most page-level floats.
A photo beside a method is the remaining good use. Do not float a whole layout.
Example
<style>
img { float: left; margin: 0 0.6rem 0.4rem 0; }
</style>
<img src="https://picsum.photos/seed/drop/64/64" alt="Drop still" width="64" height="64">
<p>s = ½ g t². The still sits in the margin of the method.</p>Chemistry
A bottle sketch beside a warning
Same pattern: small image, wrapping prose. After the wrap, a clearing element (or overflow: auto on the parent) contains the float.
Example
<style>
img { float: left; margin-right: 0.5rem; }
</style>
<img src="https://picsum.photos/seed/flask/48/48" alt="Flask" width="48" height="48">
<p>H2SO4 — corrosive. Keep the warning in the text, not only in the picture.</p>