search
HomeWeb Front-endHTML TutorialIE浏览器中hasLayout的介绍_html/css_WEB-ITnose

haslayout是Windows Internet Explorer渲染引擎的一个内部组成部分。在InternetExplorer中,一个元素要么对自身的内容进行计算大小和组织,要么依赖于父元素来计算尺寸和组织内容。为了调节这两个不同的概念,渲染引擎采用了haslayout的属性,属性值可以为true或false。

当一个元素的haslayout属性值为true时,我们说这个元素有一个布局(layout)。要想更好的理解CSS,尤其是IE下对CSS的渲染,haslayout是一个非常有必要彻底弄清楚的概念。大多IE下的显示错误,就是源于haslayout。当一个元素有一个布局时,它负责对自己和可能的子孙元素进行尺寸计算和定位。

简单来说,这意味着这个元素需要花更多的代价来维护自身和里面的内容,而不是依赖于祖先元素完成这些工作。因此一些元素默认会有一个布局。属性haslayout被设为了true。拥有布局的元素可以是默认就有layout的元素也可以是通过设置某些CSS属性得到layout的元素。如果某个html元素拥有haslayout属性,那么这个元素的haslayout值一定为true,haslayout为只读属性一旦被触发,就不可逆转。

通过IE Developer Tollbar 可以查看IE下html元素是否拥有haslayout,在IE Developer Toolbar下,拥有haslayout的元素,通常显示为‘haslayout=-1’。

负责组织自身内容的元素将默认有一个布局,主要包括以下元素(不完全列表):

*body and html *table,tr,th,td *img *hr *input,button,file,select,textarea,fieldset *marquee *frameset,frame,iframe *objects,applets,embed

对于并非所有的元素都默认有布局,微软给出的主要原因是‘性能和简洁’。如果所有的元素都默认有布局,会对性能和内存使用上产生有害的影响。

如何激发haslayout大部分的IE显示错误,都可以通过激发元素的haslayout属性来修正。可以通过设置CSS尺寸属性(width/height)等来激发元素的haslayout,使其‘拥有布局’。

如下所示,通过设置以下CSS属性即可。*display:inline-block *height:(任何值除了auto) *float:(left或right) *position:absolute *width:(任何值除了auto) *writing-mode:tb-rl *zoom:(除normal外任意值) Internet Explorer7还有一些额外的属性(不完全列表):*min-height:(任意值) *max-height:(除none外任意值) *min-width:(任意值) *max-width:(除none外任意值) *overflow:(除visible外任意值) *overflow-x:(除visible外任意值) *overflow-y:(除visible外任意值) *position:fixed。

对于内联元素(默认即为内联的元素或display:inline的元素),width和height只在IE5.x下和IE6或更新版本的quirks模式下触发haslayout。而对于IE6,如果浏览器运行于标准兼容模式下,内联元素会忽略width或height属性,所以设置width或height不能在此种情况下令该元素具有layout。

zoom总是可以触发haslayout,但是在IE5.0中不支持。具有‘layout’的元素如果同时display:inline,那么他的行为就和标准中所说的inline-block很类似了:在段落中和普通文字一样在水平方向和连续排列,受vertical-align影响,并且大小可以根据内容自适应调整。这也可以解释为什么单单在IE/Win中内联元素可以包含块级元素而少出问题,因为在别的浏览器中display: inline 就是内联,不像 IE/Win 一旦内联元素拥有 layout 还会变成 inline-block。

haslayout 问题的调试与解决

当网页在 IE 中有异常表现时,可以尝试激发 haslayout 来看看是不是问题所在。常用的方法是给某元素 css 设定 zoom:1。使用 zoom:1 是因为大多数情况下,它能在不影响现有环境的条件下激发元素的 haslayout。而一旦问题消失,那基本上就可以判断是haslayout 的原因。然后就可以通过设定相应的 css 属性来对这个问题进行修正了。建议首先要考虑的是设定元素的width/height 属性,其次再考虑其他属性。

对 IE6 及更早版本来说,常用的方法被称为霍莉破解(Holly hack),即设定这个元素的高度为 1%(height:1%;)。需要注意的是,当这个元素的 overflow 属性被设置为 visible 时,这个方法就失效了。或者使用 IE的条件注释。对 IE7 来说,最好的方法时设置元素的最小高度为 0 (min-height:0;)。

haslayout 问题引起的常见 bug

IE6 及更低版本的双空白边浮动 bug   bug 修复: display:inline; 

IE5-6/win 的 3 像素偏移 bug   bug 修复: _height:1%; 

IE6 的躲躲猫(peek-a-boo) bug   bug 修复: _height:1%;

Statement
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
The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

See all articles

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

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

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),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor