views:

272

answers:

12

When I first started writing CSS, I was writing it in an expanded form

div.class {
    margin:      10px 5px 3px;
    border:      1px solid #333;
    font-weight: bold;
    }
    .class .subclass {
        text-align:right;
        }

but now I find myself writing css like this: (Example from code I'm actually writing now)

.object1 {}
    .scrollButton{width:44px;height:135px;}
        .scrollButton img {padding:51px 0 0 23px;}
.object2 {width:165px;height:94px;margin:15px 0 0 23px;padding:15px 0 0 10px;background:#fff;}
    .featuredObject .symbol{line-height:30px; padding-top:6px;}
    .featuredObject .value {width:90px;}
        .featuredObject .valueChange {padding:5px 0 0 0;}
        .featuredObject img {position:absolute;margin:32px 0 0 107px;}

and I'm beginning to worry because a lot of the time I see the first form done in examples online, while I find the second form a lot easier for me to work with. It has a lower vertical height, so I can see all the classes at a glance with less scrolling, the tabulation of the hierarchy seems more apparent, and it looks more like code I'd write with javascript or html. Is this a valid way of doing code, or to keep with standards when putting it online should I use the vertical form instead?

+11  A: 

Well, here is what say the most :)

summary: css-tricks.com ran a poll. By a margin of roughly 3 to 1, most people preferred multi-line over single line css styles.

Sarfraz
If that website will go offline (probably not, but you never know) then this answer is very useless. If you, please, quote some information from that source and put it in your answer, that would be awesome! :-)
Pindatjuh
@Pindatjuh: And if this site goes offline then *all* the answers here will be useless too. So what?
Donal Fellows
If this site goes offline then there are, at least, the various dump files (released monthly) to keep the answers useful.
David Thomas
+1  A: 

I dont know about you but I like the vertical mode during dev as it is far more easier to read for me.

However, in prod, you wanna compress your css to reduce payload and hence, the second style makes sense. Mostly, you would be using some CSS compressor to do this.

Rajat
Won't it be cached nine times out of ten anyways?
drachenstern
I don't think caching is an excuse to not compress your CSS/JS files. Why send extra white-space as payload for that first page load ?Plus, its darn easy to compress and there are several tools out there to do that for you.
Rajat
Another reason to minify/compress your css is that not only do you gain faster download time, you also get faster browser execution/interpretation of your css.ref: http://code.google.com/speed/page-speed/docs/payload.html#MinifyCSS
Rajat
Yes, but minifying isn't for human beings. So which style you use doesn't matter from that angle.
D_N
+1  A: 

i like to write css in multi line. because this is easier to write and read. we can find error as early as possible and a look of view is nice with indentation . mostly when a designer work with css and gave to developer to develop site than developer can understand easily. so i think multi line css is better way to work.

kc rajput
+8  A: 

I personally prefer the first style. I like things that are easy to read and I don't mind scrolling. The dense nature of the second style slows down my reading, my ability to pick out the items that I'm interested in.

There certainly are trade offs to be considered with CSS due to the file size. CSS can be compressed. I find the size of CSS files to be the least of my worries with the sites I've built so far.

Ultimately, the important thing is that whichever style you choose to use is to be consistent. That consistency will make your life simpler when you have to update your CSS or when another developer has to update your CSS.

Mike Chess
+1 for the consistency point. I'd take that further still, keeping the individual properties in a consistent order. (Personally, I do position, box model, so on, and finally any proprietary ones.)
Ed Daniel
ditto on the consistency, and +1 for the expatiation, too. I'd say that should apply to all areas of coding, too, of course.
Wayne Werner
"I don't mind scrolling" - what everyone unbelievably neglects is that with thesingle-line method, you still have to scroll - **horizontally**. And IMO that is much worse and much harder to pick out the styles you want.
DisgruntledGoat
+5  A: 

Indicating the hierarchy using indentation is not a bad idea. However, you should be careful that you don't fool yourself. In your example, you may be assuming that .scrollButton is always within .object1. But CSS doesn't obey that rule. If you used a .scrollButton class outside of .object1, it would still get the styles.

JW
Good eye. ( +1 )
D_N
A: 

I prefer the second style, but be aware that it's a style. In the same way that some people prefer

function (arg)
{

   body();

}

to

function(arg){
   body();
}

I don't get it, myself. The argument is "it's easier to read", and my response is consistently "... for you". As a note, I get the feeling that this is why so many examples use the more-whitespace version; it has the reputation (if not confirmed property) of being easier to read.

Pick the one you like and stick with it. If you have a team to cooperate with, try to get to consensus, or barring that, write some auto-formatting scripts and stay out of each other's way. It's not like it's terribly difficult to mechanically transform one into the other.

Inaimathi
A: 

The style you write in is your choice(I prefer multi line) but as Rajat said you want to remove any extra whitespace after dev. Anytime you can reduce file size and payload you are doing your site and your visitors a favor.

BKCOHEN
A: 

I think it also depends on your editor. I use multi line formatting and condense every definition with Vim's folding (I set up folding marks to be { and }) so I get one tag/class/id per line, expandable when needed.

Using comments to identify "sections" I get a very clean look with minimal vertical scroll while maintaining the readability of multi line on expanded definitions.

kemp
A: 

I just want to point out that Textmate has an option that allows you to easily switch between these two styles by selecting an area and pressing Ctrl-Q/Ctrl-Alt-Q to expand/collapse. As a consequence I have come to find that I prefer my CSS collapsed unless I am writing or deep debugging a specific section. But, with the ability to easily switch between he two I see that both ways are useful for different circumstances.

mVChr
A: 

I prefer multiline right up until we deploy. At that point I want it minified.

Chris Lively
A: 

I personally find both of your examples hard to read, especially the second one.

Multi-line is easier to follow, and indentation can be misleading as CSS is not necessarily applied in that way. Your indentation may lead you to believe it is.

I prefer the basic tried and true method of multi-line, with reasonable/logical order:

div.class 
{
    margin: 10px 5px 3px;
    border: 1px solid #333;
    font-weight: bold;
}
.class
{
    text-align: center;
    margin-left: 10px;
}
.class .subclass 
{
    text-align:right;
}

Takes up a little more space and requires a little scrolling to take in, but is easy to follow. Those worried about optimization can always use CSS shrinking tools for production CSS files.

In the end as long as you are very consistent with your work and across a team (if applicable) then no answer is more correct.

KP
A: 

Perhaps, when you have multiple selectors and one rule, like this:

#header li a, #header li span {
    display:inline-block;
}

So, I prefer to do:

#header li a,
#header li span {
    display:inline-block;
}
gryzzly