How to clear float in css

Source: Internet
Author: User
Tags visibility

 

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.