Reprinted from: http://www.smallni.com/haslayout-block-formatting-contexts/
Because my brain is not working well, I printed out a hasLayout and The trigger table of Block Formatting Contexts (hereinafter referred to as BFC) is posted on the desk (it can also be said to have created a BFC). I will remember it after reading it every day. I wonder if you have an in-depth understanding of these two things. If so, If you really understand it, some strange bugs in various browsers will be easily solved. Today, let’s analyze them together and uncover their mysterious veil.
1.hasLayout
'Layout' is a proprietary concept of IE. It determines how an element positions and sizes its content, its relationship and interaction with other elements, and how it interacts with other elements. Applications also have user impacts.
<html>, <body><table>, <tr>, <th>, <td><img alt="hasLayout && Block Formatting Contexts_html/css_WEB-ITnose" ><hr><input>, <button>, <select>, <textarea>, <fieldset>, <legend><iframe>, <embed>, <object>, <applet><marquee>
display: inline-block /*ALL*/height: (除 auto 外任何值) /*仅适用IE6 7*/width: (除 auto 外任何值) /*仅适用IE6 7*/float: (left 或 right) /*ALL*/position: absolute /*ALL*/writing-mode: tb-rl /*ALL*/zoom: (除 normal 外任意值) /*仅适用IE6 7*/
min-height: (任意值)/*以下适用IE7+*/min-width: (任意值)max-height: (除 none 外任意值)max-width: (除 none 外任意值)overflow: (除 visible 外任意值,仅用于块级元素)overflow-x: (除 visible 外任意值,仅用于块级元素)overflow-y: (除 visible 外任意值,仅用于块级元素)position: fixed
2.Block Formatting Contexts (BFC)
IE has its own hasLayout attribute, so what about non-IE browsers? Non-IE browsers use BFC (Block Formatting Context)
float:(任何值除了none)overflow:(任何值除了visible)display:(table-cell/table-caption/inline-block)position:(任何值除了static/relative)
Tips: We sometimes use the overflow:hidden method to clear floats because the block formatting context of the element is triggered (IE6 7 requires zoom to be 1 ), this method is indeed simple, but very violent? -#
3. Features of hasLayout and BFC
3.1 Among the elements that trigger hasLayout and the elements that create BFC, the floating element participates in the height Calculation of
Case 1: The block-level non-replacement element of the BFC is not created, triggering IE's hasLayout.
Analyze the following code:
<div style="width:300px;"> <div id="Container" style="background:silver; zoom:1;"> <span id="SPAN1" style="background:gray;">simple text</span> <div id="DIV1" style="width:150px; height:50px; background:dimgray;">in flow</div> <div id="DIV2" style="float:left; background:gold;">float:left</div> </div></div>
According to the height calculation rules in Section 10.6.3 of the CSS2.1 specification, when calculating the height of block-level non-replaced elements in ordinary flow, floating child elements do not participate in the calculation.
So, when calculating the height of Container, it is only affected by SPAN1 and DIV1 and should be the sum of their two heights. , so the final silver part should not contain the gold part.
This code behaves as follows in different browser environments:
去掉 Container 的 ‘zoom:1′ 后,各浏览器表现一致:
可见,IE 浏览器中,触发 hasLayout 的元素在进行高度计算的时候,其浮动的子元素也会参与运算。
情况2:创建了 BFC的块级非替换元素,未触发 IE 的 hasLayout。
分析以下代码:
<div style="width:300px;"> <div id="Container" style="background:silver; overflow:hidden;"> <span id="SPAN1" style="background:gray;">simple text</span> <div id="DIV1" style="width:150px; height:50px; background:dimgray;">in flow</div> <div id="DIV2" style="float:left; background:gold;">float:left</div> </div></div>
根据 CSS2.1 规范第10.6.7部分的高度计算规则,在计算生成了 BFC的元素的高度时,其浮动子元素应该参与计算。
所以,在进行 Container 高度计算时,DIV2 也应该参与计算,所以最终银色部分应该包含金色的部分。
这段代码在不同的浏览器环境中表现如下:( 注意 IE7(S) 此时触发了 hasLayout )
可见,只要 Container 创建了 BFC,其浮动子元素就会参与其高度计算(IE7(S) 是由于 hasLayout 导致与其他浏览器的效果相同)。
3.2 与浮动元素相邻的、触发了 hasLayout 的元素或创建了 BFC 的元素,都不能与浮动元素相互覆盖
如果浮动元素的两侧有足够的空间放置该元素,则元素会紧邻浮动元素放置,必要时,该元素的宽度将会被压缩。否则它们可 能会定位到浮动元素的下方。
情况1:没有创建 BFC 的块级非替换元素,触发了 IE 的 hasLayout。
分析以下代码:
<div id="Container" style="border:2px solid gold; width:300px; height:150px;<strong> background:url("grid2a.png") repeat;</strong>"> <div id="DIV1" style=" width:100px; height:100px; float:left; filter:alpha(opacity=50); opacity: 0.5;"> Float Block </div> <div id="DIV2" style=" zoom:1;"> If I had a single flower for every time I think about you, I could walk forever in my garden. </div></div>
其中,grid2a.png 背景是 100px * 100px 的图片:
根据 CSS 2.1 9.5 Floats 中的描述,浮动元素会覆盖普通流中的块容器。所以,DIV2 应该有一部分呢被 DIV1 覆盖。
这段代码在不同的浏览器环境中表现如下:(忽略 IE 中 3px BUG 的影响)
情况2:创建了 BFC的块级非替换元素,未触发 IE 的 hasLayout。
分析以下代码:
<div id="Container" style="border:2px solid gold; width:300px; height:150px;<strong> background:url("grid2a.png") repeat;</strong>"> <div id="DIV1" style=" width:100px; height:100px; float:left; filter:alpha(opacity=50); opacity: 0.5;"> Float Block </div> <div id="DIV2" style=" overflow:hidden;"> If I had a single flower for every time I think about you, I could walk forever in my garden. </div></div>
根据 CSS 2.1 9.5 Floats 中的描述,创建了BFC的元素不能与浮动元素重叠, 所以,DIV2 应该有一部分被 DIV1 覆盖。
这段代码在不同的浏览器环境中表现如下:( 注意 IE7(S) 此时触发了 hasLayout )
3.3 触发 hasLayout 的元素和创建了 BFC的元素不会与它们的子元素发生外边距折叠
情况1:没有生成BFC的块级非替换元素,触发了 IE 的 hasLayout。
分析以下代码:
<div id="Container" style="width:300px; border:1px solid gold;"> <div id="DIV1" style="zoom:1; background:darkgray;"> <div id="DIV2" style="margin:30px 0; width:60px;">content</div> </div></div>
根据 CSS 2.1 8.3.1 Collapsing margins 第一条,两个相邻的普通流中的块框在垂直位置的空白边会发生折叠现象。
DIV1 和 DIV2 应该发生空白边折叠,深灰色的 DIV1 应该刚好包含 ‘content’ 文本。
这段代码在不同的浏览器环境中表现如下:
可见,在 IE 中,触发 hasLayout 的元素,阻止了它自身与子元素间的空白边折叠。
情况2:生成 BFC的块级非替换元素,未触发 IE 的 hasLayout。
分析以下代码:
<div id="Container" style="width:300px; border:1px solid gold;"> <div id="DIV1" style="overflow:hidden; background:darkgray;"> <div id="DIV2" style="margin:30px 0; width:60px;">content</div> </div></div>
根据 CSS 2.1 8.3.1 Collapsing margins 第三条,生成BFC 的元素不会和在流中的子元素发生空白边折叠。
DIV1 和 DIV2 不应该发生空白边折叠,深灰色的 DIV1 应该撑满 Container 。
这段代码在不同的浏览器环境中表现如下:( 注意IE7(S) 此时触发了 hasLayout )
It can be seen that in IE, if the BFC is created and hasLayout is not triggered, the blank edge collapse between itself and the child elements will still occur.
Similarities, differences and possible problems between hasLayout and BFC
Solution
The above problem can only be avoided when an element triggers hasLayout in an earlier version of IE and creates a BFC in other browsers. That is, enable both of the above to ensure compatibility across browsers, or conversely, disable both.
- Enable the element to generate BFC and trigger hasLayout
- For the element that triggers hasLayout, set it through CSS to make it generate BFC;
- Generate BFC but If there is no element that triggers hasLayout, set 'zoom:1' to make it trigger hasLayout.
- The element does not trigger hasLayout and does not create a BFC.
Part of the content of this article comes entirely from W3Help (武利剑). Some other sources of information referenced in this article:
http://www.w3.org/TR/CSS21/visuren .html#block-formatting
http://reference.sitepoint.com/css/haslayout
http://blog.csdn.net/pengju_guo/article/details/6945436
http://www.qianduan.net/comprehensive-haslayout.html

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool