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%;

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
