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 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;
}
| 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. |
float: leftFloats the element to the left side of its container. The text that follows it will wrap around its right side.
<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>
float: rightFloats the element to the right side of its container. The text that follows it will wrap around its left side.
<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>
float: noneThis 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.
<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>
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;
}
| 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. |
clear: left and clear: rightPrevents the element from sitting next to left-floated or right-floated elements, respectively.
<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>
clear: bothThis 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.
<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>
clear: noneAllows the element to remain adjacent to floated elements, wrapping normally.
<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>
Which CSS property and value is used to push an element entirely below BOTH left and right floated elements?