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;
}
Posted by Jason Striegel |
Mar 21, 2008 08:31 PM
Web |
Permalink
| Comments (7)
Recent Entries
- Second Life on an Apple II
- Nice overview of the YouTube API
- Javascript Super Mario
- Automatic outbound link analytics with jQuery
- Silence your hard drive
- Air on the EeePC
- Relational database using jQuery and HTML tables
- Javascript marker clustering for Google Maps
- Windows Mobile del.icio.us plugin
- HOWTO - Encode any string into a trigonometric function
Comments
Newest comments listed first.
| Posted by: on March 24, 2008 at 11:30 AM |
Dustin Diaz came up with this years ago for those interested... http://www.dustindiaz.com/min-height-fast-hack/
| Posted by: domce on March 24, 2008 at 1:59 PM |
Thanks! Just tried it and it works in
FF 3.0b4,ie6 and safari 3.1.
| Posted by: Jason Striegel on March 24, 2008 at 9:14 PM |
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.
| Posted by: J on March 25, 2008 at 8:09 AM |
i prefer the use of _height, it is a hack, but works perfect in ie6
| Posted by: rpan on March 30, 2008 at 9:49 AM |
Thx J _height works great for IE.
-> Jason, your sample doesn't work for me. but thx for the effort.
Bloggers
Welcome to the Hacks Blog!
Categories
- Ajax
- Amazon
- AppleTV
- Astronomy
- BlackBerry
- Blogging
- Body
- Cars
- Cryptography
- Data
- Education
- Electronics
- Energy
- Events
- Excel
- Excerpts
- Firefox
- Flash
- Flickr
- Flying Things
- Food
- Gaming
- Gmail
- Google Earth
- Google Maps
- Government
- Greasemonkey
- Hacks Series
- Hackszine Podcast
- Halo
- Hardware
- Home
- Home Theater
- iPhone
- iPod
- IRC
- iTunes
- Java
- Kindle
- Knoppix
- Language
- LEGO
- Life
- Lifehacker
- Linux
- Linux Desktop
- Linux Multimedia
- Linux Server
- Mac
- Mapping
- Math
- Microsoft Office
- Mind
- Mind Performance
- Mobile Phones
- Music
- MySpace
- MySQL
- NetFlix
- Network Security
- olpc
- OpenOffice
- Outdoor
- Parenting
- PCs
- PDAs
- Perl
- Philosophy
- Photography
- PHP
- Pleo
- Podcast
- Podcasting
- Productivity
- PSP
- Retro Computing
- Retro Gaming
- Science
- Screencasts
- Shopping
- Skype
- Smart Home
- Software Engineering
- Sports
- SQL
- Statistics
- Survival
- TiVo
- Transportation
- Travel
- Ubuntu
- Video
- Virtualization
- Visual Studio
- VoIP
- Web
- Web Site Measurement
- Windows
- Windows Server
- Wireless
- Word
- World
- Xbox
- Yahoo!
- YouTube
Archives
Recent Posts
- Second Life on an Apple II
- Nice overview of the YouTube API
- Javascript Super Mario
- Automatic outbound link analytics with jQuery
- Silence your hard drive
- Air on the EeePC
- Relational database using jQuery and HTML tables
- Javascript marker clustering for Google Maps
- Windows Mobile del.icio.us plugin
- HOWTO - Encode any string into a trigonometric function
www.flickr.com
|






Leave a comment