Lesson 31 / الدرس 31

float & clear / خاصيتا float و clear

Floating an image so text wraps around it, using clear to stop the wrap, the collapsed-parent problem — and an honest look at why float is no longer how pages are laid out.

جعل صورة عائمة ليلتف النص حولها، واستخدام clear لإيقاف الالتفاف، ومشكلة انهيار العنصر الأب — ونظرة صادقة إلى سبب أن float لم تعد الطريقة التي تُبنى بها الصفحات.

Before Flexbox and Grid existed — the tools you are about to meet next in this course — float was how developers built entire page layouts, pushing whole columns to one side of the page with hacks and workarounds. That job is over; nobody should reach for float to lay out a page today. What survives is one honest, still-useful job: pulling a single element, usually an image, to one side and letting text wrap around it — and you will keep meeting float in code written before Flexbox and Grid took over, so it is worth twenty minutes of your time.

float: pull to a side, let text wrap

img {
    float: left;
    margin-right: 1rem;
}
float: left pulls the image to the left edge of its container and takes it out of the normal flow — but unlike display: none, it does not disappear: the paragraph text that follows simply wraps around its remaining box instead of starting below it. float: right does the same thing from the other edge.

clear: stopping the wrap

h2 {
    clear: both;
}
Without clear, a heading right after a floated image would try to wrap alongside it too, squeezed into the leftover space next to it. clear: both tells an element to ignore floats completely and start below them instead — left and right clear only a float on that one side.

The collapsed-parent problem

Because a floated element leaves the normal flow entirely, its height stops counting toward its parent's height — from the parent's point of view, a container holding only floated children looks empty, exactly zero pixels tall. Any border or background color you put on that parent collapses along with it, seeming to disappear even though the floated content is still clearly visible on the page. This is one of the oldest, most confusing bugs in CSS, and it is the main reason float earned its bad reputation.

.gallery {
    border: 2px solid #4f8cff;
    overflow: auto;
}
A gallery holding two floated boxes. Delete overflow: auto from .gallery and watch its blue border collapse to a thin line above the floats, as if it had no children at all — add it back and the parent "contains" its floats again, the same overflow property from two lessons ago, reused for an entirely different problem.

Keep float for the one job it still does honestly: pulling a single image or pull-quote to one side of a block of text. For laying out whole pages — columns, navigation bars, card grids — reach for Flexbox or Grid, the tools built for exactly that job, coming up next in this course. You will still meet float reading older CSS, which is exactly why it is worth knowing rather than worth using.

  • Still the right tool: wrapping body text around a single image or pull-quote
  • No longer the right tool: multi-column page layout — that job belongs to Flexbox and Grid now
  • Still worth recognizing: a huge amount of CSS written before Flexbox existed still uses it for layout

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. What happens to the paragraph text right after a floated image?

2. A container holds only floated children and its border seems to have vanished. What is the likely cause?

3. Which value of clear pushes an element below floats on both sides?

Your task / مهمتك

Build a short article intro: float an image to the left of a paragraph so the text wraps around it, add a heading below that uses clear: both to start on its own line, and stop the container's border from collapsing by adding the right overflow value to it.

ابنِ مقدمة مقالة قصيرة: اجعل صورة عائمة إلى يسار فقرة نص ليلتف النص حولها، وأضف عنوانًا أسفلها يستخدم clear: both ليبدأ في سطره الخاص، وأوقف انهيار حدود الحاوية بإضافة قيمة overflow المناسبة لها.

  • The image uses float: left (or right) with a margin so text does not touch it الصورة تستخدم float: left (أو right) مع هامش حتى لا يلامسها النص
  • The heading below uses clear: both and starts on a clean new line العنوان أسفل يستخدم clear: both ويبدأ في سطر جديد نظيف
  • The container's border no longer collapses around the floated content حدود الحاوية لم تعد تنهار حول المحتوى العائم
How do you want to submit? / كيف تريد التسليم؟