two years agoUsing hasLayout to fix bugs in IE

Recently at work I came across a particularly tricky IE bug that I have seen a couple times now. I thought I would share the solution since it wasn't immediately obvious.

On the new Business in the community site (just launched by Torchbox) there is a timeline facility to show information about different events in Business, Politics and 'Business in the community' over time (Matthew did the awesome javascript that powers it).

bitcold.png

The scrolling is controlled by animating the horizontal position relative value of the inner timeline div, the container window has overflow: hidden. Whilst styling it, initially in quirks mode, the animation of the timeline div's worked just fine. IE behaved and respected the overflow on the container. However, in standards mode the overflow failed and the timeline divs were appearing outside of the container in both IE6 and 7, where of course other browsers were fine.

bitc1.png

When debugged I realised that adding a position property fixed the issue, from this and from experience it was then apparent that the inner timeline was having its hasLayout property set to true in IE but the container was not.

The hasLayout property is Microsoft's way of identifying elements that form part of the structure of the page, it treats them differently and some crazy bugs can occur because of it. The solution was then to force the container to also set hasLayout property to true, so putting position:relative on the container made the overflow:hidden behave.

hasLayout is really annoying, it defaults to false unless (as found on the Microsoft site) an object has the properties height, display, float, position or width set (or also writingMode or zoom which are both proprietary Microsoft properties) however I would be careful believing entirely the 'remarks' section especially as it doesn't list position:relative as one of the triggers where as in fact it is.

In case you haven't come across it before now, other craziness that can occur from the hasLayout property includes some elements disappearing behind containers with background colours (again quite easily fixed by forcing the elements inside to have the hasLayout property as true)

hasLaylout also affects keyboard navigation in subtle ways as explained in this article.

4 comments

  1. The other classic way to trigger hasLayout - if you can't relatively position an element, for whatever reason - is to give it a height.

    Doesn't matter what height. Any height will do. I go for either 1% or 100%, but that'll give it layout, and make it behave. I've had lots of "exciting" bugs with background-images on list-items because of IE6's "layout" property.

    As you note, the concept of "layout" remains in IE7, despite their tidy-up of browser bugs, so it's still a useful thing to be able to trigger.

    Good stuff to have in one place, Nat!

    tom 2nd December 2006 22:23permalink.

  2. I think this is the best reference for this topic: http://www.satzansatz.de/cssd/onhavinglayout.html (english) (although i find it sometimes confusing, but that's not the fault of the authors:))

    Sebastian 3rd December 2006 11:12permalink.

  3. I've systematically used hasLayout to fix IE rendering bugs for years now. I've reduced the "fix" down to a single line that I'll now apply out of reflex. I wrote a bout it recently in an entry I called Tackling the IE factor - IE7 update.

    Már 3rd December 2006 12:29permalink.

  4. Thanks, This has given me the understanding of this bug which ive encountered many time before in the past. I have built several complicated layouts and in IE some block elements behaved very strangely (and i knew incorrectly) and sometimes assigning a position or a height fixed the problem

    Marty 5th December 2006 09:19permalink.

Comments are closed.

2nd December 2006

You are reading "Using hasLayout to fix bugs in IE " written by Natalie Downe on the 2nd of December 2006 at 2:07 pm.

Previously hosted at http://notes.natbat.net/2006/12/02/haslayout/

Next: Spammers and their social duty

Previous: Clever use of the materials at hand!