Forum Moderators: not2easy

Message Too Old, No Replies

How Big is Your CSS?

What is a maximum file size for .css?

         

pageoneresults

3:37 am on May 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Something that I've not really seen a definitive answer on, but, when does a .css file become too large? Right now most of my files are in the 3k to 8k (max) range. At what point would you consider the file to be too large?

DrDoc

3:50 am on May 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, since the size of the CSS is counted towards the total size for the page... I would recommend not using files larger than a few kb. Most of my CSS files are less than 2kb.

My most advanced CSS is only 4.17kb.

Still, I'm not overly anxious about deleting spaces. My CSS is easy to read and follow, without making the file huge.

A key to keeping the file size down is to not apply a rule unless it is absolutely necessary to make the page load properly. Make use of shorthand declarations, inheritance, and default values as much as possible.

My CSS to HTML ratio is about 1:3

pageoneresults

4:02 am on May 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My CSS to HTML ratio is about 1:3

Is there a tool like Brett's Web Page Size Checker that you use to determine the ratio?

mipapage

7:00 pm on May 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey,

One site that I really like is cinnamon.nl. A very good use of css, try looking at it in NN4 vs. Moz or something similar. Good stuff, no? Now have a look at their css file:

25k!

Big.



Wrt file sizes, something I find interesting is that (I think this is right) alternative stylsheets are downloaded and cached along with the main stylesheet. Too bad you can't just download what you need when you need it, no?
Or am I out to lunch here?

ricfink

9:01 pm on May 21, 2003 (gmt 0)

10+ Year Member



Yes, you are right.
Alternate style sheets are requested along with the main page, and the other replaced elements. (Images, external scripts, et al.)

Can you load a new stylesheet on the fly without it first coming in along with the original page?
Yes you can using javascript. (Will work with NN7/Moz/Gecko, IE5+, and Opera 7.) Requires the right level of DOM support.

There may be thread about that somewhere on webmasterworld.
If not, let me know and I'll post a sample.

Nick_W

9:35 pm on May 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd like to see that please? ;-)

Nick

grahamstewart

9:50 pm on May 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My current project stats are

Home page HTML: 3539 bytes
Global CSS file: 8569 bytes

So I guess my CSS to HTML ratio is about 2.4 : 1

I don't think it really matters how big your CSS file is.
What really matters is the total size of the page.

ricfink

2:16 pm on May 22, 2003 (gmt 0)

10+ Year Member



As per Nick's request.
This is one variation of the code I use to switch style sheets on the fly. (I've spent a lot of time experimenting, there's quite a few variations.)
I have a special "tool" page written that I use to help build style sheets on a lot of the pages I do. It has the javascript functions and html in a "click and paste" format. (IE only.)
So just ignore the reference to "clicking on any of the links will copy the needed code" and just follow the instructions as presented.

css_builder.htm
----------------
This page outlines a method for building and debugging style sheets using two external style sheets. The first stylesheet contains the initial "base" code. The second stylesheet is used to add rules as you go along. At the end of development, the two stylesheets can be combined into one and the debug functions and links removed from the page.

Clicking on any of the links will copy the needed code to the Windows clipboard to insert into the page. (Remember to take everything out when the page is fully styled!)

1) Create two stylesheets - baserules.css and newrules.css.

2)Add these two link elements into the head:
<link rel="stylesheet" id="baserules" type="text/css" media="screen" href="baserules.css">
<link rel="stylesheet" id="newrules" type="text/css" media="screen" href="baserules.css">

2) Add this function into the head.

function addNewRules(newfile) {
var _x = newfile + '?' + Math.random().toString().slice(3,9);
var x = document.getElementById('newrules');
var y = document.getElementById('baserules');
var _q = y.getAttribute('href');
var _z = x.getAttribute('href');
if (newfile == "baserules.css") {
x.setAttribute('href',newfile);
y.setAttribute('href',newfile);
} else {
x.setAttribute('href',_x)
y.setAttribute('href','baserules.css')
}
}

4) Add these links somewhere into the body text of the page you're styling. (So it blends right into the existing text on the page.)
<a href="javascript:void(addNewRules('baserules.css'));">Base Rules</a>
<a href="javascript:void(addNewRules('newrules.css'));">New Rules</a>
--------------------------------------------------------
- end css_builder.htm -

This technique applies the stylesheets in two ways - the first is only the baserules.css sheet. The second is baserules.css cascaded with newrules.css. However, with just a little modification you can have it load baserules.css OR newrules.css.
You could also add more stylesheets to the mix if you want.
Like I said, there's a lot of variations.
Take a page, any page, add in this code rename the stylesheets accordingly and check it out.

Happy to explain anything about it that's unclear.

rich

Oaf357

6:01 pm on May 22, 2003 (gmt 0)

10+ Year Member



Honestly.

I'd say 5K is too big. That's just me though. If you're trying to do too much with CSS it becomes apparent in its file size. If you're one of the few people that has a stylesheet above 10K a site redesign is a must using fewer styles and uniformity across the site to ease not only the user experience but design as well.

My current stylesheet for my personal site is 1.46 KB but I've written some that have been pushing that 5 K limit I try and set.

mipapage

2:43 am on May 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oaf357,

Although I agree with you, 5k is a nice limit, If you're over that it may not be that you are trying to do too much.
I just went through and dropped the screen file for a site I'm working on from 8.75k to 4.66k. First eliminating comments, then grouping, then whitespace, and finally line breaks. I kept a backup for each stage, just in case ;-]


Question related to the alternative stylsheets... Does anyone know if the 'print' stylesheet comes down at first download as well?

bunltd

3:00 am on May 23, 2003 (gmt 0)

10+ Year Member



Mine run in the 3-5K range. Current use on my site: 7.5K html, 3K CSS... Could probably lighten the CSS up by doing a little more housekeeping.

RicFink: gonna try your snippet - fun.

LisaB

ricfink

1:43 pm on May 23, 2003 (gmt 0)

10+ Year Member



mipapage,

Any stylesheet specified within a LINK tag is going to get requested as the browser parses the page.
This includes sheets specified within a LINK tag having the media=print attribute, as well.

(Theoretically, you should be able to apply a print stylesheet to the document by changing that attribute to media=screen using javascript.

In fact, I seem to remember trying that out - to provide a page level print-preview button or something - but as usual had trouble with cross-browser support.)

mipapage

2:12 pm on May 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



using javascript...trouble with cross-browser support.

Hmmm, I'm sure you could 'step-over' having to download the extra sheet with php as well...thus avoiding the c-b-b-s.

ruserious

10:13 pm on May 23, 2003 (gmt 0)

10+ Year Member



You guys just made me curious. I was wondering wether background-img from alternative stylesheets are also fetched. Well, luckily they are not (for IE6 that is). :)

However I am somewhat surprised about the (soft) limits about filesize you are discussing. I'd say it totally depends (well, that's a great answer isn't it :D), but I'd have no problem with CSS files even larger than 25k. What's the drawback? They usually get cached anyhow by the browser.

What I'd stuff in there? Well, whatever I needed *G* When I first started with css I would do everything with classes, which I'd use globally across files and different elements. However after having seen a few visually very appealing graphically oriented CSS-styled sites and studying their code, I started understanding what possibilites you really have when you go with id (ok, you could do it with unique classes, too, but ids are better semantically IMHO). Now I am playing with that for a new site. When you 30 or something pages, and you want to move a lot of layout-control over the CSS-files, you'll gather quite a few ids. Add to that general styling of standard tags, classes, commenting, a few ugly hacks... you'll get quite a few kbs really fast.

And like I said, I really don't see anything wrong or problematic with it.

mipapage

10:03 pm on May 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, all of this talk about size got me to wondering, and when reviewing my copy of Speed Up Your Website by Andy King (see the books cool accompanying website here: <behave!>, I found something interesting:

In the book, Andy talks a lot about minimizing http requests. Since each file and image etc. = 1 request, optimally having one stylesheet is best. He goes on to talk about packet size (yikes!):

The maximum transmission unit (MTU), or the size of a packet, is typically between 576 and 1,500 bytes depending on connection speed.

So, 576bytes for lower connections, 1500bytes for faster ones. For the first http-request (ie, first packet) you subtract the header size (unique for each page, in his example 400bytes) and "a TCP/IP overhead of 40 bytes" you get 1160, then 1560 for subsequent packets. Even smaller sizes for slower connections.

The result here is that be careful, because if you choose an arbitrary number like 5k, you're giving away (on a high speed line) 540 bytes of empty space in the 4th packet that is used to send your file.

Think of it as some free space then to muck around and add a bit more spice to your site if you've got the room, or, cut down to the next packet size limit.


Packet Sizes For High speed lines (.4k header):
  • 1st: 1160
  • 2nd: 2620
  • 3rd: 4080
  • 4th: 5540
And so on...

[edited by: Nick_W at 10:14 pm (utc) on May 25, 2003]

mipapage

7:23 am on May 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick edit to the above:

1. The book is called Speed Up Your Site.

2. WRT packet sizes, I think that what I wrote could be mis-interpreted:

High-speed packets run about 1500 bytes.

For the first packet, you subtract the header and tcp/ip overhead (40 bytes).

Subsequent packets subtract only the tcp/ip overhead...

So, assuming a 400byte header, a 5k css file will go 'down the pipe' as follows:

1.16k in the 1st packet, another 1.46k in the second packet, another 1.46k in the third packet, 1.46k in the fourth, then in the fifth packet, you would send along the remaining 0.91bytes, leaving 550 bytes of space empty.


Not too sure what you could do with that extra space, if your page works the way you want it to, but I thought that this at least gives us something concrete to base our file size limits on...