CSS Float and Clear

CSS Float and Clear: Managing Text Wrap and Flow

CSS layout is used to control how elements are positioned and arranged on a webpage. While modern layouts heavily rely on Flexbox and Grid, the float and clear properties are classical tools that help in organizing content, specifically for wrapping text around images or other elements.


The CSS Float Property

The CSS float property allows an element to be pushed to the left or right side of its container. When an element is floated, it is taken out of the normal document flow, allowing inline content (like text) to wrap around it.

Syntax:

.element {
    float: left | right | none | inherit;
}

Float Property Values

Value Description
left Floats the element to the left side of its container.
right Floats the element to the right side of its container.
none (Default) Removes the float and keeps the element in the normal flow.
inherit Inherits the float property from its parent element.

1. float: left

Floats the element to the left side of its container. The text that follows it will wrap around its right side.

Float Left Example:

<style>
    .box-left {
        float: left;
        width: 100px;
        height: 100px;
        background-color: navy;
        margin-right: 15px;
        margin-bottom: 10px;
    }
    .container {
        border: 2px solid lightblue;
        padding: 10px;
    }
</style>

<div class="container"> <div class="box-left">Floated Left</div> <p>This text naturally wraps around the right side of the box.</p> </div>

2. float: right

Floats the element to the right side of its container. The text that follows it will wrap around its left side.

Float Right Example:

<style>
    .box-right {
        float: right;
        width: 100px;
        height: 100px;
        background-color: lightcoral;
        margin-left: 15px;
    }
</style>

<div style="border: 2px solid lightblue; padding: 10px;"> <div class="box-right">Floated Right</div> <p>The text starts on the left and wraps nicely around the right floated element.</p> </div>

3. float: none

This is the default value. It removes any active floats and keeps the element in the normal document flow. Text will not wrap around it; it will either sit above or below it.

Float None Example:

<style>
    .box-none {
        float: none;
        width: 100px;
        height: 100px;
        background-color: #8e44ad;
    }
</style>

<div class="box-none">No Float</div> <p>With <code>float: none;</code>, this text starts on a new line and does not wrap.</p>


The CSS Clear Property

When you float elements, they can sometimes cause layout issues because they escape the normal document flow. The clear property is used to control the behavior of elements that appear after floated elements. It specifies whether an element is allowed to sit next to floated elements or if it must be pushed below them.

Syntax:

.element {
    clear: left | right | both | none | inherit;
}

Clear Property Values

Value Description
none (Default) Allows the element to be adjacent to floated elements.
left Forces the element to drop below any left-floating elements.
right Forces the element to drop below any right-floating elements.
both Forces the element to drop below both left and right floating elements.
inherit Inherits the clear property from its parent element.

1. clear: left and clear: right

Prevents the element from sitting next to left-floated or right-floated elements, respectively.

Clear Left Example:

<style>
    .float-left {
        float: left;
        width: 100px; height: 100px;
        background-color: lightblue;
    }
    .cleared-item {
        clear: left;
        background-color: navy;
        color: white;
    }
</style>

<div class="float-left">Floated left!</div> <div class="cleared-item">I have <code>clear: left;</code> applied. I am pushed below!</div>

2. clear: both

This is the most commonly used value. It clears floats on both sides simultaneously, making it ideal for footer elements or elements that need to break free from a multi-column floated layout.

Clear Both Example:

<style>
    .float-box-l { float: left; width: 100px; background-color: lightblue; }
    .float-box-r { float: right; width: 100px; background-color: lightcoral; }
    .clear-both-item {
        clear: both;
        background-color: navy;
        color: white;
    }
</style>

<div class="float-box-l">Left Float</div> <div class="float-box-r">Right Float</div> <div class="clear-both-item">I am cleared on BOTH sides!</div>

3. clear: none

Allows the element to remain adjacent to floated elements, wrapping normally.

Clear None Example:

<style>
    .float-box {
        float: left;
        width: 100px; height: 100px;
        background-color: lightblue;
    }
    .no-clear {
        clear: none;
        background-color: #f4f4f9;
    }
</style>

<div class="float-box">Left Float</div> <div class="no-clear">I have <code>clear: none;</code> applied. I wrap fine!</div>


Best Practices for CSS Float and Clear

  1. Use floats mainly for text wrapping: Floats were originally created to wrap text around images (like a magazine). While they were heavily used for creating grid layouts in the past, that is no longer recommended.
  2. Prefer Flexbox or Grid for page layouts: Modern layout modules like CSS Flexbox and CSS Grid are vastly superior, more predictable, and cleaner for structuring page columns and rows.
  3. Always clear your floats: If you float elements inside a container, the container might "collapse" to a height of 0. Clearing floats is essential to prevent these overlapping layout issues.
  4. Test designs across devices: Floated elements can quickly break on smaller screens if they don't have enough room to float side-by-side, so ensure you use media queries to remove floats on mobile devices if necessary.

Exercise

?

Which CSS property and value is used to push an element entirely below BOTH left and right floated elements?