The outer margin and BFC distance of BFC are folded.

Source: Internet
Author: User

The outer margin and BFC distance of BFC are folded.
Problem description: Set margin-bottom in the sub-element. The value of this margin-bottom does not act on the sub-element, but will be transferred to the outer div, as shown below.

<Div style = "background: red;"> the content of the parent element occupies <div style = "margin-bottom: 20px; background: blue; "> contents of sub-elements occupy </div> <div style =" background: yellow "> This is the content of the next line </div>

Effect

Why does this happen? The root reason is that the BFC outer margin is folded. What is the outer margin. BFC outer margin foldingThe outer margins of two adjacent boxes (maybe sibling or ancestor) can be merged into a separate outer margin. This method of combining margins is called collapse. When the margin is folded. Trigger condition: it must be a block-level box in the conventional Document Stream and be in the same BFC. They are not separated, such as padding, border, and clearance. It is adjacent in the vertical direction, adjacent situations include the following: 1. The margin-top of the element and the margin-top of the first child element in the regular Document Stream; 2. The margin-bottom of the highly automatic element and the last one margin-bottom 3 in the normal document flow, margin-bottom of the element, margin-top 4 of the element, margin-top of the element, and margin-top of the element -bottom, however, if you do not create BFC, min-height is 0, child elements that do not contain normal document streams, height is 0, or auto. Solution: To avoid this problem, the trigger condition can be disabled to solve the problem. Therefore, we have the following solutions: 1. Add height to the parent element or set the padding value, separate them. 2: Change the parent element to a block formating context, that is, create a BFC. According to the definition of BFC, the following are optional methods: a, float: left/right B, position: absolute c, display: inline-block/table-cell d, overflow: hidden/auto

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.