The concept of BFC in CSS and the inner-layer div method of outer div wrapping

Source: Internet
Author: User
Tags add format


For the use of Div and CSS, I think it is more proficient. But for some conceptual things, know very little, for example, today to see a BFC concept, do not know what CSS BFC meaning. The concept is known only after the information has been consulted. In fact, although we do not know what BFC mean, because in the project, no one will say what BFC. But we often use BFC in the project, and each CSS layout will be used basically. In order to eliminate this blind spot, say today what is BFC Bar.


The concept of BFC is BFC the formatting context of "block-level format" and "block-level format scope". It is a concept in the 2.1 specification of the consortium, which determines how an element locates its content and its relationships and interactions with other elements.   Colloquially speaking, is a div inside, we use float and margin layout element. BFC layout caused problems about BFC layout caused by the problem, I previously in the CSS common effect Summary of the 10th article on the use of CSS pseudo class processing methods.   Today I will speak specifically.   First of all, let's look at the problems caused by BFC layout. For example, the following code


<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Clear float</title>
    <style type="text/css">
        .container{
            margin: 30px auto;
            width:600px;
            height: 300px;
        }
        .p{
            border:solid 3px #a33;
        }
        .c{
            width: 100px;
            height: 100px;
            background-color: #060;
            margin: 10px;
            float: left;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="p">
            <div class="c"></div>
            <div class="c"></div>
            <div class="c"></div>
        </div>
    </div>
</body>
</html>


The effect we want to see is:






The results appear as follows:






Solution



There are generally 2 ways to solve the problem.



Clear the float with the Clean property



Make the parent container form BFC



To clear the floating method, we can use the following methods:



Add a class to the parent element


<div class="p floatfix">
    <div class="c">1</div>
    <div class="c">2</div>
    <div class="c">3</div>
</div>


Add the following CSS


.floatfix{
    *zoom:1;
}
.floatfix:after{
    content:"";
    display:table;
    clear:both;
}


This method is a better solution!



Another solution is to let the parent element become BFC, this method has a small disadvantage, that is, the parent element has also become floating, do not recommend this method!




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.