1、Parent div defines pseudo classes: after and zoom
<style type="text/css"> .div1{background:#000080;border:1px solid red;} .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px} .left{float:left;width:20%;height:200px;background:#DDD} .right{float:right;width:30%;height:80px;background:#DDD} /*Clear floating code * /.clearfloat:after{display:block; clear:both; content: ""; visibility:hidden; height:0}.clearfloAt{zoom:1}< /style>< div class= "div1 clearfloat" >< div class= "left" > Left< /div>< div class= "right" > Right< /div>< /div>< div class= "div2" >Div2< /diV>
- Principle: IE8 and non-IE browsers only support: after, principle and method 2 is somewhat similar, zoom (IE to attribute) can solve the ie6, IE7 floating problem
- Advantages: good browser support, not easy to cause strange problems (currently: large websites are used, such as: Tengxun, Netease, Sina and so on)
- Disadvantages: Code, many beginners do not understand the principle, two lines of code to use in combination to allow mainstream browsers to support
- Recommendation: recommended use, it is recommended to define public classes to reduce CSS code.
- Score:
2、Add an empty div tag at the end of the clear:both.
<style type="text/css"> .div1{background:#000080;border:1px solid red} .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px} .left{float:left;width:20%;height:200px;background:#DDD} .right{float:right;width:30%;height:80px;background:#DDD} /*Clear floating code * /.clearfloat{clear:both}< /style>< div class= "div1" >< div class= "left"> Left< /div>< div class= "right" > Right< /div>< div class= "clearfloat" > </div>< /div>< div class= "div2" >Div2< /div>