CSS floating layout, floating principle, clear (closed) floating method

Source: Internet
Author: User

CSS Floating 1. What is floating :A technique used in our layout that allows us to lay out the elements by letting them float, we can move the elements around horizontally, and then adjust the position with the margin property. 2. The principle of floating motion : Leaving the current element out of the normal stream, as if floating, the box can move left and right until its outer edge encounters the edge of the containing box or another floating box 3. Floating generation :Use CSS properties Float:left/right/none left floating/right floating/not floating (default) 4. Effects of floats:
    • Does not affect the non-floating block-level element layout, but affects the layout of inline elements
    • The floating element can set the width and height, that is, the inline element floats and becomes a block-level element, but I prefer to inline the element with the Inline-block element, that is, the characteristics of both block-level and inline elements
    • Because floating elements out of the normal flow, there will be a highly collapsed phenomenon: the original parent container height is the current element A is open, but when the a element floats, the height of the parent container collapses (if the parent container height is less than the height of a element), the following diagram illustrates the height collapse:

5. Clear floating and closed floatsFirst of all, my personal understanding: I'll explain what I understand. closed float and clear float Clear Floating: Use clear element to remove floating outside, to resolve the effect of floating outside closed float: In the current block level, its child elements use a float, which affects the layout inside the current block and the outside of the block, so the floating closure in the current block can be maximized by removing the effect. As an example: code:
<styletype= "Text/css">. Box1{width:200px;Border:2px solid #0f0;        /*Overflow:hidden;*/    }. Box1. Child-1{float: Left;Height:100px;width:100px;background:#fd0;    }. Box1. Child-2{float: Left;Height:100px;width:100px;background:#fba;    }. Box2{width:200px;Height:150px;Border:2px solid #00f;        /*Clear:both;*/    }</style><Body>    <Divclass= "Box1">        <Divclass= "Child-1">Child-1</Div>        <Divclass= "Child-2">Child-2</Div>    </Div>    <Divclass= "Box2"></Div></Body>

Figure I: Primitive figure two: Child-1 and child-2 to make a floating graph three: distinguish between clear floating and closed floating (personal understanding: solve problems outside, Internal issue not resolved) Clear float: For box2 use: Clear:both figure IV: Closed floating (problem solved internally: solving external problems at the same time) closed float: Use Overflow:hidden for Box1 (one way, There's a better way back there. So I prefer to call it a closed float: This is what I call, in fact, can also be called clear floating, when it is feel closed floating comparison image. 6. Closed Floating method (a few common methods): Since the float brings these adverse effects, we will have to find a way to clear it. One :Set the Clear:both property by adding an empty element at the end of the floating element; Disadvantage: The cost is too high to add an additional element, maintenance difficulties
<Divclass= "Box1"><Divclass= "Child-1">Child-1</Div><Divclass= "Child-2">Child-2</Div><Divstyle= "Clear:both;"></Div></Div><Divclass= "Box2"></Div>

secondly:By setting the parent element overflow or the Display:table property to close the float, let's explore the principle here. Is the method used to add Overflow:hidden to Box1. its three:Using pseudo-elements: After, here is the code, add Clearfloat this class to Box1
/* Fitting IE6 */ . Clearfloat {Zoom:1;} . Clearfloat:after {Display:block; height:0; content:""; Clear:both; Visibility:hidden;}

Above to write a personal understanding of the floating, if there are different errors, welcome to point out

CSS floating layout, floating principle, clear (closed) floating method

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.