CSS Float

 What is the Float Property in CSS?


The float property in CSS is mainly used to move an element to the left or right side of its container, allowing surrounding text or inline elements to wrap around it.

👉 Example:

img { float: left; }

This will push the image to the left, and the text will wrap on the right side. If we use float: right;, the image will move to the right, and the text will wrap on the left side.


Where does the problem occur?

When you use float, the floated element is taken out of the normal HTML flow.
As a result, the elements that come after it (like a footer) may wrap around it instead of appearing below.

For example, if an image is floated to the left and you place a footer below it, the footer may appear squeezed next to the image, which looks odd.


How does the Clear Property Help?

To fix this, we can use the clear property. It tells the element not to wrap around floated elements.

👉 Example:

footer { clear: both; }

Here, clear: both; means the footer will ignore both left and right floats and appear properly below the floated elements.


When Should You Use Float?

In the past, developers often used float to build entire page layouts (e.g., sidebars, columns, etc.).
But nowadays, float is only recommended for wrapping text around images, because we have much better tools for layout, such as:

  • Flexbox

  • CSS Grid

  • Bootstrap

These modern tools are more powerful, flexible, and easier to use.


Key Takeaways:

  1. float: left / right → Moves elements to the side and allows text wrapping.

  2. clear: left / right / both → Ensures elements don’t wrap around floats.

  3. Use float mainly for text wrapping, not for full layouts.

Comments