Internet Explorer CSS limits

Monday, May 03, 2010 by ASP.NET AJAX Team | Comments 11

We all know that Internet Explorer has some bugs. Some of them can even bring down the entire rendering of your page. Like for instance the forced Standards mode crash bug in IE8, which can make your page to disappear completely if you have a floated container with max-height and overflow: scroll.

Two such bizarre bugs are these less known limitations in Internet Explorer - the 31 stylesheets per file and the 4095 selectors in a file.

Let’s first discuss the more widely known of the two -

31 stylesheets per file

If you are an ASP.NET developer and you develop mid-sized projects using App_Themes the chances are that you will hit this limit sooner than later. The problem is that App_Themes engine iterates through the skin folder and loads every CSS file kept in there regardless if it is needed or not. Every Internet Explorer version (including IE8 and unfortunately the IE9 preview released recently) on the other hand has the limit of loading only 31! stylesheets per file and thus fails to load all CSS files in a theme if they are more than 31. Another thing is that style tags in the HTML head or body are also counted as stylesheets (they are in fact parsed and created as different stylesheet objects) and are contributing to the problem - the more style tags you have, the less stylesheets you can include.

Unfortunately this bug is transferred to our RadControls as well. If you use a big number of RadControls and custom skins for them in a theme and just dump all the CSS files in it, you are in trouble.

Click here for a demo of a broken project which fails to load its CSS in all IE browsers. You can find the original project zipped at the end of this blog post.

There are several ways to fix the issue. One of them is to add a RadStyleSheetManager to your page - it has a stylesheet combining feature which is switched on by default and that will help you to partially reduce the number of loaded CSS files. However this method fixes the issue only to an extent (combines only the non-external skins) and may lead you to the second CSS limit in Internet Explorer which we will discuss soon enough.

Fortunately there is an alternative solution - it involves removing all the stylesheets from the App_Themes folder, moving them somewhere else within the site and creating there one or several CSS files in which you link the removed CSS files one by one using @import declarations.

Click here for a demo of a fixed site using the latter method. A zip with the original is awaiting you at the end of the article.

4095 selectors in a CSS file

A little less known limitation in all Internet Explorer browsers is the maximum number of possible selectors in a CSS file - 4095. This means that if you stumble upon the 31 stylesheets bug and decide to combine all your stylesheets to avoid it - at the end you will probably be in similar situation as in the beginning.

Unfortunately the only fix for this is splitting the file to several different CSS files and using the described above @import method to include them in your project.

Click here to see a test case of this bug or download the source below.


Update: As of Q3 2010 SP1 RadStyleSheetManager can combine stylesheets from an external folder, check this forum post for more information on how to enable this feature.


Update: As of Q3 2010 RadStyleSheetManager also monitors if the number of selectors exceeds 4095 and splits the combined stylesheet to two or more smaller ones.



Download all test-cases as project here

11 Comments

  • Vlad 04 May 2010
    Indeed three years later .. and the situation is the same :(
  • pugsley 04 May 2010
    Voted on both issues! I'll keep my fingers crossed :)
  • Kamen Bundev 10 May 2010
    Unfortunately the 4095 selectors per CSS file bug has been closed with Won't Fix in Microsoft Connect.
  • Kamen Bundev 05 Aug 2010
    And now the 31 stylesheets per file bug has been marked with Postponed and most likely the fix won't make it in Internet Explorer 9.
  • Kami Petersen 17 Aug 2010
    Is the following counted as one or two selectors, in the context of the 4095 selectors maximum?

    p.main {}
  • Kamen Bundev 17 Aug 2010
    This is one selector. A comma separates the selectors in one CSS rule. Check the W3C description of selectors and their syntax.
  • Nick Kusters 09 Sep 2010
    Microsoft Connect gives me a "Page not found" after logging in for both links.
  • Kamen Bundev 11 Sep 2010
    This probably happens if you are not subscribed for Internet Explorer Feedback Program in Microsoft Connect.
  • Kamen Bundev 30 Jun 2011
    Good news! Microsoft extended the three limits in IE10 Preview 2, check this bit of info in MSDN:
    http://msdn.microsoft.com/en-us/ie/hh272902#_CSSRemoveLimits

    The 4095 selectors limit is not mentioned, but our test cases show that its also extended.
  • devshed 13 Jul 2011
    Hello,
    I have the same issue with a specific page in a SharePoint 2007 installation. How would I go about correcting this problem ?
  • Stefan 13 Jul 2011
    devshed, the solutions are discussed in this very blog post as well as the comments which accompany it.

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)