Table of Contents
Learning about hasLayout and Block formatting contexts
What is hasLayout?
What are Block formatting contexts?
Home Web Front-end HTML Tutorial Learning about hasLayout and Block formatting contexts (Part 1)_html/css_WEB-ITnose

Learning about hasLayout and Block formatting contexts (Part 1)_html/css_WEB-ITnose

Jun 24, 2016 am 11:39 AM

Learning about hasLayout and Block formatting contexts

@(css BFC)[IE hasLayout|Miaotong]

What is hasLayout?

haslayout 是Windows Internet Explorer渲染引擎的一个内部组成部分。在InternetExplorer中,一个元素要么自己对自身的内容进行计算大小和组织,要么依赖于父元素来计算尺寸和组织内容。为了调节这两个不同的概念,渲染引擎采用了 hasLayout 的属性,属性值可以为true或false。当一个元素的 hasLayout属性值为true时,我们说这个元素有一个布局(layout)。注意:hasLayout属性是微软特有的过时属性,在IE8、IE9中,hasLayout属性已经被废弃。下文中的InternetExplorer都是指IE7、IE6及以下版本。
Copy after login

When an element has a layout, it is responsible for sizing and positioning itself and possible descendant elements. Simply put, this means that the element needs to spend more time maintaining itself and its contents, rather than relying on ancestor elements to do this work. Therefore, some elements will have a layout by default. When we say that an element "has layout" or "gets layout", or that an element "has layout", we mean that its Microsoft-specific property hasLayout is set to true. A "layout element" can be an element that has a layout by default or an element that has a layout by setting certain CSS properties. You can check whether HTML elements under IE have haslayout through IE Developer Toolbar. Under IE Developer Toolbar, elements with haslayout are usually displayed as "haslayout = -1". ‘Layout’ In IE, you can use the hasLayout attribute to determine whether an element has layout, such as object.currentStyle.hasLayout.

Elements responsible for organizing their own content will have a layout by default, which mainly includes the following elements (incomplete list):

	<html>, <body>	<table>, <tr>, <th>, <td>	<img>	<hr>	<input>, <button>, <select>, <textarea>, <fieldset>, <legend>	<iframe>, <embed>, <object>, <applet>	<marquee>对于并非所有的元素都默认有布局,微软给出的主要原因是“性能和简洁”。如果所有的元素都默认有布局,会对性能和内存使用上产生有害的影响。http://www.satzansatz.de/cssd/onhavinglayoutrev07-20060517.html
Copy after login

Debugging and solving haslayout issues

When the web page behaves abnormally in IE, you can try to activate haslayout to see if the problem is the problem. A common method is to set zoom:1 to the css of an element. Zoom:1 is used because in most cases it fires the element's haslayout without affecting the existing environment. Once the problem disappears, it can basically be determined that it is the cause of haslayout. Then you can correct this problem by setting the corresponding css properties. It is recommended that the first thing to consider is to set the width/height attributes of the element, and then consider other attributes.
For IE6 and earlier versions, the common method is called the Holly hack, which is to set the height of this element to 1% (height:1%;). It should be noted that this method will not work when the overflow property of this element is set to visible. Or use IE's conditional comments.
For IE7, the best way is to set the minimum height of the element to 0 (min-height:0;).
Common bugs caused by haslayout issues
Double margin floating bug in IE6 and lower versions
bug fix: display:inline;
3 pixel offset bug in IE5-6/win
bug fix: height:1%;
IE6’s peek-a-boo bug
bug fix: height:1%;
IE6/7 negative margin hidden bug:
bug Repair: Remove the hasLayout of the parent element; or assign hasLayout to the child element and add position: relative;

How to activate haslayout?

Most IE display errors can be corrected by activating the haslayout attribute of the element. You can activate the element's haslayout by setting the css size attribute (width/height), etc., so that it "has layout". Just set the following css properties as shown below.

  • display: inline-block
  • height: (any value except auto)
  • width: (any value except auto)
  • float: (left or right)
  • position: absolute
  • writing-mode: tb-rl
  • zoom: (any value except normal)
  • Internet Explorer 7 also has some additional properties (not a complete list):

  • min-height: (any value)
  • max-height: (any value except none)
  • min-width: (any value)
  • max-width: (any value except none)
  • overflow: (any value except visible)
  • overflow-x: ( Any value except visible)
  • overflow-y: (Any value except visible)
  • position: fixed
    where overflow-x and overflow-y are Properties in the css3 box model are not yet widely supported by browsers.
    For inline elements (the default is inline elements, such as span, or elements with display:inline;),
    width and height are only triggered in IE5.x and IE6 or newer versions of quirks mode hasLayout. For IE6, if the browser is running in standards compatibility mode, inline elements will ignore the width or height attributes, so setting width or height cannot order the element to have layout in this case.
    zoom can always trigger hasLayout, but it is not supported in IE5.0.
    If an element with "layout" displays: inline at the same time, its behavior is very similar to the inline-block mentioned in the standard: it is arranged horizontally and continuously in the paragraph like ordinary text, subject to vertical- align affects, and the size can be adjusted adaptively according to the content. This can also explain why in IE/Win alone, inline elements can contain block-level elements with less problems, because in other browsers, display: inline means inline, unlike IE/Win, once the inline element has a layout, it still has a layout. Will become inline-block.
  • What are Block formatting contexts?

    This article refers to the W3C CSS 2.1 specification Visual formatting model
    http://www.w3.org/TR/CSS2/
    http://www.w3.org/TR/CSS2/visuren .html#inline-level
    http://www.w3.org/TR/CSS2/visuren.html#normal-flow

    Box: The basic unit of CSS layout

    Box is the object and basic unit of CSS layout. From a visual point of view, a page is composed of many Boxes. The type of element and display attribute determine the type of this Box. Different types of Box will participate in different Formatting Context (a container that determines how to render the document), so the elements within the Box will be rendered in different ways. Let’s see what boxes there are:

    block-level box: elements with display attributes of block, list-item, table will generate block-level boxes. And participate in block fomatting context;
    inline-level box: Elements with display attributes of inline, inline-block, and inline-table will generate inline-level boxes. And participate in inline formatting context;
    run-in box: only available in css3, I won’t talk about it for now. http://www.w3.org/TR/css3-box/#run-in-boxes

    Formatting context

     Formatting context is the W3C CSS2.1 specification a concept in . It is a rendering area on the page and has a set of rendering rules that determine how its sub-elements will be positioned and their relationship and interaction with other elements. The most common Formatting contexts are Block fomatting contexts (BFC for short) and Inline formatting contexts (IFC for short).

    There are only BFC and IFC in CSS2.1, and GFC and FFC are also added in CSS3.
     
    BFC definition

     BFC (Block formatting contexts) is literally translated as "block-level formatting context". It is an independent rendering area in which only the Block-level box participates. It stipulates how the internal Block-level Box is laid out and has nothing to do with the outside of this area.
     
    BFC layout rules :

  • The internal Boxes will be placed one after another in the vertical direction.
  • The vertical distance of the Box is determined by margin. The margins of two adjacent boxes belonging to the same BFC will overlap
  • The left side of the margin box of each element is in contact with the left side of the containing block border box (for a left-to-right format ation, otherwise the opposite). This is true even if there is float.
  • The BFC area will not overlap with the float box.
  • BFC is an isolated independent container on the page. The sub-elements inside the container will not affect the elements outside. And vice versa.
  • When calculating the height of BFC, floating elements also participate in the calculation.
  • In CSS3, this concept has been changed: http://www.w3.org/TR/css3-box/#block-level0
    In CSS3, BFC is called flow root.
  • How to trigger BFC?

  • float: (any value except none)
  • overflow: (any value except visible)
  • display:(table-cell/table-caption/inline-block)
  • position: (any value except static/relative)
    Note: We sometimes use overflow The :hidden method is used to clear floats because the BFC of the element is triggered (IE6 7 requires zoom to be 1 and triggers hasLayout). You can refer to the detailed explanation of css float
  • This is the summary for today, and we will continue with the next article tomorrow. If there are any errors or deficiencies, please point them out, thank you very much! ----Miaotong.

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1666
    14
    PHP Tutorial
    1273
    29
    C# Tutorial
    1255
    24
    HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

    The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

    The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

    The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

    The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

    HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

    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.

    HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

    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 vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

    HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

    HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

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

    From Text to Websites: The Power of HTML From Text to Websites: The Power of HTML Apr 13, 2025 am 12:07 AM

    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.

    See all articles