"Turn" CSS Learning topic-BFC

Source: Internet
Author: User

CSS Learning topic-BFC: Original link: CSS Learning topic-BFCArticle Directorybfc:block formatting Context.

BFC is a way of layout, in which the boxes are arranged vertically from the top of the containing block, and the entire width horizontally (unless a new BFC is built inside the box itself). Developer Center has a description of the BFC:
A block formatting context is a part of a Web page Visual CSS rendering, a block boxes arrangement and a zone where the float element interacts.

Can be understood as a range of functions in which the layout of a BFC is unrelated to, or does not affect, the layout outside of it. An example of an image can be used as a reference:

Think of a page as a large container, and the goods in this container are HTML elements. In the real life in order to avoid different people's goods to confuse each other, is to put the goods packaging and then loaded into the container, so that no matter how you put the goods in the packaging, will not affect other people's goods. Then this package can be imagined as block formatting Context.

How to create a BFC

A block formatting Context can be generated when an HTML element satisfies any of the following conditions:

    • The value of float is not "none"
    • The value of overflow is not "visible"
    • The value of display is "Table-cell", "table-caption", or "Inline-block" any one of the
    • The value of position is not one of "static" or "relative"

I'm a kid with a floating layout. Only today know this original also can not clear floating to the layout, as long as the concept of BFC clear, completely can not clear floating to layout.

Previous misconceptions 1. Folding margin

It was always thought that the adjacent two boxes were put together, their margin would overlap, and the maximum value would be the margin between them.

Here I have a thorough understanding of the cause of folding margin in an article.

These margin are in the ordinary flow, and in the same BFC;
These margin are not separated by non-empty content, padding, border, or clear;
These margin are adjacent in the vertical direction, including the following situations:

1, a box top margin and the first sub box top margin
2. A box's bottom margin and the bottom margin of the last child box, but must be in the case of the box height of auto
3. A box's bottom margin and the next box's top margin immediately following
4. A box top margin with its own bottom margin, but must meet the height without creating BFC, 0 min-height, 0 or "auto", and no normal stream of sub box

The adjacent box in the vertical direction does not collapse:

The margin of the root element does not participate in folding

A clearance box's upper and lower margin is adjacent, it will collapse with the next box, but the folded margin will no longer be folded with the bottom margin of their parent box

Calculation of folded margins

When two margin is positive, take the maximum value of the two, when the margin is negative, take the absolute greater, then, from the 0 position, negative displacement, when there is positive negative, first remove the negative margin in the absolute value of the largest, and then, and positive margin values of the largest The margin is added. It must be noted, however, that all adjacent margin is involved in the operation and cannot be carried out in a step.

How to solve the case of a float again two boxes folded together? method Of course is to build a BFC

<div style= "background: #eee; border:1px solid Red;overflow:hidden;" >
< div id= "A" style= "background: #aaa; overflow:auto;margin-top:20px;" >
< div id= "B" style= "background:white;border:1px solid green;margin-top:10px;" >
B: After a overflow:auto, I have a 10px margin-top.
</div>
</div>
</div> above can be seen, to solve the vertical margin overlay problem, the key is to give the corresponding container to create a new BFC. Set up a BFC to solve two different boxes in the same box. 2.BFC can wrap floating element instance parsing

To continue the discussion of the topic of BFC, this article discusses a simple example: how to make a two-row, two-column page layout that solves the problem of non-wrapping of floating layouts.

There are many layout methods for making two rows of two columns, floating layouts, table layouts, and display:inline-block-based list layouts. This focus is on floating layouts, because we're here to solve a problem with floating layouts: What if the element floats without wrapping? Other layouts do a brief narrative first, as the expansion will be discussed carefully in the future it.
Implemented with floating layouts

Consider the following example, where the goal is to display two rows of two columns:

<div id= "A" >
< div id= "red" style= "background:red;width:100px;height:100px;float:left;" ></div>
< div id= "Orange" style= "background:orange;width:100px;height:100px;float:left;" ></div>
</div>
< div id= "B" >
< div id= "yellow" style= "background:yellow;width:100px;height:100px;float:left;" ></div>
< div id= "green" style= "background:green;width:100px;height:100px;float:left;" ></div>
</div>

We can see that four boxes end up in one column.

But our intention was to put them in the same box, divided into two columns.

What's the way?

Set up a BFC in one of the outer boxes.

<div id= "A" style= "Display:inline-block;" >
< div id= "red" style= "background:red;width:100px;height:100px;float:left;" ></div>
< div id= "Orange" style= "background:orange;width:100px;height:100px;float:left;" ></div>
</div>
< div id= "B" >
< div id= "yellow" style= "background:yellow;width:100px;height:100px;float:left;" ></div>
< div id= "green" style= "background:green;width:100px;height:100px;float:left;" ></div>
</div> Well, finally realize the floating layout of the two rows of two columns, understand?is the role of BFC: BFC can wrap floating elements3. Floating on the left, the right side of the box is covered by the left side of the previous I always thought is normal, now understand that BFC to understand that can be avoided, has been to the right to add padding-left or margin-left to make it out of the left of the mask. It is now known that you can make it to the right of a by creating a new BFC to the right.CodeCan be: <div class= "Item" ><div class= "img" ></div><div class= "content" ></div></div The &GT;CSS code can be. img{float:left; margin-right:10px;}. Content{overflow:auto; zoom:1;} 4.discussion on floating overflow in layer<div id= "side" style= "Float:left" > Left floating elements </div>
< div id= "content" >
< div id= "Content-inner" >
< ul class= "Content-banner Clearfix" >
< li class= "item" style= "Float:left" > Left floating element </li>
< li class= "item" style= "Float:left" > Left floating element </li>
< li class= "item" style= "Float:left" > Left floating element </li>
</ul>
</div>
</div> This problem instance to the UL's parent element plus one: Overflow:auto;*zoom:1 can be (plus float or other), that is, a new BFC, the problem solved. This article is from this article: BFC Study notes, thank you very much for writing such high-quality articles. In addition, I also like here a few examples and solutions he cited. We hope to find out more about BFC in the future.

1, BFC can prevent vertical margin overlay problem

2. BFC can contain floating of internal elements

3. BFC can prevent the element from being covered by floating

4, BFC can decide to clear the range of floating

Resources:

Vernacular block formatting Context

One of the CSS positioning mechanisms: normal flow

CSS 101:block formatting Contexts (BFC) (the original is dead, here is reproduced)

Block-level boxes, containing block, block formatting context of the difference between and relations

About block formatting Context (BFC)

Original link: CSS learning topic-BFC All rights reserved, please specify the source when reproduced, offenders must investigate.
Annotated source format: front-end Development blog (http://caibaojian.com/bfc.html)

Source:CSS Learning Topic-bfc-Front-end development blog



"Turn" CSS Learning topic-BFC

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.