I was doing a bit of stuff using float and clear. I found no difference using float: none;
or clear: none;
Is there any? can anybody illustrate the difference with an example
The CSS float property specifies how an element should float. The CSS clear property specifies what elements can float beside the cleared element and on which side.
The float property can be specified with any of the following values: none (default): The element doesn't float. It is simply displayed where it occurs in the text. left: The element floats to the left of its container. right: The element floats to the right of its container.
Purpose of clearing floats in CSS: We clear the float property to control the floating elements by preventing overlapping. On our webpage, if an element fits horizontally next to the floated elements, unless we apply the clear property same as float direction then the elements will be a move below the floated elements.
Clearing Floats To clear a float, add the clear property to the element that needs to clear the float. This is usually the element after the floated element. The clear property can take the values of left , right , and both . Usually you'll want to use both.
We can use CSS clear property to specify the side of the floated element which is to be cleared of flowing content.
Clear has four valid values as well. Both is most commonly used, which clears floats coming from either direction. Left and Right can be used to only clear the float from one direction respectively.
It seems like you didn't understand the underlying concept of what float
does. Any value of float
except none
whenever assigned to a block-level element takes that element out of the document flow. Suppose you have two different div
elements, one with float:none
and the other with clear:none
. Now the later could be either in the document flow or out of the flow of document -- depending upon its float value. I present you two examples. In the first version the Red paragraph uses float:none
and in the second version the Red paragraph uses clear: none
Red paragraph using float:none
:
#usefloatnone
{
border: 1px dotted black;
background-color: red;
width: 1050px; height: 350px;
float: none;
}
#useclearnone
{
border: 1px dotted black;
background-color: red;
width: 1050px; height: 200px;
float: right;
clear: none;
}
#normal
{
border: 1px dotted black;
width: 1050px; height: 100px;
}
</style>
</head>
<p id="usefloatnone"> Red paragraph </p>
<p id="normal"> Normal paragraph </p>
<p id="normal"> Normal paragraph </p>
<p id="normal"> Normal paragraph </p>
</html>
Red paragraph using clear:none
:
#usefloatnone
{
border: 1px dotted black;
background-color: red;
width: 1050px; height: 350px;
float: none;
}
#useclearnone
{
border: 1px dotted black;
background-color: red;
width: 1050px; height: 200px;
float: right;
clear: none;
}
#normal
{
border: 1px dotted black;
width: 1050px; height: 100px;
}
<p id="useclearnone"> Red paragraph </p>
<p id="normal"> Normal paragraph </p>
<p id="normal"> Normal paragraph </p>
<p id="normal"> Normal paragraph </p>
You can see the difference in effect while using clear: none
and float: none
now. I suggest you to first thoroughly understand the concept of float
and clear
from this tutorial by w3.org community. You use clear
property upon elements when you want to clear any floating elements around/(usually left or right to) them.
They're two totally different things.
float
will make an element align to the left or right (the parameter) inside its parent. float: none
does nothing, unless the element was already floating. The float element lose it's automatically filled width, and reduce it to as small as it can get.
clear
will make sure there are no floating elements on the side you tell. If there is one, it will move down until there is none in the given direction. clear: both
will check this for both directions.
Here's an illustration to show you what floats
and clears
do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With