Skip to content


Make Your Web Site Not Suck In Internet Explorer – IE CSS Min-Height Hack

Now it’s not surprise to anyone out there that I hate Internet Explorer. No I mean I really hate it! One of the reasons that I hate it so much is that it’s buggy. Call it whatever you want, maybe it’s too forgiving on bad HTML or CSS, but whatever their intentions are (forgiving sloppy code or just too lazy to interpret code right) it causes tons of problems.

The Min-Height Problem

I know a lot of you have had this problem in the past. It’s gotten so bad for me that, at one point, I’ve just stopped using them. Bottom line, Internet Explorer pretends it doesn’t see the min-height property. It uses the regular height property as a min-height. Confused? Let me make it a bit simpler:

Firefox

The height property is a fixed size. It doesn’t shrink and it doesn’t expand with content.

The min-height property is exactly what it says: it gives the element a minimum height, but it still expands with the content of the element. It’s perfect for if you have an element that will just look totally ugly if you it’s empty.

Internet Explorer

The height property is interpreted as a minimum height, funny enough. Doesn’t that suck?

IE CSS Min-Height Hack

But here’s the good news, there’s a fix/hack. Now mind you, there are tons of different ways to get around this issue. There’s conditional IE tags, there’s Javascript hacks, etc. I’ll focus on two, which I like. So here’s what we’re working with:
[sourcecode language='css']
#box {
min-height:100px;
}
[/sourcecode]

Solution/Hack One

Since, IE doesn’t understand min-height, we have to force a regular height on the element that the other browsers can’t see:
[sourcecode language='css']
#box {
min-height:100px;
}
/* mac hide \*/
* html #box{height:500px}
/* end hide */
[/sourcecode]

To me personally, this feels right (because we’re doing something specifically for IE and hiding it from other browsers), except for one thing: it involves two hacks. As the comments suggest, IE 5 (I think) for MAC does implement the height property correctly. This just gets worse and worse, doesn’t it?

Solution/Hack Two

[sourcecode language='css']
#box {
min-height:100px;
height: 100px;
}

html > body #box {
height: auto;
}
[/sourcecode]

Here’s the explanation: We set min-height for Firefox (and other well behaved browsers), then we immediately set the height property so that IE can be happy. This has the effect of, essentially, defeating the purpose of min-height for the proper browser. We then use the CSS child selectors (which, conveniently IE can’t see), to set the height back to auto and make everyone happy.

There’s a slight problem when using 100% heights that I don’t really want to get into, but you can see a fix for it here.

Why I love this hack, is because it’s a little bit cleaner. But the really great part about this is that, because we only set height to auto as a fix, we can fix multiple elements all in one line:
[sourcecode language='css']
html > body #box,html > body #box2,html > body #box3,html > body #box4, html > body #etc {
height: auto;
}
[/sourcecode]

Well folks, there we have it. The nasty subject of CSS hacks. Hope this helps.

Posted in CSS.

Tagged with , , , .

Related Posts


14 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. colleen says

    I hate IE too! Bleah Bleah Bleah!

  2. Daniel Costalis says

    Another one that I like doesn’t technically involve any hacks at all. I wrap a div around the div and set the heights that way if I REALLY need to have a set height, and just hide the overflow.

  3. Abba Bryant says

    I think #box { height:auto !important; min-height:100px; height:100px; }

    does the trick. Firefox shouldn’t apply the height 100px rule due to the unsupported in ie6 !important declaration.

  4. Luis says

    Hello. There is a much simpler solution for this:

    #div {
    min-height: 100px;
    _height: 100px;
    }

    IE will read “_height” property, other browsers won’t.

    • IE hater says

      You are right!
      min-height: 100px;
      _height: 100px;
      IT WORKS!
      DAMN IE

  5. Beth says

    Hack 2 saved my bacon! until i read the comment above, and then i used that instead… er. so i guess what i’m trying to say is, very useful page all-round..? :D

  6. rskbuvan says

    This is looking pretty simple but what happens if the height of the DIV Exceeds its limit for example we have set height:100px; this will be assigned to IE. If the content of the DIV exceeds means what would be the result.

    Looking for your reply.

  7. brent says

    check this fix out, it uses pure CSS and works great:

    min-height fast hack

    it’s the best one I’ve found

    brent
    @
    mimoYmima.com

    brent’s last blog post..Re-designs: CleverHive.com, RheaKaram.com, SlouchProductions.com

  8. Isaac says

    I don’t see why you are bothering with IE5/mac hacks when MSN.com displays terribly, Microsoft.com is less than perfect, Windows.com looks like someone’s 5-year-old child has been let loose on the CSS and has deleted a few key sections, and it has a negligible market share.

    To be honest, if jQuery doesnt support it (and jQuery supports quite a few minority and old browsers), then it isn’t worth fussing about.

    Isaac

  9. marco says

    Internet Explorer Is WEB DEVELOPER’S ENEMY NUMBER 1!

1 2

Continuing the Discussion

  1. IE Hack: CSS Centering - Another Reason I Hate Internet Explorer | Web Development 2.0: Web Design, CakePHP, Javascript linked to this post on June 9, 2008

    [...] here’s another common Internet Explorer CSS hack that’s pretty popular and not too [...]



Some HTML is OK

or, reply to this post via trackback.

CommentLuv Enabled