On having layout

A work in progress.
This version: Rev. 6 2006–04–15
Table of contents


A lot of Internet Explorer's rendering inconsistencies can be fixed by giving an element “layout.” John Gallant and Holly Bergevin classified these inconsistencies as “dimensional bugs,” meaning that they can often be solved by applying a width or height. This leads to a question of why “layout” can change the rendering of and the relationships between elements. The question, albeit a good one, is hard to answer. In this article, the authors focus on some aspects of this complicated matter. For more thorough discussions and examples, please refer to the links provided.

hasLayout — A Definition

“Layout” is an IE/Win proprietary concept that determines how elements, in analogy to a window, draw and bound their content, interact with and relate to other elements, and react on and transmit application/user events.

Microsoft developers decided that box-type elements should be able to acquire a “property” (in an object-oriented programming sense) they referred to as layout, or alternatively, hasLayout.

hasLayout is probably neither a property or even a behavior, but rather an engine-inherent rendering concept which gives a quality to an element.

This rendering quality can, in fact, be irreversibly triggered to true by some CSS properties, and some HTML elements have “layout” by default.


We speak of an element “having layout” or “gaining layout” or say that an element “has layout” when we assume that the Microsoft-proprietary property hasLayout is set to true. A “layout element” can be any element which has layout by default or has gained layout by setting the appropriate CSS property.

In “non-layout” elements, hasLayout was not triggered, i.e. a pure div without a dimension can be a “non-layout ancestor”.

Giving or applying “layout” to an element that does not have it by default involves setting a CSS property that triggers hasLayout = true for the element in question. See Default Layout Elements and Properties for these listings. There is no way to set hasLayout = false other than to remove the CSS property that caused hasLayout = true in the first place.

The Hand We've Been Dealt

The hasLayout problem affects designers (and coders) at all experience levels. Layout has unusual and hard to predict effects on the display behavior of boxes that “have” layout, as well as implications for child elements within the boxes.

Consequences of an element having, or not having “layout” can include:

The above list is brief and incomplete. This article attempts to more thoroughly describe issues encountered by the application of “layout” or the lack of it.

Where Layout Comes From

Unlike standard properties, or even proprietary CSS properties available in different browsers, layout is not directly assigned via CSS declarations. In other words, there is no “layout property.” Certain elements automatically “have layout” and it is quietly added when various CSS declarations are made.

Default layout elements

The following elements appear to have layout by default.


The following CSS property/value pairs will, if applied, allow an element to gain layout.

position: absolute
Refers to its containing block, and that's where some problems begin.
float: left|right
The float model has a lot of quirks due to some aspects of a layout element.
display: inline-block
Sometimes a cure when the element is at inline level and needs layout. Applying layout is probably the only real effect of this property. The “inline-block behaviour” itself can be achieved in IE, but quite independently: IE/Win: inline-block and hasLayout.
width: any value
This is often an implicit fix, more often the trigger when hasLayout does things wrong.
height: any value
height: 1% is used in the Holly Hack.
zoom: any value (MSDN)
MS proprietary, does not validate. zoom: 1 can be used for debugging.
writing-mode: tb-rl (MSDN)
MS proprietary, does not validate.

As of IE7, overflow became a layout-trigger.

overflow: hidden|scroll|auto
This property did not apply in prior versions, unless “layout” was added to the box by other triggers.
overflow-x|-y: hidden|scroll|auto
As part of the CSS3 box model module, overflow-x and -y are not widely implemented yet. They did not trigger hasLayout in prior versions of IE.

And new hasLayout actors appeared on the screen in IE7. As far as hasLayout is concerned, the min/max properties act similar to how width and height works, and the effects of fixed and absolute positioning seem to be identical.

position: fixed
min-width: any value
Even the value 0 lets the element gain layout.
max-width: any value other than 'none'
min-height: any value
Even the value 0 sets haslayout=true
max-height: any value other than 'none'

Based on querying the IE Developer Toolbar and preliminary testing.

Notes on elements at inline level

For inline elements (either inline by default like span, or having display: inline)

Elements having both “layout” and display: inline behave in a similar way as what the standards say about inline-block: they flow horizontally like words in a paragraph, are sensitive to vertical align, and apply a sort of shrink-wrapping to their content. As soon as the inline elements have layout, they act as inline-block, this is an explanation why, in IE/Win, inline elements can contain and hold block-level elements with less problems than in other browsers, where display: inline remains inline.

The hasLayout script property

We have chosen to refer to hasLayout as a “script property” in order to distinguish it from the CSS properties we are familiar with.

Note once an element has layout, there is no way to set hasLayout = False.

The hasLayout-property can be used to check if an element has layout: If, for example, it has “eid” as id, than simply writing in the IE5.5+ address bar javascript: alert(eid.currentStyle.hasLayout) tests its state.

The IE Developer Toolbar allows for live inspecting of the current style of an element; when hasLayout is true its value is reported as “-1”. By live editing the attributes of a node, you can set “zoom (css)” to “1” to trigger hasLayout for debugging purposes.

Another thing to consider is how “layout” affects scripting. The clientWidth/clientHeight properties always return zero for elements without “layout.” This can be confusing to new scripters and is different to how Mozilla browsers behave. We can use this fact to determine “layout” for IE5.0: If the clientWidth is zero then the element does not have layout.

CSS hacks

The following hacks to trigger haslayout have been well tested in IE6 and lower. Future versions of IE might react differently. We will revisit this when new versions of this browser are publicly available.

John Gallant and Holly Bergevin published the Holly hack in 2003:

  1. /* \*/
  2. * html .gainlayout { height: 1%; }
  3. /* */

Or we can use the underscore hack:

  1. .gainlayout { _height: 0; }

Alternatively, and possibly more future proof, are conditional comments:

  1. <!--[if lte IE 6]>
  2. <style>
  3. .gainlayout { height: 1px; }
  4. </style>
  5. <![endif]-->

The use of an external style sheet for whatever fixes IE-Win needs, linked from inside a conditional comment, is also a secure and elegant solution:

  1. <link rel="stylesheet" href="allbrowsers.css" type="text/css" />
  3. <!--[if lte IE 6]>
  4. <link rel="stylesheet" href="iefix.css" type="text/css" />
  5. <![endif]-->

We prefer height: 0 and 1pxheight should “always” be used unless it conflicts with something else (overflow: hidden). About the value, we prefer to avoid 1%, since it may (albeit very rarely) cause problems.

A notable case when we cannot use height is when we want an element to stay inline. This is a case we would use display: inline-block. We use zoom: 1 only in an early debugging phase to encircle a rendering error.

We've seen desperate attempts of “holy” hacks (sic!) applied to floated elements, or to elements already having a width. Remember the goal of such an hack is not to apply a height to an element, but to trigger hasLayout = True.

Don't give layout to all: * {_height: 1px;}. Poison in that concentration, having layout is not the cure, it changes the rendering fundamentally.

Hack management

But the browsers they are changing, and we have to face the problem that depending on the bugs that are fixed in IE7 and up, inevitably hacks for IE6 might break in (or be detrimental to) new versions of the browser, or new browser versions with similar layout bugs might not deliver filters like * html anymore. In this light, the use of the MS proprietary zoom, can be advised.

  1. <!--[if lt IE 7]><style>
  2. /* style for IE 6 + IE5.5 + IE5.0 */
  3. .gainlayout { height: 0; }
  4. </style><![endif]-->
  6. <!--[if IE 7]><style>
  7. .gainlayout { zoom: 1;}
  8. /* or whatever we might need tomorrow */
  9. </style><![endif]-->

While we think “future proof” is a contradiction in terms, we strongly suggest the web designer “plays for sure” and review her pages for explicit and implicit “hacks” and use conditional comments to serve those hacks to the appropriate browser version.

A short note about IE Mac.

IE Mac and IE for Windows are two different animals, living in separate parts of the zoo. Each has its own rendering engine, and IE Mac doesn't know about the “hasLayout” behaviour (or contenteditable) in any way. The IE Mac rendering engine tend to be rather standard compliant, with i.e. height being treated as height, as it should. Hacks and workarounds for the “hasLayout” problems (especially when using the height or width properties) will often have rather detrimental effects on IE Mac, and should be hidden from that browser. More on IE Mac problems can be found at the IE Mac, bugs and oddities pages.

MSDN documentation

In the MSDN, there are very few pages related to the hasLayout MS-property, and less is explained how having layout correlates with the visual formatting model of IE.

Back in IE4, nearly every element had sort of layout, except for simple inline elements not positioned absolutely and without a dimension (MSDN). And in this early layout concept, there were “layout properties” like border, margin, padding, which can't be applied to such a simple inline element. In other words, “having layout” was just another term for roughly: “may have these properties.”

MSDN still speaks of “layout properties,” but the meaning has changed, they are not associated with elements having layout anymore. In IE5.5, the MS-property hasLayout was introduced, more or less an internal flag.

In IE5.5, the documentation of the MSHTML Editing Platform (which allows for live editing, sizing, and dragging layout elements via <body contenteditable=true>) reveals three important aspects related to having layout:

If a layout element has contents, the layout of its contents is determined by its bounding rectangle.

Having layout basically means an element is rectangular.

Internally, having layout means that an element is responsible for drawing its own content.

(Editing Platform)

The inner workings related to layout itself was not documented until August 2005, when, as a result of The Web Standards Project and Microsoft task force, Markus Mielke [MSFT] opened the door for a thorough discussion:

In general, elements in Internet Explorer's Dynamic HTML engine are not responsible for arranging themselves. A div or a p element may have a position within the source-order and flow of the document, but their contents are arranged by their nearest ancestor with a layout (frequently body). These elements rely on the ancestor layout to do all the heavy lifting of determining size and measurement information for them.

(HasLayout Overview)


Our interpretation is an attempt to explain what happens in known cases, and it should serve as a guide for cases not fully known. The attempt to demystify a black box only by inserting some test cases into it and listen if it rattles is doomed to failure. The “question why” cannot be answered. We have to try to understand the framework in which the whole “hasLayout” model works, and how it affects rendering of web documents. Out of this, guidelines can be developed (and those can only be guidelines, not absolute solutions).

We think they speak of a little window. The content of a layout element would be completely independent from anything outside of the element's boundary, and the content couldn't affect anything outside either.

The hasLayout MS-property is a sort of flag: when it is set, the element has this special layout “quality”, which includes special capabilities on the floating, clearing, stacking, counting and so on for the element itself and for its non-layouted child elements.

This greater independence of layout elements is probably the reason why they are usually more stable, and so they make some bugs to disappear. The price for this can be both deviance from standards, and further bugs/problems at their boundaries.

The MS “page” model, thinking in semiotics, can be seen as consisting of little blocks of stories that are unrelated, where as the HTML and W3C model has “page” model as complete narrative, story, blocks of information that are related.

A review of the consequences

Clearing floats and the extend to fit

Floats are auto-contained by layout elements. That's one reason why most beginners struggle with their IE-build pages in a compliant browser where floats stick out of the container when not cleared.

The opposite behavior: what if a float must stick out of its container, e.g. when auto-containing is not the desired effect? For a demonstration of the frustrating problems one might encounter, check out our in-depth discussion:

In IE, a float will always “belong” to its layout container. Subsequent elements might respect the layout container, but not the float itself.

This, and IE6's expanding of a container to fit any wider content (the “extend-to-fit”), can be seen as an aspect of the “determined by its bounding rectangle” rule.

Even worse: clear can't affect a float outside of the clearer's layout container. Float layouts relying on that bug in IE cannot be transferred to work in a compliant browser without a general re-do.

See section “Similarities with the CSS specs” for further information.

Elements next to floats

When a block follows a left floating element, the text content should flow right to the float and slide under the float. But if the block has layout, say, a width is set for some reasons, the layout element behaves like a rectangle, the text does not slide under the float. The width is incorrectly calculated starting at the right of the float, so width: 100% adds to the width of the float, resulting in a horizontal scroll bar. This is far off out of the specs.

Similar to this, actually, relatively positioned elements next to floats should offset with respect to the padding edge of the parent (i.e. left: 0; on a r.p. element would place it on top of a preceding left floated box). In IE, the offset left: value; starts from the right margin edge of the float, causing a horizontal misalignment by the total outer width of the float (a workaround is to use margin-left instead, but beware the quirky percentages flaws).

According to the specs, floats interweave with subsequent boxes. This cannot be accomplished with two-dimensional rectangles that don't intersect.

By (re-)visiting this bug page

we'll see that the layout box following the float will not show the 3px text jog, because the float's hardwired 3px surrounding cannot affect the content inside the layout element anymore, but moves the whole layout element by 3px. Like a shield, layout prevented the content from been affected, but the energy of the push by the float moves the shielded box itself.

See section “Similarities with the CSS specs” for further information.


Lists are affected by layout applied either to the list (ol, ul) or to the list elements (li). Different versions of IE react differently. The most evident effects are on the list markers (fully customized lists where the markers are not required won't have these problems.) The markers are probably created by internally adding some elements that are somewhat “attached” to the list elements (usually hangs out of them) and seems rather unstable. Unfortunately, being “internal” only objects, they cannot be accessed to try to correct mis-behaviours.

The most evident effects are:

Sometimes they can be restored by changing margins on the list elements. This looks like a consequence of the fact that a layout element tends to crop internal elements hanging out of it.

A further problem is that (in ordered lists) any list element with layout seems to have its own counter. Let's say we have an ordered list with five elements where only the third has layout. We'll see this:

1... 2... 1... 4... 5...

Moreover when a list element with layout displays on multiple lines the marker is vertically aligned at the bottom (not at the top, as expected.)

Some of these problems cannot be cured, so when the markers are desired it's better to avoid layout on lists and list elements. If it's necessary to apply some dimension, this is better applied to other elements: for example a width can be applied to an external wrapper, and a height to the content of each list item.

Another common problem with lists in IE occurs when the content of any li is an anchor with display: block. In these conditions the white space between list items is not ignored and usually displayed as an extra line for each li. One of the methods to avoid this extra vertical space is to give layout to the block anchors. This also has the benefit of making the whole rectangular area of the anchors clickable.


A table always has layout, always behaves as a width defined object. In IE6, table-layout: fixed is usually equivalent to a table with a width of 100%, with all problems that this brings (erroneous computations). As a side note, just a few things on the situation in IE5.5 and “quirks mode” IE6.

Relatively positioned elements

Note that position: relative does not trigger hasLayout, which leads to some rendering errors, mostly disappearing or misplaced content. Inconsistencies might be encountered by page reload, window sizing and scrolling, selecting. With this property, IE offsets the element, but seems to forget to send a “redraw” to its layout child elements (as a layout element would have sent correctly in the signal chain of redraw events).

are related descriptions. As a rule of thumb, never position an element relatively without setting layout. In addition, we may check if the parent of such a construct needs layout and/or position: relative too, this becomes essential when floats are affected.

Absolutely positioned elements:
Containing block, what containing block?

It is essential to understand the CSS concept of the containing block, which answers where to relate an absolutely positioned (a.p.) element to: to define the offset origin, and to define the length percentages are calculated with respect to.

For a.p. elements, the containing block is established by its nearest positioned ancestor. If there is no such ancestor, the initial containing block of html is used.

Normally, we would set such a containing block via position: relative. That means, we can let a.p. elements relate to lengths and origins independent from the flow of elements, i.e. to fulfill the needs of the “content first” accessibility concept or to make life easier in complex float layouts.

This design concept is questioned by IE, due to the concurring layout concept: the a.p. element is offset with respect to its nearest layout positioned ancestor, but the percentage dimension relates to the next layout ancestor. Note the slight difference, and as already mentioned, position: relative does not trigger hasLayout.

Say a non–layout parent is positioned relatively — we are urged to set layout to this parent to get the offset to work:

Say a non-positioned parent must have a dimension, and the design relies on a percentage width calculation — we can drop that idea, due to a lack of browser support:


The MS-proprietary filter is applicable to layout elements only. Some extend the boundary of an object. They show their own specific flaws.

Re–flow of rendered elements

Once all the elements are rendered, IE reflows the containing layout block when a :hover-transition (i.e., a change of the link's background) occurs. Sometimes the elements are placed at a new position because at the moment the hover occurs, all the widths and offsets of the related elements are known to IE. This is unlike to happen on first load, where the width is undetermined yet due to the expand-to-fit feature. This can result in a jump on hover.

These relational bugs, based on the re-flow problem, create problems in liquid layouts where is most common the use of percentages for margins and paddings.

Background origin

The hasLayout MS-property affects the extension and the positioning of the background. For example, according to the CSS spec, background-position: 0 0 should refer to the “padding edge” of the element. In IE/Win it refers to the “border edge” when hasLayout = false, and to the “padding edge” when hasLayout=true:

Margin collapsing

The hasLayout MS-property affects the collapsing of margins between a box and its descendants. According to the spec the top margin of a box with no top padding and no top border should collapse with the top margin of its first in-flow block-level child:

In IE/Win this never happens when the box has layout: it seems that layout prevents the margins of the children to stick out of the containing box. Moreover when hasLayout is true, either on the container or on the child, other wrong margins computations show up:

hasLayout affects the clickable/hoverable area of a block-level anchor. Usually with hasLayout = false only the part covered by the text is sensitive. With hasLayout=true the whole block area is sensitive. The same is true for any block element with an attached onclick/onmouseover event handler.

In–page keyboard navigation: an odyssey

While tabbing through a page and entering an in-page link, the next tab key press won't resume on the subsequent anchor:

The tab will bring — and often mislead — the user to the first target of the nearest layout ancestor (if this layout ancestor is formed by a table, div, span and certain other elements).


Some properties applied to elements with width: auto cause them to compute their width with a shrink-wrap algorithm. Examples of such properties are float: left|right, position: absolute|fixed, display: table|table-cell|inline-block|inline-table.

This works in IE/Win, of course limited to the supported properties. But when the element which should shrink-wrap contains a block element with “layout” assigned, then in most cases this child expands to the full available width, independently of its content, and prevents the shrink-wrapping effect of the parent.

A floating vertical navigation ul fails to shrink-wrap, because for the links, a {display: block; zoom: 1;} was needed to fix the list whitespace bug and to expand the clickable area.

The shrink-wrap remains in effect only if the layout child has an assigned width, or it has a shrink-wrap property itself, such as float.

Clipping over the edge

In general, when a box contains more complex structures like protruding content, “hasLayout” on this container is often necessary to avoid rendering problems. This almost-requirement causes a dilemma at the boundaries, since a block element that gains “layout” becomes a sort of self-enclosed box.

Nested content boxes that are moved outwards from that element (by using negative margins, for example) are clipped.

The clipped portions can be restored by triggering “hasLayout” on that content box, and position: relative is also needed in IE 6. The IE 7 behaviour seems slightly better in that position: relative is no longer necessary.

The stack, the layering, and layout

There seem to be two layering and stacking orders within IE/Win:

Both stacking models, though incompatible, are residents of IE's engine. As a rule of thumb: While debugging, don't miss checking both suspects. We regularly see related problems in drop down or similar complex menus, where the stacking, positioning and floating can lead to manifold disasters, and bug fixes may include giving z-indexed positioned layout, but it varies, so be warned.

The contenteditable–debacle

The attribute contenteditable=true, set on a HTML tag like <body contenteditable=true> allows for live editing, dragging and re-sizing the element and it's layout children. Now try that with floats or layout li in an ordered list.

In order to manipulate elements (edit them), “contenteditable” and “hasLayout” introduce a separate stacking order for those elements which return true for hasLayout.

The Editing Platform inherits the layout-concept, evidence can be seen that contenteditable is the reason for the layout-misconception (with the implication that applications that somewhat integrate the IE editing engine force a backward compatibility to this layout-concept).

Similarities with the CSS specs

Are your MSIE-designed pages failing in other browsers? No need to let that happen, you know! Any good browser can handle MSIE-designs just fine if you ask them nicely — and serve them some valid CSS.

Utilizing the slight similarities found between hasLayout and the establishment of “new block formatting context” gives us some means to reproduce the effects of hasLayout for “containment of floats” and for the behaviour of “elements next to a floated element” in standard compliant browsers.

Quirks mode

Please refer to our quirks mode chapter for information about this rendering mode.

Layout — a conclusion

The layout concept as a whole is not compatible to a number of basic CSS concepts of the visual formatting model, namely containing, the flow, floating, positioning and layering.

This leads to IE/Win specific violations of the CSS specification due to the presence or absence of layout on elements in the page.

Having layout — part of another engine?

The object model inside Explorer appears to be a hybrid of a document model and their traditional application model. I mention this as it is important in understanding how Explorer renders pages. The switch for jumping from a document model to an application model is to give an element “layout”.

(Dean Edwards)

Sometimes it's impossible to give an interpretation to some behaviour: it's simply like, depending on hasLayout status, one of two different rendering engines is used, each one with its own quirks and bugs.

The absurdity of bugs

Software-bugs are the result of human errors and lack of completeness and logic during the creation-process. It's a fundamental human shortcoming, for which a lasting cure is yet to be found.
Any attempts to correct buggy software without recreating it from scratch, will inevitably lead to even more and more complex bugs finding their way into the software, because of the same human shortcomings.
All software that rely on other software — including Operating Systems (of course), will also rely on its bugs. Thus we get a cascade of bugs from all involved bits of software, which makes even the thought of finding bug-free software completely absurd.

(Molly ‚the cat‛)

This article was created on June 30, 2005 and last changed on April 15, 2006.

Holly Bergevin
Ingo Chao
Bruno Fassino
John Gallant
Georg Sørtun
Philippe Wittenbergh
Special thanks for supporting the project to:
Dean Edwards, and Molly ‚the cat‛
Brazilian Portuguese by Mauricio Samy Silva
中文版本 by old9
Discuss this article:
Contact us:
Copyright notice:
This work is published under a Creative Commons license.

Table of contents

  1. Introduction
  2. hasLayout — A Definition
  3. Nomenclature
  4. The Hand We've Been Dealt
  5. Where Layout Comes From
  6. Default layout elements
  7. Properties
  8. Notes on elements at inline level
  9. The hasLayout script property
  10. CSS hacks
  11. Hack management
  12. A short note about IE Mac.
  13. MSDN documentation
  14. Interpretation
  15. A review of the consequences
  16. Clearing floats and the extend to fit
  17. Elements next to floats
  18. Lists
  19. Tables
  20. Relatively positioned elements
  21. Absolutely positioned elements: Containing block, what containing block?
  22. Filter
  23. Re–flow of rendered elements
  24. Background origin
  25. Margin collapsing
  26. Block–level links
  27. In–page keyboard navigation: an odyssey
  28. Shrink-wrapping
  29. Clipping over the edge
  30. The stack, the layering, and layout
  31. The contenteditable–debacle
  32. Similarities with the CSS specs
  33. Quirks mode
  34. Layout — a conclusion
  35. Having layout — part of another engine?
  36. The absurdity of bugs

Permanent location: http://www.satzansatz.de/cssd/onhavinglayout.html