15

I am using ASP.NET MVC to develop a site. The CSS file has grown to 88KB and is having a little more 5,000 lines. I noticed recently that styles added at the end are not there in the browser. Is there any size limit on CSS file or on the number of lines?

EDIT: Sorry I forgot to mention that this problem is occurring in Windows 7 both in FireFox and IE8.

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
dotcoder
  • 2,828
  • 10
  • 34
  • 50

11 Answers11

17

Internet Explorer has is said to have a limit of 4096 CSS rules per file. There's a limit on the addRule function that seems to apply to rules applied through a style sheet as well: Reference

Also, it has a limit on the number of style sheets you can embed in a single document. I think it is 20.

Unicron
  • 7,275
  • 1
  • 26
  • 19
  • 2
    While this was the case in IE4, is it still relevant in e.g. IE8? – Piskvor left the building Jul 09 '10 at 10:58
  • @Piskvor It seems to be the case at least in IE 7 according to this blog post: http://joshua.perina.com/africa/gambia/fajara/post/internet-explorer-css-file-size-limit There are also interesting comments. Can't find anything official on it, though. – Unicron Jul 09 '10 at 11:00
  • made a question about it, let's see if someone knowledgeable deems it interesting enough to answer: http://stackoverflow.com/questions/3211991/does-ie-8-have-a-limit-on-number-of-stylesheets-per-page – Piskvor left the building Jul 09 '10 at 11:07
  • 4
    The limit to the number of style sheets you can link to in a single page is 31 (although MSDN says its 30 here: http://support.microsoft.com/kb/262161) this limit also applies to inline styles. – Tor-Erik Jul 09 '10 at 11:16
  • @Piskvor [Seems to still be the case in IE9](http://stackoverflow.com/questions/13039108/ie9-fails-to-parse-css-file-having-more-than-4100-classes) – Martin Smith Oct 23 '12 at 21:02
14

I think that if you are having problems with the size of your css files then it is time to rethink your styling strategy. The C in CSS stands for cascading. Quite often when CSS files get too big it is due to styles not being re-used where appropriate and through poor use of the cascading behaviour.

I don't say this lightly. I have worked on some large, complex retail sites and currently on very complicated financial trading applications. Whenever I have come accross sites with more than a few hundred styles, we have achieved large improvements in design, reductions in complexity and improvement of maintainability by reducing css complexity.

One place to start is doing a Google sesarch on css reset. There are multiple different implementations, but they generally follow the theme of overriding the differences in layout for each of the browsers and removing arbitrary borders, margins and padding etc. Starting with a clean slate, if you will. Then you can go ahead and build up your styles from there, being careful to make the most of cascading and css chaining

Chaining is where you have more than one class on an element. eg:

<div class="box right small"></div>  

box might have some general styles that you might like to apply to many block elements such as div, h1...h6, p, ul, li, table, blockquote, pre, form. small is self explanatory right might simply be aligned to the right, but with a right padding of 4px. Whatever. The point is that you can have multiple classes per element and build up the styling from reusable building blocks - groupings of individual style settings. Otherwise known as classes.

On a very simple level, look for oportunities to combine styles:

so:

h1 {font-family: tahoma, color:#333333; font-size:1.4em;}  
h2 {font-family: tahoma, color:#333333; font-size:1.2em;}  
h3 {font-family: tahoma, color:#333333; font-size:1.0em;}  

becomes

h1, h2, h3 {font-family: tahoma, color: #333}  
h1 {font-size:1.4em;}  
h2 {font-size:1.2em;}  
h3 {font-size:1.0em;}  

Only, slightly smaller, but do this kind of thing lots of times and you can make a difference.

Also, Validate your css. This will help you spot errors in your code.

Daniel Dyson
  • 13,192
  • 6
  • 42
  • 73
  • I found that I missed an ending single quote for the image url. I thank all for the help. I will give it a try to make the CSS really cascading. – dotcoder Jul 12 '10 at 08:09
  • 3
    I dont understand why this is the accepted answer. The question was, "Is there any size limit on CSS file or on number of files?" This answer is "rethink your strategy." Non-sequitor. – Will Lanni Oct 05 '14 at 06:32
  • 3
    I agree with @WillLanni, this doesn't answer the question. – Mahn Jul 11 '15 at 15:07
  • @Mahn I don't really care. 13 people so far have found it useful. My goal on here is to answer what someone is asking but also to prevent people doing something that is plain stupid. The C stands for Cascading. It makes CSS powerful. It means you don't EVER have to reach a theoretical limit that no one should even be aware of or care about. – Daniel Dyson Jul 13 '15 at 04:15
  • I just refreshed myself on the SO 'answer' FAQ, and it does indeed state that "don't do that" answers are viable. @mahn what Daniel wrote is actually acceptable, even if it doesn't quite answer the original question (dealing with IE8, for example). I'll redact my comment officially here. http://stackoverflow.com/help/how-to-answer – Will Lanni Jul 14 '15 at 21:36
  • Thanks @Will. Removing the down votes would be nice too, but not necessary – Daniel Dyson Jul 15 '15 at 01:58
  • Unfortunately it's locked in :( – Will Lanni Jul 16 '15 at 02:37
9

What does the CSS validator say about your CSS rules?

Cut and paste the last "ineffective" rules of your stylesheet at its beginning: are some of them now working?

Obvious typo errors can be seen with a good text editor like PSPad, Notepad++, Scite: " ; /* */ and so on. They have a pretty limited scope like ; or are easily seen like a comment not closed. The less obvious one I encountered was not closing a parenthesis:

background: green url(img/dummy_gradient.png left top no-repeat;

Nothing worked till the next ) in my CSS rules 50 lines below!

FelipeAls
  • 21,711
  • 8
  • 54
  • 74
8

Your question is:

Is there any size limit on CSS file or on the number of lines?

The answer, for IE9 and lower, is yes, depending on what you mean by "lines" and "size limit".

  1. Size: the max size style sheet that IE9 and less will read is 288kb. Any styles after that mark do NOT get read. Read this article by Joshua Perina for a little more info on this.
  2. There is no max on the number of lines in a style sheet that I'm aware of. There is a max on the number of selectors. IE9 and lower stops reading (and may not even load a file, that info seems to be up in the air) at 4095 selectors per style sheet. (see linked article below)
  3. If by "lines" you mean how many lines of @import linked style sheets you can add per file, you can have 31 max style sheets imported in. Each stylesheet can have 31 max @imported style sheets. (see linked article below)
  4. @import nesting supports up to 4 levels deep.

This article on MSDN talks a little more about the max selector and linked style sheets, and how they came to those numbers when developing IE.

Will Lanni
  • 923
  • 12
  • 25
  • 1
    Regarding #2, nice distinction between the number of _rules_ vs _selectors_. This is verifiable via simple [JSFiddle experiments](http://jsfiddle.net/jasonkylefrank/sjwn0xfy/). Regarding #1, I'm not yet convinced of this file-size limit. In the article by Joshua that you provided, the file size may be a red herring - the number of selectors could still be the problem in his case. Finally, in the interest of completeness, it may be worth adding a #4, mentioning that (as the other article points out), there appears to be a limit of @import nesting of 4 levels deep. – Jason Frank Aug 12 '14 at 15:37
  • 1
    For #4, you mean _4_ levels deep right (as the MSDN article declares)? – Jason Frank Aug 13 '14 at 14:06
  • Argh. I edited wrong. Yes. Thanks for the catch, and yes I'm quoting the article. – Will Lanni Aug 13 '14 at 21:43
6

No there isn't a limit. Probably the file is being cached and that's reason you are not seeing modifications. Try clearing the browser cache or Ctrl+F5.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Even after refreshing as you said, it is not loading well. this is happening our Windows 7. – dotcoder Jul 09 '10 at 11:08
  • 1
    As Unicron posted IE (including IE 8) has several limits to CSS (although file size is not one of them), in this case the 4096 rules per file limit is probably the cause of the problem. http://blogs.telerik.com/blogs/posts/10-05-03/internet_explorer_css_limits.aspx – Tor-Erik Jul 09 '10 at 11:23
  • 1
    @Ju9OR assuming the css is not minified and normal indentation and line spacing are followed, with 5000 lines, even if they are the smallest of rules, you can write about 1600 rules. Or am I mistaken? –  Jul 09 '10 at 12:33
  • 2
    This answer is mistaken. There are several limits: 4095 max selectors per style sheet. 31 style sheets can be linked into a file. And the max kb size that IE will read is 288kb. Anything after that doesn't get read. – Will Lanni Sep 18 '13 at 18:54
  • 1
    @Dimitrov I believe this answer is mistaken. I Was having issues with IE9 appearing as though half the CSS was getting rendered. I resolved most of this by minifying the CSS. This confirms, in my case, that there IS a CSS limit in older IE (IE < 10) browsers. – Matt Jensen Oct 02 '14 at 00:39
  • @Dimitrov I'm sorry to say I can confirm this answer is mistaken. Please take a look at http://blesscss.com for a description of the IE limitation and a proposed solution. Not sure whom is downvoting my comments, but it is unconstructive to this conversation. – Matt Jensen Oct 30 '14 at 21:18
3

There is no limit for most normal browsers (as pointed out in most answers). Another very important consideration. If you compress your responses, in Apache I would use gzip or deflate, the file size is reduced dramatically. Since CSS is very repetitive, and for example, in your 5000 lines you use the word "color" 800 times, the compression will only send one instance of the word "color" to the browser with 799 references to the word in the compressed dictionary. So your file may be 88K on disk, but 9K over the wire.

Tools like FireFoxes Firebug are useful to see the compressed file size. I am sure IE has a http header viewing tool.

So to make my point, the 88K is the file size on disk, while, if using compression, you need to consider the file size in the response.

Christopher Altman
  • 4,868
  • 2
  • 33
  • 49
1

If you're not seeing additions to the bottom of the file reflected, see if you can spot where it breaks and look for stupid errors and mistakes. CSS will fail quite silently, but will still happily work up until it does.

This could be the cause of your worries.

Malabar Front
  • 624
  • 9
  • 14
0

There might be in some browsers (I’ve not heard of one, but it’s possible), but 88 KB is absolutely fine for a CSS file.

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
0

Theoretically, there isn't a limit.

Practically, most normal browsers (FF, Chrome, Opera, Safari) can handle whatever you throw at them. Some of the older and/or mobile browsers however (Access NetFront, for one - bundled with many mobile phones) run into problems with largish pages (about 100KB and above) and throw all kinds of errors.

TL;DR: No, unless you're trying to support all kinds of weird browsers.

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
0

There is of course a limit for what browsers can handle, but it's certainly a lot more than 88 kilobyte or 5000 lines. An educated guess would be something like a megabyte or 65535 lines... You are likely to run into performance problems long before you reach the limit.

The reason that you don't see changes appear is probably caching. You can use Ctrl+F5 to clear the cache for the page in your own browser (as Darin mentioned.)

We rename the CSS file when we publish a new version of it, to be sure that all visitors gets the latest version.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Have you validated your CSS? Some browsers will include styles up to the point of a syntax error (or certain syntax errors) and then effectively truncate the file for you, leading to just this behavior.

I'd also vote for refactoring your CSS, you can probably get away with a bit less . . .

Wyatt Barnett
  • 15,573
  • 3
  • 34
  • 53