Home Web Front-end HTML Tutorial IE浏览器中hasLayout的介绍_html/css_WEB-ITnose

IE浏览器中hasLayout的介绍_html/css_WEB-ITnose

Jun 24, 2016 am 11:32 AM

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 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
1253
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