CSS clear floating float three ways to summarize, why clear float? What are the effects of floating? One up $ ('. Float ')

Source: Internet
Author: User

First, throw a problem brick (display:block) look at the phenomenon:

Parsing the HTML code structure:

<div class="outer">    <div class="div1">1</div>    <div class="div2">2</div>    <div class="div3">3</div></div>

Parsing CSS Code styles:

.outer{border: 1px solid #ccc;background: #fc9;color: #fff; margin: 50px auto;padding: 50px;}.div1{width: 80px;height: 80px;background: red;float: left;}.div2{width: 80px;height: 80px;background: blue;float: left;}.div3{width: 80px;height: 80px;background: sienna;float: left;}

I don't set the height of the outermost div.outer here, but we know that if the elements inside it don't float, the height of the outer layer will be automatically open. But when the inner element floats, there is a bit of impact:

(1): Background cannot be displayed (2): Border cannot be stretched (3): Margin setting value does not display correctly

Second, clear CSS floating:

Method One: Add new elements, apply clear:both;

Html:

<div class="outer">    <div class="div1">1</div>    <div class="div2">2</div>    <div class="div3">3</div>    <div class="clear"></div></div>

Css:

.clear{clear:both; height: 0; line-height: 0; font-size: 0}

Result: (corrected: Padding will not be affected)

Method Two: Parent div definition Overflow:auto (note: The parent div is the div.outer here)

Html:

<div class="outer over-flow"> //这里添加了一个class    <div class="div1">1</div>    <div class="div2">2</div>    <div class="div3">3</div>    <!--<div class="clear"></div>--></div>

Css:

.over-flow{    overflow: auto; zoom: 1; //zoom: 1; 是在处理兼容性问题}

The result: Of course it is! Img{display:none}; Sketch

Principle: Use the overflow property to clear the float there is a point to note that the overflow property has three attribute values: hidden,auto,visible. We can use Hiddent and auto values to clear the float, but remember not to use the visible value, if the use of this value will not be able to achieve clear floating effect, the other two values can be, its area is said to be a SEO friendly, another hidden SEO is not too friendly, The other difference I can not say, nor waste time.

Method Three: It is said to be the highest method: After method: (note: Father acting on a floating element)

First of all, the principle: This method to clear the float is now the most popular on the internet, a clear float, he is using: after and: Before to insert two elements inside the element, from the face to clear the effect of floating. The implementation principle is similar to the Clear:both method, except that: clear inserts a div.clear tag in HTML, and outer uses its pseudo-class clear:after to add a div.clear-like effect inside the element. Here's a look at how it's used:

.outer {zoom:1;}    /*==for IE6/7 Maxthon2==*/.outer :after {clear:both;content:‘.‘;display:block;width: 0;height: 0;visibility:hidden;}   /*==for FF/chrome/opera/IE8==*/

wherein Clear:both; means to clear all floats; content: '. '; Display:block, for FF/CHROME/OPERA/IE8, where content () can be value or empty. The function of Visibility:hidden is to allow the browser to render it, but not to display it so that it can be clearly floated.

Last: But not unimportant, nor do I not know!

Next tab directly clear floating Brother label floating, the next tab in the properties of the direct write to clear clear:both, so you can clear the above label floating without adding an empty label to clear the float.

$ ('. float '). End (). Conclusion: Although there are a lot of ways to clear the float, but not every one is suitable for you, and not each can be very good compatible with all browsers, so refer to you think the best way to do, personally think the method three good, do not need more than the label, but also very good compatibility. Again again: When an inner element is floating, the parent element will no longer contain the floating inner element if the float is not closed, because the floating element is out of the document flow. Which is why the outer layer cannot be stretched!

(End of this article)

CSS clear floating float three ways to summarize, why clear float? What are the effects of floating? One up $ ('. Float ')

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.