Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why <div class="clear"></div> is used after multiple floating DIVS in a container DIV?

Tags:

html

css

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;
}
like image 703
OM The Eternity Avatar asked Apr 17 '12 04:04

OM The Eternity


People also ask

Why do we need to use clear property along with floats in CSS?

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.

Why clear both is used in CSS?

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.

What div class clears?

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.

How do you clear a floated element?

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.


2 Answers

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.

like image 145
deceze Avatar answered Sep 19 '22 03:09

deceze


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

like image 42
Brian Avatar answered Sep 23 '22 03:09

Brian