Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of style="clear:both"?

Tags:

html

css

I happened to see a div which had the style clear:both! What is the use of clear in style?

<div style="clear:both"> 
like image 438
thomasbabuj Avatar asked Jun 18 '09 11:06

thomasbabuj


People also ask

What does CSS clear do?

The clear CSS property sets whether an element must be moved below (cleared) floating elements that precede it. The clear property applies to floating and non-floating elements.

Why We Use clear in HTML?

The clear property is used to specify which side of floating elements are not allowed to float. It sets or returns the position of the element in relation to floating objects. If the element can fit horizontally in the space next to another element which is floated, it will.

How do you clear a float on both sides?

Clearing Floats The footer should sit below both the sidebar and main content. 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 .

Why do you need to clear floats?

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.


2 Answers

clear:both makes the element drop below any floated elements that precede it in the document.

You can also use clear:left or clear:right to make it drop below only those elements that have been floated left or right.

+------------+ +--------------------+ |            | |                    | | float:left | |   without clear    | |            | |                    | |            | +--------------------+ |            | +--------------------+ |            | |                    | |            | |  with clear:right  | |            | |  (no effect here,  | |            | |   as there is no   | |            | |   float:right      | |            | |   element)         | |            | |                    | |            | +--------------------+ |            | +------------+ +---------------------+ |                     | |   with clear:left   | |    or clear:both    | |                     | +---------------------+ 
like image 65
RichieHindle Avatar answered Oct 13 '22 06:10

RichieHindle


Just to add to RichieHindle's answer, check out Floatutorial, which walks you through how CSS floating and clearing works.

like image 33
Paul Dixon Avatar answered Oct 13 '22 05:10

Paul Dixon