Back in the day, all the CSS of the sites I built were in a single file: style.css. Things were easy. I built the site; I organized my styles; I knew where everything was.
Of course, then I discovered the power of leveraging an open-source community. And now there are innumerable developers writing the CSS for my websites. To keep things manageable, each module of functionality has its own stylesheet.
Unfortunately, once you get in the habit of having multiple stylesheets for your website, you will eventually run into a weird problem with Internet Explorer: some of your styles won’t apply. At all.
And if you have sufficient Google-fu, you’ll eventually discover this is a little known bug: Internet Explorer will only load the first 31 stylesheets and will ignore the rest. And this isn’t even limited to our usual suspect, IE6. All versions of IE have this limitation.
The Full Problem
and why I was insane enough to load 993 stylesheets on one page
Microsoft has a Knowledge Base article that says “All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer.” Not only is this KB article wrong (it’s 31, not 30), the full picture is more subtle.
As we all know, there are two ways to load a stylesheet on your web page. You can use a
<link /> tag or an
@import inside a
<link rel="stylesheet" href="style1.css" type="text/css" media="all" />
<style type="text/css" title="text/css" media="all">
In order to explore the limitations of combinations of @import and <link />, I created a test suite which you can download below.
IE CSS Stylesheet Limit Test Suite
- using <link> tags test
- using stylesheet <link> tags after other <link> tags test
- using <style> tags test
- using single <style> tag test
- using two <style> tags test
- using mixed <link> and <style> tags test
- using 993 stylesheets in 32 <style> tags test
What I discovered is that IE will load the stylesheets in the first 31
<link rel="stylesheet" /> or
<style></style> tags it encounters. And inside each of those
<style></style> tags it will only load the first 31
Fortunately, my CMS/framework of choice is Drupal and it has a built-in solution. If you enable the “Optimize CSS files” option on
admin/settings/performance, Drupal will aggregate all of your site’s stylesheets into one file. This will keep you well below IE’s limits. And, as a bonus, it will decrease server load and improve user-experienced performance of your site since the browser will have far fewer server connections to make in order to get your styles. Yay! But this option isn’t enabled by default (something we are currently working on fixing in Drupal 7), so make sure you go enable it on your production servers now!
Now developers may note that you’re still in a bind when developing your site and testing the CSS in Internet Explorer. Turn on CSS aggregation. Check site in IE. Modify CSS. Turn off CSS aggregation and then back on to re-aggregate the new CSS. Repeat. Drink until you forget. This is a serious PITA.
Fortunately, while we wait for a core fix in Drupal 7, we can use the IE CSS Optimizer module which I wrote specifically to test IE when developing a Drupal theme. Once you enable this module, simply choose the “Partial optimization for theme CSS development” option on admin/settings/performance. Now all the module CSS files will be aggregated into one file, giving you up to 30 stylesheets to play with in your theme without hitting IE’s limits.
And don’t forget to disable this module or choose “Full optimization” when you’re done developing your theme.
A workaround has been added to Drupal 7.0, so this is no longer a problem for Drupal 7 websites!