Beware of Margins or Padding when Using the min-width Hack for IE

Published Sunday, September 14, 2008 @ 7:01 am • 15 Responses

While we all watch as Internet Explorer 6 dies a slow, painful death, many unfortunate designers and developers continue to find themselves dealing with IE6’s lack of support for simple things like minimum and maximum widths. Fortunately, there are solutions to this problem, primarily in the form of CSS expressions such as this:

/* set the minimum width for IE 6 */
#target_element {
	width: expression((document.body.clientWidth < 335)? "333px" : "auto"); /* min-width for IE6 */
	min-width: 333px; /* min-width for all standards-compliant browsers */
	}

Although ugly, invalid, and resource-intensive, this technique works well at setting min-width properties for IE6. But beware! There is a catch. Chris Herdt recently wrote in with an important discovery:

…inclusion of any padding or margin on the element that has the fix applied will cause IE6 to crash…

After checking out Chris’ online demonstration of the behavior, I returned to the lab and began running a few tests. In addition to verifying and understanding Chris’ demonstration, I also wanted to know the following:

  • Does this issue affect older (pre-6) versions of Internet Explorer?
  • Is this issue seen in similar hacks such as max-width or max-height?
  • What are some workarounds?

First of all, this issue seems only confined to Internet Explorer version 6. Although I didn’t test on IE4 or IE3 (why bother?), IE5 did not crash when put to the test. So, for all practical purposes, IE6 seems to be the only browser affected by this issue.

As for related CSS expressions, such as max-width and max-height, the addition of margins and/or padding does not seem to have such an effect. Only when using the CSS min-width expression (i.e., hack) on IE6 will the addition of margins and/or padding crash the browser.

To explore a couple possible workarounds for this bug, let’s first review the mechanism by which the min-width expression operates. Here it is again (so you don’t have to scroll up):

/* set the minimum width for IE 6 */
#target_element {
	width: expression((document.body.clientWidth < 335)? "333px" : "auto"); /* min-width for IE6 */
	min-width: 333px; /* min-width for all standards-compliant browsers */
	}

The element for which we are applying the min-width property is typically a div. Here, we see that the ID of such an element is #target_element, surprisingly enough. So we have this division, and we want it to remain at least 332 pixels wide, regardless of how the browser is resized. 1. To accomplish this in Internet Explorer, we must use the JavaScript expression seen in the example; however, to accomplish the same thing in all modern browsers (e.g., Safari, Opera, Firefox, IE7, et al), we need only specify a value for the min-width property (i.e., the second declaration in our example). On a side note, this sort of browser-wrangling is exactly one of the reasons why people are so excited about the inevitable death of Internet Explorer 6.

So this is all fine and well and good, right? Well yes, it certainly works, but only until you try adding a bit of horizontal margin or padding to the target element, as seen in this example:

/* set the minimum width for IE 6 */
#target_element {
	width: expression((document.body.clientWidth < 335)? "333px" : "auto"); /* min-width for IE6 */
	min-width: 333px; /* min-width for all standards-compliant browsers */

	margin-right: 1px; /* say goodnight, IE6! */
	}

That little margin-right declaration will crash IE6 in a bad way. In fact, any of the following horizontally effective declarations will crash IE6, regardless of their values:

  • margin-right: 1px;
  • margin-left: 1px;
  • margin: 0 1px 0 1px;
  • padding-right: 1px;
  • padding-left: 1px;
  • padding: 0 1px 0 1px;

This brings us to an easy workaround for this issue: don’t add horizontal (left and/or right) padding or margins to the target element of a min-width expression. You can add all the vertical (top and/or bottom) padding and margins that you need, but stay away from teh horizontal stuff!

If you do need to apply space between the target element and its parent, consider using Chris’ workaround:

…I applied padding to the body instead of the content div and then just changed the expression to document.body.clientWidth - [padding values] < [desired minimum width +1].

Simply apply the padding (or margins) to the parent of the target element and subtract the difference from the CSS expression:

/* first apply the padding to the parent element */
#parent_element {
	padding-right: 33px;
	padding-left: 33px;
	/* sum equals 66px */
	}
/* then set the minimum width for IE6 accordingly */
#target_element {
	width: expression((document.body.clientWidth - 66 < 335)? "333px" : "auto"); /* min-width for IE6 */
	min-width: 333px; /* min-width for all standards-compliant browsers */
	}

Of course, there are probably other workarounds to consider as well. Perhaps there is a way to configure the layout that doesn’t even require the min-width property? I don’t know, but I do know one thing: if you are still required to design for Internet Explorer 6, you have my deepest sympathy.

Special thanks to Chris Herdt for discovering this issue and bringing it to my attention. With several articles now on the topic of using the CSS min-width expression, it is important to share any pertinent information that may affect its implementation. Hopefully, this article will prove helpful to those encountering issues with the technique.

Footnote

  • 1 When using the min-width expression, always increase the value of your desired minimum width by (at least) 1 pixel. This is necessary to prevent Internet Explorer crashing, this time for a completely different reason. For more information, check out this comment.

Dialogue

15 Responses Jump to comment form

1Joshua Richardson

September 14, 2008 at 3:51 pm

If you re-write the expression to take into account IE6’s box model then you can still use min-width s with IE6.

I.e. using a javascript function:

function IE6MinWidth( size, border, padding, margin )
{
       if (document.documentElement.clientWidth > (size+border+padding+margin+1))
       {
               return "auto";
       }
       else
       {
              return size+"px";
       }
}

Then change your expression to something like:

width: expression(IE6MinWidth(400,3,5,10));

Example page up at http://webcoders.com.au/minWidth.html

2H5N1

September 15, 2008 at 12:34 am

Simply forget about IE behaviour :)
I’m joking, but I enjoy people seeing their browser crashing and saying: “this site has something wrong” or “this site has a bug”. :D

By the way… hey Perishable, I’ve got problems sending posts with Firefox on this blog. I always have to use Opera or Internet Explorer (for my sadness) :D

3Jeff Starr

September 15, 2008 at 9:03 am

@Joshua: Excellent! Thanks for sharing with us. Your method further distinguishes all of the parameters involved with setting the proper width of the target element (i.e., width, border, padding, margin, etc.) and presents them from an enhanced JavaScript function that looks like it may be included unobtrusively via external file. Meanwhile, you have vastly improved the appearance and semantical quality of the stylesheet, thereby keeping file content nice, clean and organized. The CSS is still not valid, but at least it looks better. Thanks again — well done!

4Jeff Starr

September 15, 2008 at 9:10 am

@H5N1: Actually, you have the right idea.. as linked in the article, there is a rapidly growing movement to finally dump IE6 once and for all. I for one have embraced the idea, and no longer consider IE6 when designing/developing content. This article was posted for the sake of Chris (and other developers still required to work with IE6), who was sharp enough to discover the bug and thoughtful enough to share it with me. So I did my part and further transmitted the information to my readers, who will do with it as they please. Hopefully, it will help someone who is dealing with the issue.

As for the non-Firefox posting, what’s up with that? I post all of my comments via Firefox, and am sure that many others do as well (given the fact that this is the first I am hearing of the issue). Is this the only site that gives you trouble on Firefox? Could it be some security extension that you have installed? I don’t know, just throwing stuff out there.. would definitely like to help resolve the issue.

5H5N1

September 16, 2008 at 1:23 am

I get a FORBIDDEN error page if I submit a post via Firefox.
I’ve got no security extensin, but now I’m actually behind a (fu**ing) ISA server :)

6Jeff Starr

September 17, 2008 at 8:30 am

Could it be that the ISA server is sending blank-referer requests through Firefox? I currently have Apache configured to deny all no-referer requests for the comments.php file..

7H5N1

September 17, 2008 at 11:11 pm

I’m not the admin of ISA server.
I’m in my customer’ office! :)
I have no idea about what these buddies have done with their server :D

8Joshua Richardson

September 18, 2008 at 9:00 pm

Made the script simpler you can just call it like:

widthg: expression(IE6MinWidth(this,500));

And let it do the calculations for you. Makes writing the custom IE6 crap easier if you have to do it.

http://webcoders.com.au/minWidth.html

9Jeff Starr

September 21, 2008 at 8:41 am

Well keep an eye on it and let me know if you experience the issue anywhere else.. :)

10Christopher Ross

October 11, 2008 at 4:19 pm

Dealing with margins/padding in IE is one of the banes of my existence. I get hired by government departments to make their Internet Explorer only websites compatible with FireFox … it’s painful. Thanks for a great article.

11Jeff Starr

October 12, 2008 at 10:19 am

I feel your pain, brother! ;)

12Cooltad

October 22, 2008 at 10:37 am

If you use this: window.onload = checkAvailableWidth;window.onresize = checkAvailableWidth;function checkAvailableWidth(){var html = document.getElement("html");html.style.width = (document.body.clientWidth > 900)? "900px" : "auto";} then the problem should go away, along with having a much less resource-intensive way of solving IE6’s min and max problem. Javascript, of course.

13Jeff Starr

October 22, 2008 at 10:55 am

@Cooltad: that looks like a good way of dealing with the maximum width scenario, but you would need to either decrease the first value of 900 by at least a pixel, or else increase the second instance of 900 by at least a pixel in order for it to function properly. Likewise if you were to modify the script to account for the minimum width scenario, the first and second width values would need to differ in the same direction by at least one pixel for it to work. In any case, it’s another great tool to have available for dealing with good ‘ol IE6! ;)

14Cooltad

October 22, 2008 at 11:23 am

Wow, I made that script and totally forgot about the 1 pixel difference; thanks.

15Jeff Starr

October 22, 2008 at 12:13 pm

@Cooltad: yes, well, that information may have been less for you and others who are savvy and more for the other visitors who use this site as a technical reference for code examples, methods, etc. Anything I can do to alleviate confusion and unnecessary questions is a good thing! :)

Subscribe to comments on this post


Share your thoughts..

TopRead official comment policy

Contact Perishable Press

  • Contact Jeff via form

Search Perishable Press

About Perishable Press

Perishable Press is the virtual playground of Jeff Starr — visionary, founder and lead developer of Monzilla Media, a small web and graphic design company in the lush desert oasis of Moses Lake, Washington. Perishable Press features articles and tutorials on many aspects of digital design..

Read more..

Perishable on Twitter

better to have too many ideas and not enough time than the converse

Perishable on Tumblr

WordPress Tip for Multiple Themes

Sunday, 4 January 2009, 5:16 pm

If your site makes available multiple themes for users to choose from, remember to include the JavaScript (or any other required code) for any statistical applications that you might be using, such as Mint, Google Analytics, and so forth. I am not sure about the various WordPress statistics plugins, but they may need to be included as well. A good way to check if your stats plugin is tracking data across all themes is to either visit a few pages that you know others aren’t hitting, or else activate each of the alternate themes and check the source code of each one for the required code.

Earlier today, I realized that only several of my most recent themes included the required JavaScript for Mint and Google Analytics. I am now in the process of editing each of the 18 themes available for users at Perishable Press. Haven’t decided on whether or not both statistics apps are needed for all themes, but I will certainly be using at least one of them to keep an eye on everything.

Insane Christmas

Monday, 22 December 2008, 9:47 pm

For as long as I can remember, Christmas has always been a relatively peaceful affair. Sure there’s the usual holiday stress — traffic, shopping, presents, relatives, and all that goes with the preparation of a traditional celebration, but when it’s all said and done, you get to relax and enjoy the peace and harmony of gathering together and basking in the reason for the season: the birth of Christ.

This year, however, the stress factor has been kicked up a few notches, making for a rather insane Christmas if I do say so myself. In addition to the usual holiday chaos, we are currently purchasing a brand new home, and quickly realizing the incredible amount of work involved in the process. If you’ve ever bought a newly built home, you know exactly what I am talking about here.

Plus, as if all the paperwork, inspections, insurance, costs, and anxious anticipation weren’t enough to confound the usual holiday stress, we are also packing up everything, dealing with kids, working full-time jobs, and — beginning on Christmas Eve — moving into our new house.

It certainly is all a great joy and blessing to have such amazing things going on, but combined with the work that I do on the Web — blogging, designing, projects, helping people, and so on — it really becomes all too much rather quickly. We are doing are best to get through everything with our sanity intact, but I have to admit that this is the most insane Christmas I have ever experienced.

New (4G) Blacklist Now in Beta

Monday, 22 December 2008, 9:27 pm

Just a quick note to anyone interested in securing their websites against malicious activity, spam, and other nonsense. Several months after releasing my 3G Blacklist, I have finally begun work on the next incarnation of the blacklist: the 4G Firewall!

The first part of the blacklist is now ready for testing, and I plan on setting it up on Perishable Press within the next few days. While testing on my own site, I thought it would beneficial to also invite a few “beta” testers to run the code on their own site(s) as well.

So, if you have a site that receives its share of malicious attacks, and cracker exploits, drop me a line via the contact form at Perishable Press and I will send you the initial block of HTAccess directives. This version of the Blacklist is looking better than ever, and I look forward to releasing the complete version to the public early in 2009.

Thanks for the Free Traffic and Link Juice

Sunday, 7 December 2008, 1:26 pm

Just wanted to thank the fine folks at fafich.ufmg.br for all the free traffic and link juice. Thanks to their misapplication of my comprehensive canonicalization code, every non-canonical version of their 21,700 indexed pages points directly to my site, Perishable Press. This means that every one of their permalink URLs that is mistyped, lacks the “www” prefix, or contains the superfluous “index.php” file name is directed via permanent redirect directly to the home page of my site.

I have tried contacting the site owner(s) about this situation, but it has been over a week and I have yet to hear anything back. Hopefully, they will take notice soon and correct the issue by properly configuring their htaccess file, but in the meantime, I certainly don’t mind the extra link juice and free traffic! :)

No Plugin Needed for Feed Delay

Monday, 24 November 2008, 10:01 am

I recently saw a WordPress plugin that was designed to delay the publication of your WordPress feed by any specified time interval. While it is a good idea to carefully proofread your content before posting it, a plugin certainly is not required to do so.

As savvy WordPress users already know, WordPress has a built-in post-preview feature that enables authors to view their unpublished content as a published post. This enables authors to do any amount of proofreading and browser checking until they are satisfied with the results.

To do this, simply write your post as usual, and then click on the “Preview this post” button on the right-hand side of the screen. In older versions of WordPress (less than 2.5, I think), you actually need to save (without publishing!) the post first and then re-open it as if to continue editing. You will then see a “Preview »” link sort of hidden (due to poor CSS design) in the upper-right corner near the edit post field. Right-click on that link to open in new tab and you are good to go.

No extra plugin needed! :)

Read more on Tumblr..

Subscribe to Comments Recent Dialogue

  • Jeff Starr: Hi heywho, glad to hear you are doing well! ;) I wish I could join in the festivities.. it has been so long that I almost have forgot...
  • Rob Barrett: Thanks for posting about the Stealth Publish plugin -- just what I needed for my site. Works perfectly!...
  • Jeff Starr: Hi Chiwan, I got your email and have sent some information that may help you with this. Cheers, Jeff...
  • Chiwan: Hi. This is cool. So I can I replace the clock that comes with your Apathy theme with this clock? If that's not possible, how do ...
  • Brass Engraved: Thankyou very much for this, worked like a dream!...
  • Patrix: I'm using FeedBurner and the Feedsmith plugin for my filter blog, DesiPundit. I found your post via the WordPress page for RSS feeds ...
  • teddY: @Jessi Hance: Sorry to hear about your experience with Twitter spammers/flamers. I was once a victim of flamers and it sucks that peo...
  • heywho: Hey.... Very Nice...... I'm TOTALLY not a programmer..... but I have this thing I want to do...... so I just decided to start doi...
  • Rodrigo Nunes: NIce SEE MY BLOG http://designrn.wordpress.com/...
  • zubfatal: The Quintessential theme looks great to me, however when scrolling up or down on the page, it makes my laptop work harder than it sho...

Read more recent comments..

Attention: Do NOT follow this link!