CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



CSS Float

CSS Float is a property that allows an element to be floated to the left or right of its container. This property is commonly used to create layouts where elements are positioned side by side.

When an element is floated, it is taken out of the normal flow of the document and positioned to the left or right of its container. This allows other elements to flow around it, creating the desired layout.

The float property can be applied to any block-level element, including divs, paragraphs, and images. It is commonly used in conjunction with the clear property, which specifies whether an element should be allowed to float next to a previously floated element.

Examples

Here are some examples of how the float property can be used:

Example 1: Float an Image to the Right

To float an image to the right of its container, you can use the following CSS:

<style>
    img {
        float: right;
        margin: 0 0 10px 10px;
    }
</style>

<div>
    <img src="example.jpg" alt="Example Image">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, velit eget bibendum bibendum, velit elit bibendum elit, vel bibendum elit elit vel.</p>
</div>

In this example, the image is floated to the right using the float: right; property. The margin property is used to add some space between the image and the text.

Example 2: Float Two Divs Side by Side

To float two divs side by side, you can use the following CSS:

<style>
    .left {
        float: left;
        width: 50%;
    }
    .right {
        float: right;
        width: 50%;
    }
</style>

<div class="left">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, velit eget bibendum bibendum, velit elit bibendum elit, vel bibendum elit elit vel.</p>
</div>
<div class="right">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, velit eget bibendum bibendum, velit elit bibendum elit, vel bibendum elit elit vel.</p>
</div>

In this example, the left and right divs are floated to the left and right, respectively, using the float: left; and float: right; properties. The width property is used to specify that each div should take up 50% of the container's width.

Example 3: Clear a Float

To clear a float, you can use the clear property. For example:

<style>
    .left {
        float: left;
        width: 50%;
    }
    .right {
        float: right;
        width: 50%;
    }
    .clear {
        clear: both;
    }
</style>

<div class="left">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, velit eget bibendum bibendum, velit elit bibendum elit, vel bibendum elit elit vel.</p>
</div>
<div class="right">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, velit eget bibendum bibendum, velit elit bibendum elit, vel bibendum elit elit vel.</p>
</div>
<div class="clear"></div>

In this example, the clear: both; property is used to clear the floats and ensure that the following content is not affected by the floats.

Conclusion

CSS Float is a powerful property that allows you to create complex layouts with ease. By floating elements to the left or right of their containers, you can create side-by-side layouts and other interesting designs.

References

Activity