The code is as follows: |
Copy code |
<Style> . Container { Border: 3px solid #000; Width: 600px; Background-color: # eee; Margin-bottom: 50px; } . Floatedbox { Float: left; Border: 3px solid # bbb; Width: 125px; Height: 125px; Background-color: # fff; Margin-right: 1em; Padding: 0 10px; } </Style> </Head> <Body> <Div class = "container"> <Div class = "floatedbox"> <P> Floated box </P> </Div> <H3> Container 1 <P> (<Em> without </em> Easy clearing) </P> </Div> </Body> </Html> |
I checked it online. This is a famous floating problem. What we need to do is to clear the floating effect.
Solution
There are many solutions on the Internet, but they are generally in these three categories:
Add the div element containing the clear: both style definition to the outer layer of the floating div.
Using overflow: auto, but this is a legend that may have a very good effect.
Use the after pseudo element for processing, and use hack for IE
First, the second type. Since there are some cups in the legend, I certainly don't need them. The first one is that people like me who have html-like cleanup will certainly not use it. Adding a div tag for no reason makes me feel awkward.
Therefore, we will color the after pseudo element for processing. The specific code after processing is as follows:
The code is as follows: |
Copy code |
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"> <Title> easy floating clear </title> <Style> . Container { Border: 3px solid #000; Width: 600px; Background-color: # eee; Margin-bottom: 50px; } . Floatedbox { Float: left; Border: 3px solid # bbb; Width: 125px; Height: 125px; Background-color: # fff; Margin-right: 1em; Padding: 0 10px; } . Clearfix: after { Content :"."; Display: block; Height: 0; Clear: both; Visibility: hidden; } * Html. clearfix {height: 1% ;} </Style> </Head> <Body> <Div class = "container"> <Div class = "floatedbox"> <P> Floated box </P> </Div> <H3> Container 1 <P> (<Em> without </em> Easy clearing) </P> </Div> <Div class = "container"> <Div class = "floatedbox"> <P> Floated box </P> </Div> <H3> Container 1 <P> (<Em> without </em> Easy clearing) </P> <Div style = "clear: both;"> </div> </Div> <Div class = "clearfix container"> <Div class = "floatedbox"> <P> Floated box </P> </Div> <H3> Container 1 <P> (<Em> without </em> Easy clearing) </P> </Div> </Body> </Html> |
Solve the problem. Some people on the Internet say that adding a "dot" behind the div tag is uncomfortable, and visibility should be used to hide it. We recommend that you use the following code instead:
The code is as follows: |
Copy code |
. Clearfix: after {content: ""; display: block; height: 0; clear: both ;} |
In this way, our problem is solved.