Understanding haslayout-Major evil root cause of CSS bug in main ie browsers

Source: Internet
Author: User

What is haslayout??Haslayout is a unique attribute of IE. Many CSS bugs in IE are closely related to them. In IE, an element either calculates the size and organization of its own content, or relies on its parent element to calculate the size and organization content. When the haslayout attribute value of an element is true, it is responsible for calculating and locating the size of itself and its potential child elements. Although this means that this element requires more cost to maintain itself and its content, rather than relying on the ancestor element to do this.


Haslayout = true by default for the following elements
<Table> <TD> <body> <HR> <input> <SELECT> <textarea> <button> <IFRAME> <embed> <Object> <APPLET> <marquee>
In many cases, changing the haslayout status to true can solve many bugs in IE.
Haslayout attributes cannot be set directly. You can only set certain CSS attributes to trigger and change the haslayout status. The following lists some CSS attribute values that can trigger haslayout.
-------------------------------------
Display
Value for starting haslayout: inline-block
Value for canceling haslayout: other value
--------------------------------------
Width/height
Value for starting haslayout: value other than auto
Value for canceling haslayout: Auto
---------------------------------------
Position
Value for starting haslayout: absolute
Value for canceling haslayout: static
----------------------------------------
Float
Value for starting haslayout: Left or Right
Value for canceling haslayout: None
---------------------------------------
Zoom
Value of haslayout startup: Value
Cancels the haslayout value: narmal or null.
(Zoom is the proprietary attribute of Microsoft IE. It can trigger haslayout but does not affect the Display Effect of the page. Zoom: 1 is commonly used for debugging, but ie 5 does not support this attribute .)
----------------------------------------
Writing-mode: Tb-rl
This is also Microsoft's proprietary attribute.
IE7 also has some additional attributes that can be triggered (incomplete 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) 5
Position: fixed
Problems caused by the element haslayout are generally easy to find: The content is often misplaced or even completely invisible. For example, when an element contains floating or absolute positioning content, it usually shows strange and wrong behavior.Code:

<Div id = "nofloatbox">
<Div id = "floatbox"> </div>
</Div>

The CSS code is as follows:

# Nofloatbox {
Border: 1px solid # ff0000;
Background: # CCC;
}
# Floatbox {
Float: left;
Width: 100px;
Height: 100px;
Border: 1px solid # 0000ff;
Background: #00ff00;
}

The IE display result is as follows:
 



It can be seen that the non-floating div is not supported by the height of the floating element in it, and its height is not automatically calculated. Next we will add a ZOOM: 1 to the non-floating Div to trigger its haslayout attribute. the CSS code is as follows:

# Nofloatbox {
Border: 1px solid # ff0000;
Background: # CCC;
Zoom: 1;
}
# Floatbox {
Float: left;
Width: 100px;
Height: 100px;
Border: 1px solid # 0000ff;
Background: #00ff00;
}

Save and refresh the IE browser window, for example:

 

It can be seen that the height of the peripheral container is supported this time.
Similarly, setting the other settings described above will trigger the CSS attribute of haslayout will get this result.
Generally, browsers that comply with standards such as Firefox can add overflow: hidden; To solve this problem, while IE does not. You need to trigger its haslayout attribute.
Haslayout can also have an effect on inline elements. When haslayout of an inline element is true, you can set the height and width for this inline element and get the expected effect. For example:
Code:

<P> today's <span style = "width: 100px; Height: 50px; Background: # DDD;"> good weather </span> </P> 

Shows the effect:



Add ZOOM: 1 to span to trigger haslayout:

today's weather really good



the effect of this operation is shown in:



note that haslayout is proprietary to Microsoft, it is ineffective for browsers that comply with standards such as Firefox, so it cannot be too dependent. It seems that IE8 does not need to trigger haslayout to achieve the same effect as Firefox. I wonder if IE8 has abandoned this attribute?
In fact, according to the rational semantics, the appropriate document stream, and the pages produced by correct standardization, under the standard rendering browsers produced by various companies, generally, there will be no compatibility issues.
in general, if the display does not match the expected effect caused by layout, it will behave normally in ff, and an error will occur in IE. At this time, you can try to trigger the haslayout attribute of the parent container and its sub-containers. You can usually add ZOOM: 1; to debug the feature. Until the problem element is found, and then the targeted correction is made. The best way is to set the size attribute for this element. However, sometimes it is inconvenient to specify the size attribute, you can only find an alternative. For IE7, the best way is to set the minimum height attribute to 0. This technique is harmless, because 0 is the initial value of this attribute. And there is no need to hide this attribute from other browsers. The method for triggering an element haslayout in IE6 and earlier versions is to set the height attribute of this element to 1% when the overflow attribute is visible, and then hide this setting for other browsers. This technology is the famous Holly hack.

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.