Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between float: none and clear: none

Tags:

css

css-float

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

like image 560
shubendrak Avatar asked Aug 09 '13 18:08

shubendrak


People also ask

What is the difference between float and clear?

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.

What does float none mean?

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.

What does clear float mean?

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.

How do you use clear and float?

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.

Can you turn off a float property using clear?

We can use CSS clear property to specify the side of the floated element which is to be cleared of flowing content.

What are two valid ways to clear floats?

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.


2 Answers

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.

like image 129
user31782 Avatar answered Sep 18 '22 20:09

user31782


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.

enter image description here

like image 39
Broxzier Avatar answered Sep 18 '22 20:09

Broxzier