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 class = "div2"> div2 </div>
- Principle: IE8 or above is supported by non-ie browsers: after. The principle is similar to method 2. Zoom (ie conversion has attributes) can solve the floating problem of IE6 and IE7.
- Advantage: Good browser support, not prone to strange issues (currently: large websites are used, such as Teng Xun, Netease, Sina, etc)
- Disadvantages: There are many codes. Many beginners do not understand the principle. They need to use two sentences in combination to support mainstream browsers.
- Suggestion: It is recommended that public classes be defined to reduce CSS code.
- ★★★★
2. Add the empty Div label clear: both at the end
<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 class = "div2"> div2 </div>
- Principle: add an empty Div and clear the floating with clear: both raised by CSS, so that the parent Div can automatically get the height.
- Advantages: simple, less code, good browser support, and not prone to strange problems
- Disadvantages: Many beginners do not understand the principle. If there are many floating la s on the page, a lot of empty divs will be added, which makes people feel uncomfortable.
- Suggestion: it is not recommended, but this method was previously used to clear floating
- ★★★
3. Parent Div defines height
<Style type = "text/CSS">. div1 {Background: #000080; Border: 1px solid red;/* solution Code */height: 200px ;}. 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} </style> <Div class = "div1"> <Div class = "Left"> left </div> <Div class = "right"> right </Div> </div> <Div class = "div2"> div2 </div>
- Principle: if the parent Div manually defines the height, the parent Div cannot automatically obtain the height.
- Advantages: simple, less code, easy to master
- Disadvantage: It is only suitable for layout with fixed height. It must be accurate. If the height is different from that of the parent Div, problems may occur.
- Suggestion: it is not recommended. It is only recommended for layout with fixed height.
- ★★
4. Parent Div defines overflow: hidden
<Style type = "text/CSS">. div1 {Background: #000080; Border: 1px solid red;/* solution Code */width: 98%; overflow: hidden }. div2 {Background: #800080; Border: 1px solid red; Height: 100px; margin-top: 10px; width: 98% }. left {float: Left; width: 20%; Height: 200px; Background: # DDD }. right {float: Right; width: 30%; Height: 80px; Background: # DDD} </style> <Div class = "div1"> <Div class = "Left"> left </div> <Div class = "right"> right </Div> </div> <Div class = "div2"> div2 </div>
- Principle: width or ZOOM: 1 must be defined, and height cannot be defined. When overflow: hidden is used, the browser automatically checks the height of the floating area.
- Advantages: simple, less code, and good browser support
- Disadvantage: it cannot be used with position, because the excess size will be hidden.
- Suggestion: We recommend that you only use position or a friend who has a deep understanding of overflow: hidden.
- ★★★
5. Parent Div defines overflow: Auto
<Style type = "text/CSS">. div1 {Background: #000080; Border: 1px solid red;/* solution Code */width: 98%; overflow: auto }. div2 {Background: #800080; Border: 1px solid red; Height: 100px; margin-top: 10px; width: 98% }. left {float: Left; width: 20%; Height: 200px; Background: # DDD }. right {float: Right; width: 30%; Height: 80px; Background: # DDD} </style> <Div class = "div1"> <Div class = "Left"> left </div> <Div class = "right"> right </Div> </div> <Div class = "div2"> div2 </div>
- Principle: width or ZOOM: 1 must be defined, and height cannot be defined. When overflow: Auto is used, the browser automatically checks the height of the floating area.
- Advantages: simple, less code, and good browser support
- Disadvantage: the scroll bar appears when the internal width and height exceed the parent Div.
- Suggestion: it is not recommended. If you need a scroll bar or make sure that your code does not appear, use it.
- ★★
6. Parent Div also floats together
<Style type = "text/CSS">. div1 {Background: #000080; Border: 1px solid red;/* solution Code */width: 98%; margin-bottom: 10px; float: Left }. div2 {Background: #800080; Border: 1px solid red; Height: 100px; width: 98%;/* solution Code */clear: both }. left {float: Left; width: 20%; Height: 200px; Background: # DDD }. right {float: Right; width: 30%; Height: 80px; Background: # DDD} </style> <Div class = "div1"> <Div class = "Left"> left </div> <Div class = "right"> right </Div> </div> <Div class = "div2"> div2 </div>
- Principle: When all the code is floating together, it becomes a whole.
- Advantage: no advantage
- Disadvantage: there will be a new floating problem.
- Suggestion: it is not recommended.
- ★
7. Parent Div definition display: Table
<Style type = "text/CSS">. div1 {Background: #000080; Border: 1px solid red;/* solution Code */width: 98%; display: Table; margin-bottom: 10px ;}. div2 {Background: #800080; Border: 1px solid red; Height: 100px; width: 98% ;}. left {float: Left; width: 20%; Height: 200px; Background: # DDD }. right {float: Right; width: 30%; Height: 80px; Background: # DDD} </style> <Div class = "div1"> <Div class = "Left"> left </div> <Div class = "right"> right </Div> </div> <Div class = "div2"> div2 </div>
- Principle: Convert Div attributes into tables
- Advantage: no advantage
- Disadvantage: new unknown problems may occur.
- Suggestion: it is not recommended.
- ★
8. Add the BR label clear: both at the end.
<style type="text/css"> .div1{background:#000080;border:1px solid red;margin-bottom:10px;zoom:1} .div2{background:#800080;border:1px solid red;height:100px} .left{float:left;width:20%;height:200px;background:#DDD} .right{float:right;width:30%;height:80px;background:#DDD} .clearfloat{clear:both} </style> <div class="div1"> <div class="left">Left</div> <div class="right">Right</div><br class="clearfloat" /></div><div class="div2"> div2 </div>
- Principle: parent Div defines ZOOM: 1 to solve the IE floating problem. Add the BR label clear: both at the end.
- Suggestion: it is not recommended.
How to clear float