SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
CSS
1. CSS Tutorial for Beginners — Learn CSS from Scratch 2. CSS Syntax Explained — Selectors, Properties & Values 3. CSS Selectors Tutorial — Class, ID, Attribute & More 4. How to Add CSS to HTML — Inline, Internal, External 5. CSS Colors Tutorial — HEX, RGB, RGBA, HSL Explained 6. CSS Backgrounds Tutorial — Images, Size & Position 7. CSS Borders Tutorial — Style, Width, Radius & Shadow 8. CSS Margin Explained — Shorthand, Auto & Collapse 9. CSS Padding Explained — Shorthand & Margin Difference 10. CSS Height and Width — px, %, em, rem, vw, vh Units 11. CSS Box Model Explained — Content, Padding, Border 12. CSS Text Styling Tutorial — Color, Align & Spacing 13. CSS Fonts Tutorial — font-family, Sizes & Google Fonts 14. CSS Link Styling — Hover, Visited, Active & Buttons 15. CSS Lists Styling — Markers, Counters & Shorthand 16. CSS Tables Styling — Zebra Stripes & Hover Rows 17. CSS Display Property — Block, Inline, None Explained 18. CSS Position Explained — Relative, Absolute, Fixed 19. CSS Overflow Explained — Hidden, Scroll, Auto & More 20. CSS Float and Clear — Layouts & Clearfix Trick 21. CSS Pseudo-classes — Hover, Focus, nth-child & More 22. CSS Pseudo-elements — ::before, ::after & ::first-line 23. CSS Form Styling — Inputs, Labels & Buttons Tutorial 24. CSS Navigation Bar Tutorial — Dropdown Menu Included 25. CSS Website Layout Tutorial — Header, Sidebar, Footer 26. CSS Flexbox Tutorial — Complete Guide with Examples 27. CSS Grid Tutorial — Columns, Rows & Layouts Guide 28. CSS Responsive Design — Media Queries Tutorial 29. CSS Transitions & Animations — No JavaScript Needed 30. CSS Variables Tutorial — :root, var() & Theming 31. CSS Transforms Explained — Rotate, Scale, Translate 32. CSS Filters Tutorial — Blur, Grayscale, Sepia & More 33. CSS Text Effects — Shadows, Wrapping & Overflow 34. CSS Keyframes Tutorial — Bouncing Ball Animation 35. CSS Grid Template Areas — Layout in Words, Not Numbers
Final Test — Certificate ₹10
Try this code live
Lesson 20 of 35

CSS Float and Clear — Layouts & Clearfix Trick

4 min read · Free · No login needed

Before flexbox and grid existed, float was how whole page layouts were built. Today it has a smaller job, but it is still the right tool for one thing: letting text wrap around an image, like in a newspaper. A photo on the left, the article text flowing around it on the right.

Diagram — see how it fits together
Diagram: CSS Float

When you float an element, it moves to one side of its container and the following content flows around it. The element is taken out of the normal flow — and that causes one famous problem, which this lesson also fixes.

Float left and float right

float: left pushes the element to the left side of its container, and the text wraps around its right side. float: right does the mirror image. The classic example is a picture in an article: float it left, and the paragraph text sits neatly beside it.

.photo {
  float: left;
  width: 140px;
  margin: 0 16px 8px 0;
}

Clear — push below the floats

A floated element keeps pulling the next content up beside it. When you want something to stop wrapping and go back to a full line — a footer, for example — you give it clear: both. It moves down below every floated element, left and right, and starts on a fresh line.

.footer {
  clear: both;
}

The collapsing parent problem

Here is the famous problem. A parent box that contains only floated children collapses to zero height — it acts as if the children are not there at all, because floated elements are out of the normal flow. The parent's background and border shrink to nothing, and the next section slides up underneath the floats.

Clearfix — the standard fix

The standard fix is called a clearfix. You add a tiny invisible element after the floats, using the ::after pseudo-element, and clear it. The parent then stretches to wrap its floated children again. It is three lines of CSS, and you will use it on every float layout.

.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

Float layout — two columns

With float and clear you can build a simple two-column layout: one column floats left, the other floats right, and the footer clears both. It is the oldest layout trick on the web, and it still works. For new layouts, flexbox and grid are easier — you will meet them later in this course — but float is the tool for wrapping text around images.

Note: A floated element is out of the normal flow, so its parent does not see its height. If your parent box has collapsed to a thin line, the floats inside it are almost certainly the cause — add the clearfix class to the parent.
Tip: Always give a floated image a margin on the side the text wraps around — margin: 0 16px 8px 0 for a left-floated image. Without it, the text touches the picture and the page looks cramped.

Common mistakes

  • Forgetting to clear floats, so the footer or the next section slides up beside them.
  • Letting the parent collapse to zero height and wondering where its background colour went.
  • Floating every element on the page instead of using flexbox or grid for real layouts.
  • Forgetting the margin between a floated image and the text wrapping around it.
  • Using clear: left when there are also right-floated elements — use clear: both to be safe.

Remember

  • float: left and float: right move an element to one side; text wraps around the other side.
  • clear: both pushes an element below all floated elements.
  • A parent with only floated children collapses to zero height — fix it with the clearfix.
  • The clearfix is ::after with content: "", display: table and clear: both.
  • Float is best for wrapping text around images; use flexbox or grid for page layouts.

Example explained

  • .photo { float: left; width: 140px; margin: 0 16px 8px 0; } — the orange CHAI box floats to the left, and the welcome text wraps around its right side with a 16px gap.
  • .clearfix::after { content: ""; display: table; clear: both; } — the clearfix makes the wrapper stretch around its floated children instead of collapsing.
  • .menu { float: left; width: 45%; } — the menu column floats to the left side of the page.
  • .offers { float: right; width: 45%; } — the offers column floats to the right, so the two columns sit side by side.
  • .menu, .offers { box-sizing: border-box; } — the padding is counted inside the 45% width, so the two columns really do fit on one line.
  • .footer { clear: both; } — the footer moves below both floated columns and starts on a fresh full-width line.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>Float Layout — Chai Shop</title>
<style>
body {
  font-family: Arial, sans-serif;
  margin: 0;
}
.header {
  background-color: #7c2d12;
  color: white;
  padding: 16px;
  text-align: center;
}
.photo {
  float: left;
  width: 140px;
  height: 140px;
  background-color: #f59e0b;
  color: white;
  text-align: center;
  line-height: 140px;
  margin: 0 16px 8px 0;
  border-radius: 8px;
}
.clearfix::after {
  content: "";
  display: table;
  clear: both;
}
.menu {
  float: left;
  width: 45%;
  box-sizing: border-box;
  background-color: #fef3c7;
  padding: 12px;
}
.offers {
  float: right;
  width: 45%;
  box-sizing: border-box;
  background-color: #dcfce7;
  padding: 12px;
}
.footer {
  clear: both;
  background-color: #1e3a5f;
  color: white;
  text-align: center;
  padding: 12px;
}
</style>
</head>
<body>

<div class="header"><h1>Chai &amp; Snacks Shop</h1></div>

<div class="clearfix">
<div class="photo">CHAI</div>
<p>Welcome to our little chai shop, next to the school gate. We serve hot masala chai, fresh samosas and bun maska from 7 in the morning.</p>
<p>Students get a special price before 9 AM. Bring your friends, sit a while, and start the day with a hot cup.</p>
</div>

<div class="clearfix">
<div class="menu">
<h2>Menu</h2>
<p>Masala Chai — Rs 10</p>
<p>Samosa — Rs 10</p>
<p>Bun Maska — Rs 15</p>
</div>
<div class="offers">
<h2>Today's Offers</h2>
<p>Chai + Samosa — Rs 15</p>
<p>Buy 2, get 1 free — before 9 AM</p>
<p>Free elaichi chai on your birthday</p>
</div>
</div>

<div class="footer">Open 7 AM – 9 PM, every day.</div>

</body>
</html>

Output

Output — how it looks in the browser
Output of CSS Float and Clear — Layouts & Clearfix Trick

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. What is float best used for today?

Letting text wrap around an image, like in a newspaper. Use flexbox or grid for page layouts.

2. What does clear: both do?

It pushes an element below all floated elements.

3. Why does a parent of floated children collapse?

Floated children leave the normal flow, so the parent thinks it is empty and shrinks to zero height. The clearfix fixes it.

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

CapCut Mobile Video Editing Course Beginner to Advance
₹99
View
Summer Industrial Training in MERN Stack 2026
₹3,000
View
Ethical Hacking & Cyber Security Workshop
View
Browse all courses
CSS — Lessons
1. CSS Tutorial for Beginners — Learn CSS from Scratch 2. CSS Syntax Explained — Selectors, Properties & Values 3. CSS Selectors Tutorial — Class, ID, Attribute & More 4. How to Add CSS to HTML — Inline, Internal, External 5. CSS Colors Tutorial — HEX, RGB, RGBA, HSL Explained 6. CSS Backgrounds Tutorial — Images, Size & Position 7. CSS Borders Tutorial — Style, Width, Radius & Shadow 8. CSS Margin Explained — Shorthand, Auto & Collapse 9. CSS Padding Explained — Shorthand & Margin Difference 10. CSS Height and Width — px, %, em, rem, vw, vh Units 11. CSS Box Model Explained — Content, Padding, Border 12. CSS Text Styling Tutorial — Color, Align & Spacing 13. CSS Fonts Tutorial — font-family, Sizes & Google Fonts 14. CSS Link Styling — Hover, Visited, Active & Buttons 15. CSS Lists Styling — Markers, Counters & Shorthand 16. CSS Tables Styling — Zebra Stripes & Hover Rows 17. CSS Display Property — Block, Inline, None Explained 18. CSS Position Explained — Relative, Absolute, Fixed 19. CSS Overflow Explained — Hidden, Scroll, Auto & More 20. CSS Float and Clear — Layouts & Clearfix Trick 21. CSS Pseudo-classes — Hover, Focus, nth-child & More 22. CSS Pseudo-elements — ::before, ::after & ::first-line 23. CSS Form Styling — Inputs, Labels & Buttons Tutorial 24. CSS Navigation Bar Tutorial — Dropdown Menu Included 25. CSS Website Layout Tutorial — Header, Sidebar, Footer 26. CSS Flexbox Tutorial — Complete Guide with Examples 27. CSS Grid Tutorial — Columns, Rows & Layouts Guide 28. CSS Responsive Design — Media Queries Tutorial 29. CSS Transitions & Animations — No JavaScript Needed 30. CSS Variables Tutorial — :root, var() & Theming 31. CSS Transforms Explained — Rotate, Scale, Translate 32. CSS Filters Tutorial — Blur, Grayscale, Sepia & More 33. CSS Text Effects — Shadows, Wrapping & Overflow 34. CSS Keyframes Tutorial — Bouncing Ball Animation 35. CSS Grid Template Areas — Layout in Words, Not Numbers
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →