views:

223

answers:

11

CSS code formatting question: I have an old habit of adding a single space before the { starts a new CSS rule, like this:

body {
  font-size: 16px
}

the whitespaces like this one between body and { add up throughout the css.

Ideally, i'd prefer writing my rules like so (and thus saving whitespace):

body{
  font-size: 16px
}

Actually, I forget why I do that?

Note that I intentionally simplified the example here to show the whitespace. Of course there is no need to remove the space here from the file. But in real life projects with 2-3,000 rules, it might add up. Also, i presume that a single whitespace at that position will compress badly compared to the situation where it is removed.

I have long forgotten why I add that odd whitespace. Is it safe to remove? Which browser needed the whitespaces?

+1  A: 

it is very safe to remove it. If you did minification of you CSS it would remove them anyway and its good practise to do minify your CSS because it uses less bandwidth to move the CSS.

AutomatedTester
Safe, maybe, but not readable. A few spare bytes are not worth the readability and maintainability of your code.
Evan Kroske
The question was it safe to do so? Yes it is and 1 space between things won't make a difference to readability. If you are going to mark people down, do so to the people who do it all one 1 line!
AutomatedTester
+1  A: 

CSS skips any whitespace just like most programming languages. The extra space between the rule and the opening brace makes it easier to read. Personally the body{ looks ugly.

Where you put whitespace is up to you in CSS but it helps to make it readable.

Would you rather this:

body{
text-align:center;
font-size:10pt;
}

Or this?

body {
  text-align: center;
  font-size: 10pt;
}
Nick Bedford
+1  A: 

Some people to save space with css will put the rules on one line. This saves even more space as you don't have the new line characters and it's still quite readable.

body { font-size: 16px; font-weight: bold; }

Given that css is cached on most websites, even 2-3000 lines is not going to save you much space, and the performance increase will probably be negligible anyway.

EDIT: Changed most people to some people.

Deeksy
You can't count on caching, but I agree that the extra size is probably negligible.
Joel Potter
Not sure about "most people".. from what I can tell, its pretty divided.
Doug Neiner
I agree, "most people" is pretty loaded.
Deeksy
Also, a space character is the same number of bytes as a newline, no?
Kenny Winker
In windows, a new line is twice the bytes because it's a 2 character sequence. \r\n in that order if I remember correctly.
Deeksy
Most people do... how ever, most programmers don't.
Austin Kelley Way
A: 

Yes you can delete the spaces, in fact if you really want to save bytes, you could delete the space in "font-size:16px" as well.

Having said that, most servers and browsers gzip web content over the wire anyway, so the actual space saving would be minimal, probably not worth the cost in readability.

Nick
A: 
body {

Looks nicer (at least I think so).

Don't think about saving a few bytes, no matter how you format your code you should run it through a css compression utility before deploying it. Depending on your development environment you might be able to automate this task.

You should also configure your http server to use gzip compression for css (which will save a lot more than the whitespace between body and {).

Kimble
+10  A: 

You probably add the space because it reads/looks better.

Is safe to remove, no browser cares about the spaces. Any CSS compressor/optimizer will remove all those spaces, line breaks, etc.

Nowadays is rare for people to hand optimize their css, my suggestion is that make your css readable and mantainable and afterwards use an compressor/optimizer like YUI.

http://developer.yahoo.com/yui/compressor/

rjlopes
Thanks for answer. Actually, i already use YUI compressor directly on this project and have noticed that it removes that particular whitespace.
Jesper Rønn-Jensen
+4  A: 

Internet connections are fast. What kills you is the latency, sometimes a couple of seconds, while the server is waiting for your reply to make it across the net. The actual file transfer only takes a small fraction of the total time spent.

Given that a decent server setup will also send the css file gzipped I really don't think it'll make much difference in the greater scheme of things, considering all the other overheads involved. I'd stick with whatever looks best.

If the CSS file does get to the point where you think this would make a difference - as another poster said - minification is the way forwards. And don't forget a line break or tab takes as much space as a space.

Meep3D
+1  A: 

I would actually prefer the third option:

class
{
    ...
}

I find it more readable in the case where you have a style that applies to several classes, like this:

classA,
classB,
classC
{
    ...
}

But of course, in the end it's all about personal preference. Write the code in a way that is easy to read, understand and maintain and use a minifier/compressor to optimize for performance in a production environment.

Anders Fjeldstad
A: 

my favorite format is like so:

body{ margin:0;
      font-family:Verdana, Tahoma, Arial, sans-serif;
      padding:0;}

it just seems silly to me to create a new line only for the last '}' char. It still seems readable enough for me, but the downside is that not many CSS editors that try to auto format your CSS do it like that. I usually have to turn off CSS auto formatting or space it out manually.

jamauss
+1  A: 

if you have to worry about the number of unneeded spaces in a CSS file contributing to download bulk, you likely have additional issues brewing elsewhere ..

personally i always go for readability over worrying about these sorta things. i like spaces myself

Scott Evernden
A: 

Well I personally don't use white space on CSS at all and I don't find it hard to read, it is just a matter of getting used to it!

Evilalan