Easiest cross-browser CSS min-height

Enforcing a minimum height for block elements in HTML is one of those few CSS tricks that you can’t live without. There are still enough folks using IE6, unfortunately, and it doesn’t support the min-height or min-width CSS parameters. This has caused the invention of a number of different hacks and browser-conditional style sheets to get the desired effect.

Many of the different methods work well, but after trying a number of them, I can say that the following method is the easiest to use and is compatable across all common versions of Firefox, Safari, and IE. Many of you are probably already using this method—it’s not new—but for those of you who aren’t, give it a try. It should save you a lot of headaches.

Cross Browser min-height

.foo {
min-height:100px;
height: auto !important;
height: 100px;
}

This works because all of the more recent browsers will understand and respect the min-height setting as well as the !important designation. So in the example above, the block will be given the min-height setting you specify, and the height:auto will take precendence over the height:100px, even though it appears earlier in the code. With content shorter than 100px, the min-height setting is observed, and with content that is longer, the block is sized to fit the content.

In the case of older versions of IE, neight the min-height parameter nor the !important designation are supported. Instead, the browser essentially sees a height:auto, immediately followed by a height:100px, and the latter takes precendence. Lucky for us, height parameter in older versions of IE function exactly like the min-height parameter. When content expands past the size of the element, it grows to accommodate it. When content is shorter, the specified height is respected.

I don’t find myself using it as much, but this also works with min-width:

Cross Browser min-width

.bar {
min-width:100px;
width: auto !important;
width: 100px;
}

13 Responses to Easiest cross-browser CSS min-height

  1. Anonymous on said:

    Dustin Diaz came up with this years ago for those interested… http://www.dustindiaz.com/min-height-fast-hack/

  2. Thanks! Just tried it and it works in
    FF 3.0b4,ie6 and safari 3.1.

  3. Jason Striegel on said:

    Thanks for the link to Dustin’s post. I’ve been using this trick for quite a while, so it’s good to know who came up with it.

  4. i prefer the use of _height, it is a hack, but works perfect in ie6

  5. Pavel Ushakov on said:

    Thanks for the tip!

  6. Thx J _height works great for IE.
    -> Jason, your sample doesn’t work for me. but thx for the effort.

  7. Voila! Thanks

  8. Can’t get this to work in FF 2.0.0.15, but it works a charm in IE7.

  9. Can’t get this to work in FF 2.0.0.15, but it works a charm in IE7.

  10. Bas Wenneker on said:

    Great tip, I used this a dozen times…

  11. This unfortunately does not work for the body tag.

  12. codeslinger on said:

    @ Dieter –
    And it wouldn’t ever work for the body tag. The body tag isn’t *that* kind of tag. Try placing a wrapper div around your body content and enforcing min-height on that.

  13. Exactly what I needed!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s