I have encountered a <div class="clear"></div>
at many places, but I am not aware why is it done? Can someone brief me on this, why it is used in css? This is the CSS:
div.clear {
clear:both;
}
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.
The “clear: both” means floating the elements are not allowed to float on both sides. It is used when no need of any element float on the left and right side as related to the specified element and wanted the next element only shown below.
It's a method used to clear the float property your nav has obtained through its . col class. Without this the content following your nav element may appear alongside your nav element rather than below it.
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.
The height of an element is determined by its child elements (unless it is explicitly set). E.g.:
+------A------+
|+-----------+|
|| B ||
|+-----------+|
|+-----------+|
|| C ||
|+-----------+|
+-------------+
The height of A is determined by where the lower border of its child C is.
Now, floating elements do not count towards the height of their parents, they're taken out of the regular flow:
+------A------+
+--+---------++
| B |
+---------+
+---------+
| C |
+---------+
The A element is collapsed to a minimal height, because its two children are floated.
Clearing elements are introduced to restore the correct height:
+------A------+
| +---------+|
| | B ||
| +---------+|
| +---------+|
| | C ||
| +---------+|
|+-----D-----+|
+-------------+
The D element is a zero-height element with the clear
attribute set. That causes it to fall below the floated elements (it clears them). That causes A to have a regular child element to calculate its height by. That's the most typical use case at least.
The often better solution to introducing extra HTML elements is to set A to overflow: hidden
though. That has the effect of forcing a height calculation, which has the same effect of growing the height to the desired size. This solution may or may not have other side effects though.
It is to clear out floating styles.
clear: http://www.w3.org/wiki/CSS/Properties/clear
float: http://www.w3.org/wiki/CSS/Properties/float
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