Wrapping Text Using Float and Clear

css-float

Hi everyone!

Now that we have explored the display property in CSS, it is time to dive into another foundational concept that has shaped web design for years — the float property. Even though newer layout methods like Flexbox and Grid are now widely preferred, learning how float works still adds a lot of value. It equips us to handle older projects, debug legacy code, or create simple layout tricks quickly.

What Float Does in CSS

The float property allows an element to shift to the left or right inside its container. This movement causes surrounding content — like text or other elements — to wrap around the floated item. Float was inspired by print-style layouts and has been a common technique for aligning images next to paragraphs or creating sidebar layouts.

Despite being somewhat outdated for modern complex layouts, float still has its use cases. Understanding how it works will prepare us for a wider range of situations in web development.

Floating Images Beside Text

Let us walk through a simple example. Suppose we have this HTML:

<img src="cat.jpg" alt="Cute Cat">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod lorem at diam facilisis tincidunt...</p>

And here is the CSS to make it float:

img {
  float: left;
  margin-right: 10px;
}

The result is that the image moves to the left, and the paragraph wraps around it to the right. To float it on the other side, we could use float: right instead.

This method creates a tight visual connection between images and their corresponding text, making it especially useful for articles, profiles, or content-heavy pages.

Using Clear to Fix Layout Flow

Floated elements can sometimes cause problems in layout flow. If the next content block does not account for the float, it might appear beside the floated element instead of below it. For example:

<img src="cat.jpg" alt="Cute Cat">
<p>This is the main paragraph...</p>
<footer>Copyright 2025</footer>

Without any adjustment, the footer may float next to the image. To prevent this, we use the clear property:

footer {
  clear: both;
}

This ensures that the footer drops below the floated elements and restores the expected layout flow. Using clear: both is a simple but powerful way to maintain structure.

Building Columns with Float

We can also create simple side-by-side layouts using float. Take this example:

<div class="cat-block">Cat content goes here</div>
<div class="dog-block">Dog content goes here</div>
<footer>Footer content</footer>

And here is the CSS:

.cat-block {
  float: left;
  width: 45%;
}

.dog-block {
  float: right;
  width: 45%;
}

footer {
  clear: both;
}

This gives us two columns for Cat and Dog content placed side by side. The footer stays underneath both, spanning the entire width.

Illustration suggestion: Display two rectangular blocks labeled Cat and Dog horizontally aligned with some text or images inside. A full-width footer appears below them.

Hands-On Practice with Float and Clear

If we are working on the 8.1 CSS Float project files, here are some steps we can try to solidify what we have learned:

  1. Float the images within each content block so that text wraps around them.

  2. Apply float: left to the Cat block and float: right to the Dog block.

  3. Use clear: both on the footer to ensure proper layout structure.

Doing these exercises will give us practical insight into how float and clear work together in real web layouts.

When to Use Float in Modern CSS

While Flexbox and Grid are now the default choices for layout, float can still be the right tool in some situations:

  • Wrapping text around images in blogs or editorial layouts

  • Making lightweight layout tweaks without full redesigns

  • Working on older websites that were built before modern layout systems

Knowing when and how to use float will help us work efficiently across a broader range of projects.

Final Thoughts

Float has played a major role in the evolution of CSS layout techniques. While we may not rely on it as heavily today, having a good grasp of float and clear gives us a well-rounded understanding of how web design has progressed. It also equips us to handle legacy code or quick layout fixes confidently.

Next, we will dive into Flexbox — a more flexible and powerful system for building modern, responsive designs. Until then, keep practicing and enjoy the process of mastering CSS!

Comments

Postingan Populer

Image

Halo teman-teman! Aku mau cerita tentang salah satu project seru yang aku kerjakan: halaman marketing untuk SEO Panel . Aku nggak bikin seluruh platform-nya, tapi fokus di bagian depan website supaya setiap elemen dan kontennya bisa langsung menyampaikan nilai produk dengan jelas dan mudah dipahami. Awal Proyek SEO Panel punya platform SEO all-in-one yang keren banget di belakang layar, dengan fitur-fitur lengkap buat bantu pengguna meningkatkan peringkat website. Tapi mereka butuh halaman depan yang nggak kalah kuat: tampilannya harus menarik, langsung menjelaskan manfaat, dan bikin pengunjung mau mencoba layanan. Aku mulai dengan memahami audiens mereka: siapa penggunanya, masalah apa yang sering ditemui di SEO, dan apa yang bikin mereka tertarik. Dari situ aku bikin alur konten dan layout yang gampang ditangkap pengunjung dalam beberapa detik pertama. Proses Desain Untuk halaman ini aku pakai WordPress dan Elementor supaya desainnya fleksibel dan nggak perlu coding dari awal. Fokus...

Image

Superingan is a Blogger template designed with simplicity and functionality in mind, offering a clean and user-friendly platform for professional websites. This theme is perfect for various website types, including personal blogs, online magazines, news websites, and portfolios. Features: Responsive Design: Your website will look its best across all devices, from desktops and laptops to tablets and smartphones. Customizable Colors: Easily change the template's colors to match your branding or preferences. Custom Headers: Upload your own header image to add a personal touch to your website. Custom Menus: Create and manage your website's navigation menus with ease. Widget Areas: Add widgets to display additional content and features on your website's sidebar. Clean HTML & CSS Code: The template is built with clean and well-structured code, making it easy to learn and customize further. Superingan is an ideal choice for those seeking a professional website with a sim...

Hi everyone! Welcome back to our CSS Grid series on Dindin Design! In the previous article, we explored how to size our grid columns and rows using fr , px , minmax() , and more. Now that we know how to set up a grid, it’s time to take it to the next level — by learning how to place elements inside the grid. In this article, we’ll learn Grid Item Placement — how to position and span items across your grid using properties like grid-column , grid-row , and grid-area . You’ll also learn how Flexbox and Grid can work together, and how to create overlapping layouts. What You’ll Learn Key Grid terminology: tracks, lines, cells, and items How to place grid items using grid-column , grid-row , and grid-area How to span items across multiple tracks Centering content inside grid items using Flexbox How to create overlapping layouts Grid Basics Refresher Before diving into placement, let’s refresh a few terms: Term Description Grid Container The parent element wi...