105

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

<div style="clear:both">
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
thomasbabuj
  • 3,779
  • 3
  • 24
  • 22

3 Answers3

252

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    |
|                     |
+---------------------+
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
20

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

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • 1
    On 15 Feb 2016, I was unable to connect to the server for Floatutorial (connection refused on port 80). – dlu Feb 15 '16 at 16:18
3

When you use float without width, there remains some space in that row. To block this space you can use clear:both; in next element.

Ken
  • 1,110
  • 2
  • 10
  • 26
imdad
  • 31
  • 1