On the overlap of floating two elements

problem description

two DIV,DIV1 floats to the left, but DIV2 does not float. The effect is that DIV2 floats to the position of DIV1 and is covered by DIV1, and the text of DIV2 is squeezed out

.

what methods have you tried

clearing floats in DIV2 solves this problem, but I don"t quite understand why it"s not an element or a line, and why it overlaps.

related codes

/ / Please paste the code text below (do not replace the code with pictures)

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <style>
  div{
  width: 100px;
  height: 100px;
}
-sharpdiv1{
  background-color:red;
  float:left;
}
-sharpdiv2{
  background-color:yellow; 
 /* clear: both; */
}
  </style>
</head>
<body>
  <div id="div1">
    I AM DIV ONE
  </div>
  <div id="div2">
    I AM DIV TWO
  </div>
</body>
</html>

what result do you expect?

I hope I can understand why this happened, thank you!

Dec.07,2021
The

float takes the element out of the document stream. Just add a margin-left:100px; to the div2 element


  1. div2 is overwritten by div1 because div1 uses float,position to break away from the normal document stream, which is equivalent to not occupying the document position, so div2 adds it.
The text of

2.div2 is squeezed out because float is not completely separated from the document stream, because the floating block of is separated from the normal document stream, but it still occupies the text space of the normal document stream , which causes the text content of div2 to be squeezed out.


float was originally used to surround images similar to Chinese characters in word. Just keep this in mind


means that the floating block part is separated from the document stream.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3eaac-2c436.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3eaac-2c436.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?