views:

111

answers:

4

The drumbeat of writing terse CSS code has been beaten into my head for years. For example:

Do this: .foo,.bar{ color:#f00; }

Not this: .foo{ color:#f00; } .bar{ color:#f00; }

With GZIP compression isn't it irrelevant how I write the above CSS? Since GZIP will create a dynamic library with color:#f00 as a single instance and use pointers to its multiple uses. I want to know because it is more convenient to repeat style definitions versus searching for an identical declaration.

A: 

I think you would still want to continue using the first method because it's more readable. Having a separate section for every thing will make you css bloated and difficult for people to get through it.

Joseph
Not if the two classes aren't related in any way, or it's likely that individual definitions will change and separate out over time.
Stefan Kendall
@iftrue I'm assuming they're related, otherwise why would they be grouped in the first place. Also, I wouldn't separate something out because it was "likely" that it would need separation in the future. If the future dictates separation, then let it happen when it happens, don't try to predict what you don't know.
Joseph
+5  A: 

You could use a css minifier before publishing and stop worrying about readability and file size.

GZIP will definitely have a bigger before/after size impact with 'sloppy' CSS files, but having the smallest file possible before compression, is alwaysmost of the time a good thingtm, as it will be marginally smaller.


Remember: Source code is for humans to read, and only incidentally for machines to run.

voyager
+1 for the reminder.
Kawa
Good quote. I say we let compressors do nasty hacks for us and let our production code shrink as compressors improve. :)
Stefan Kendall
+2  A: 

If that makes sense, go for it. Otherwise, I'd write the CSS in a logical fashion so it can be maintained properly, and then rely on gzip + a CSS compressor (like YUI Compressor) to handle the dirty work for you.

Stefan Kendall
+3  A: 

It depends on what you try to achieve:

  • If .foo and .bar need to have the same colour, you´re better of using the first method because it is easier to maintain.
  • If your first statement is a result of you manually optimizing your file-size, and it´s just a coincidence that .foo and .bar have the same properties, I would definitely split it and use the second solution.

In the end gzip and / or a css compressor will handle the file size so I wouldn´t worry about that.

Worry about maintainability and readability instead.

jeroen