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>

Do not start a new Harbor studio layout with floats. Useflex or grid for rows, columns, and equal-height cards. Float remains useful for wrapping text around a small figure, and for reading older CSS.

Float versus modern layout

JobOld toolPrefer now
Text beside a photofloatStill fine, or a small grid
Two columns of cardsFloated divs plus clearfixFlex or grid with gap
Full-page layoutFloated sidebarGrid template areas
Vertical centeringNot what float is forAlign 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>

FAQ: CSS Float

Common questions about this page.

What is the StudyGrid CSS tutorial?

The StudyGrid CSS tutorial is a full beginner track: selectors, the box model, text, layout, flex, grid, and responsive design. Each chapter has copy-and-preview examples.

Should I run css float examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn css float in this CSS CSS lesson (CSS Float).

Is the CSS editor the same as Try HTML?

No. Try CSS is a stylesheet preview at /css/try. Try HTML stays at /html/try. CSS lessons never open the HTML-only editor or the Python editor.

Do I need to install anything to learn CSS?

No. Open a chapter, click Try it in CSS, and the page preview updates in the browser.

Where should I start the CSS tutorial?

Start at CSS Intro, then Syntax, Selectors, and How To. After colors and the box model, continue to flex, grid, and media queries. Use Next at the bottom of each chapter.

Is the CSS tutorial free?

Yes. The CSS studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live preview editor.