CSS Hackz Series: Minimum Width, Maximum Width for Internet Explorer 6

Post #547 categorized as Function, Presentation, last updated on May 20, 2008
Tagged with css, expression, hacks, ie, javascript

Opening the CSS Hackz series is the infamous CSS-expression hack for achieving minimum and maximum widths in Internet Explorer 6.

Here is how to set the maximum width for IE 6:

#target_element { 	
	width: expression((document.body.clientWidth > 778)? "777px" : "auto");
	max-width: 777px;
	}

Here is how to set the minimum width for IE 6:

#target_element { 	
	width: expression((document.body.clientWidth < 335)? "333px" : "auto");
	min-width: 333px;
	}

Here is how to set both maximum and minimum widths for IE 6:

#target_element { 	
	width: expression((document.body.clientWidth < 335)? "333px" : (document.body.clientWidth > 778)? "777px" : "auto");
	max-width: 333px;
	min-width: 777px;
	}

To implement any of these techniques, simply replace all instances of 777 and/or 333 with the desired target min/max width(s). Then replace all instances of 778 and/or 335 with a value that is at least one pixel greater than your target value(s). This important step is frequently omitted from many min/max-width tutorials, and is required to prevent Internet Explorer from freezing up and crashing. See this comment for additional information.

Alternatively, as Robert Nyman points out, a better approach involves targeting IE 6 directly via externally executed JavaScript function:

window.onload = checkAvailableWidth;
window.onresize = checkAvailableWidth;
function checkAvailableWidth(){
	var container = document.getElementById("container");
	container.style.width = (document.body.clientWidth > 1100)? "1100px" : "auto";
}

..which should be included via conditional comment:

<!--[if lt IE 7]>
	<script type="text/javascript" src="/js/maxWidthFixForIE6.js"></script>
<![endif]-->

There are several clear benefits to using this method for achieving minimum and maximum widths in IE 6:

  1. Valid CSS!
  2. Clean separation of behavior from presentation
  3. Vastly improved performance due to JavaScript’s use of event handlers. The CSS expression is recalculated continuously as the user interacts with the web page, while the JavaScript behavior is calculated only when the specified events (i.e., browser resize) occur.

Either way, it’s your choice which method to use when implementing minimum/maximum width for IE 6. If you know of an alternate method of achieving the effect, be kind and share a comment with us!

Next up..

Next in the CSS Hackz Series: PNG Image Display for Internet Explorer! Don’t miss it!

Subscribe to Perishable Press


6 Responses

TopLeave a comment

[ Gravatar Icon ]

#1Purpurina

I’m trying your hack for maximum-width, inside the container div I have a swf which is set to be 100% width. It doesn’t appear, so doesn’t seem to work with flash objects…
Any suggestions?
The hack works fine in the same scenario if I simply remove the swf and add some lines inside the div. Another thing I found is that I have set the margin for container to “0 auto” in order to have it centered, when applying your hack, the div goes to the left, doesn’t remain centered.

[ Gravatar Icon ]

#2Jeff Starr

Hi Purpurina, you may want to experiment with various JavaScript approaches for facilitating the proper display of your Flash content. A robust library such as jQuery or Prototype should provide the additional tools you need to get Internet Explorer to behave.. I have also seen a few independent (standalone) scripts that may help; try searching for “minimum maximum width javascript ie” for more information.

[ Gravatar Icon ]

#3Ben

Great post. I wonder if you’ve ever ran into a situation like this, however…

I need to set a min-width for IE6, but I’ve got a flash object whose width is 100%. Even if I offset the values in the width property like you describe, IE6 freezes up every single time when I resize the window. Any ideas on how to work around that?

[ Gravatar Icon ]

#4Jeff Starr

@Ben: This issue may be the result of padding and/or margins applied to the target element. See this article for more information.

[ Gravatar Icon ]

#5Chad Garrett

Call me crazy, but I don’t think you have to go to such extremes for min-width. Last I checked, IE ignores width when the width of the content is bigger than the set width. All you need to do (if you have a separate IE6 stylesheet) is this:

width: 778px;

And IE6 behaves as if this meant min-width.

Share your thoughts..

TopRead official comment policy

The rules are simple. Comment intelligently. Stay on-topic. Don’t spam! Suspected spam will be deleted. Use your real name or nickname, not a site name or business name. Using a site name or business name is a good way to get your link or comment removed. Certain comments are moderated; if your comment does not appear after several days, or if you wish to comment privately, contact me. Also, by posting a comment, you grant this site a perpetual license to reproduce your comment, name, and website URL. Lastly, you may use basic HTML markup, but please do not use <pre> tags. Instead, wrap your code with <code> tags. Use a new set of <code> tags for each code term or phrase, as well as for each individual line of code (i.e., multiple lines of code require multiple code tags). Please see the complete comment policy for more information.