CSS Coding Standards

Events happening in the community are now at Drupal community events on www.drupal.org.
alanburke's picture

Hi all,
I'm at the documentation sprint.
I would like to propose some CSS coding standards, to accompany the PHP coding standards, along with the proposed Javascript standards.

I've added a handbook page at
http://drupal.org/node/302199
Discussion should be in this thread, and the handbook page updated as necessary.

Stella, the Coder module maintainer, has promised to add this to Coder module, once the dust settles on the dissussion.

Comments

Thanks Nick!

alanburke's picture

First up,
Thanks to Nick Lewis for kicking this off at
http://www.nicklewis.org/node/966.

The standards are slightly different to those he prosposed.

For the most part, the standards describe how core css files currently function.
The main expection is that I propose a single line between each rule, to aid readability.

I do realise that just adding a standards page, without prolonged discussion, is perhaps controversial.

I don't anticpate changing Drupal 6 core css files at all.

Hopefully we will reach some agreement,
then the rules can be added to the Coder module for Drupal 7.
Once that happens, we can create patches, if necessary, for core files.

Update - May also apply to Drupal 6

alanburke's picture

Myself and Stella spoke to Gabor.

Stella intends to add the CSS and Javascript rules to Drupal 6 version of coder.

Once that happens we can work on patches for core.

In principle, Gabor agrees that changing core css code to comply with coding standards does not constitute an API change [even if the standards were added retrospectively].

So these standards may yet apply to core for Drupal 6.

I'm not a huge fan of the

morbus iff's picture

I'm not a huge fan of the "multiple selectors must be on one line". Very long selectors could make that line hundreds of lines long. Something like, for example:

#casetracker-status-cant-reproduce td.sorted.odd, #casetracker-status-resolved td.sorted.odd, #casetracker-status-needs-work td.sorted.odd, [etc.] {

versus:

#casetracker-status-cant-reproduce td.sorted.odd,
#casetracker-status-resolved td.sorted.odd,
#casetracker-status-needs-work td.sorted.odd {

Gah, posted before I wanted

morbus iff's picture

Gah, posted before I wanted to (usability problem: I /expect/ a Preview button because I don't "see" the live Preview). Anyways, I'd want it both ways: small selectors can be placed on one line, but very large ones like the above should be split for readability's sake.

+1

sun's picture

...however, hard time to find a rule for this, because what's a long line? Exceeding 80 chars?

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

hundreds of lines

xurizaemon's picture

according to Morbus's comment above, a very long line can be hundreds of lines long ;>

If my comments have helped you, please pay it forward!
Use issue queues to discuss module issues - this will help your questions assist others (including yourself!) in future.

One line is a terrible

tjholowaychuk's picture

One line is a terrible standard, I vote no to that as well... Please remember we DO have CSS minification, whitespace is irrelevant, readability is much more important .


Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

I agree

mike stewart's picture

readability is key. separate lines can be helpful in identifying typos, too.

--
mike stewart { twitter: @MediaDoneRight | IRC nick: mike stewart }

Each property should end in

sun's picture

Each property should end in a semi-colon.
[Even though this isn't strictly necessary for the last rule, it aids readability.]

Please remove that (too limited) justification in brackets. Just like the trailing comma in multi-line arrays in PHP, the standard also makes patches against existing code cleaner.

There should be a single empty line between rules.

-1 on this rule, because most stylesheets become unclear this way; however, +1 for separating related styles into "blocks" using one or two single empty lines between rules.
For example,

.foo-bar-baz .foobar .beer {
  margin: 0 0 2em 0;
}


.foo-bar-baz .foobar .beer div {
  overflow: hidden;
}


.baz-bar-foo .barfoo .beer {
  margin: 0 0 2em 0;
}


.foo-bar-baz .foobar .beer ul {
  overflow: hidden;
}

is harder to distinguish than blocks with related styles:
.foo-bar-baz .foobar .beer {
  margin: 0 0 2em 0;
}
.foo-bar-baz .foobar .beer div {
  overflow: hidden;
}
.foo-bar-baz .foobar .beer ul {
  overflow: hidden;
}


.baz-bar-foo .barfoo .beer {
  margin: 0 0 2em 0;
}

meh... the <code> filter does not properly display these empty lines and blocks. Hope you get the point anyway - a) group styles that belong together (i.e. apply on the same target, f.e. a block), and b) do not separate styles within one grouping block, but separate styles for different target elements or output objects with empty lines.

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

I completely agree with

brenda003's picture

I completely agree with this. I don't think it aids readability to have a single line separator at all, actually. It just takes up more space on your screen. It actually seems to be very common right now to only use a single empty line separator for different "sections", though I can't swear on it, I'm sure that's how a few of the big themes (at least used to) do it.

I'm more of a fan of using

geerlingguy's picture

I'm more of a fan of using comments to differentiate sections of selector blocks... it's simply harder (in my mind) to find a selector when they're all butted up against each other.

Indentation

Zarabadoo's picture

I have been finding indentation on related and nested blocks to provide better readability. I personally +1 the single space between declarations, but between distinct blocks I use comments. Multiline comments for the major blocks, and then single line comments for the tiny bits that need more explanation. Using the same example, it would be something like this:

/
* Styles for the beer within the foo-bar-baz
/
.foo-bar-baz .foobar .beer {
  margin: 0 0 2em 0;
}

  .foo-bar-baz .foobar .beer div {
    overflow: hidden;
  }

    /
The ul elements need some specific styles for this */
    .foo-bar-baz .foobar .beer ul {
      overflow: hidden;
    }

/

* The beer in the baz-bar-foo needs to be styled differently
*/
.baz-bar-foo .barfoo .beer {
  margin: 0 0 2em 0;
}

I find when there starts to be 1000+ lines of css to dig through, this comes in very handy.

Note: The spacing in the code element seems to be off.

agree with indentation

mike stewart's picture

yes, another nicety when it comes to readability. although some might argue this will make maintenance more difficult... but I think it definitely helps readability.

edit: ok, I probably should have read the whole thread before posting. but after reading everything in the thread, i still feel this aids readability, but don't feel as strongly about it. probably because much of my css reading comes through a parser such as firebug

--
mike stewart { twitter: @MediaDoneRight | IRC nick: mike stewart }

I've never seen this used

brenda003's picture

I've never seen this used before, it looks very "wrong" to me.

This seems like a good idea,

Nick Lewis's picture

This seems like a good idea, that will go satanic in the wrong hands. The big worries for me is:
1. the indention will often not reflect reality, because of the laziness that we developers are prone to while writing css.
2. The format encourages bad practices, in some ways....

For example, say I wanted unique styles for blogs.... this format encourages me to do it like this....

body#blog {
color:#222222;
}
  #blog-page-title {
    font-size:1.5em;
  }
  .blog-links {
    font-size:1.3em;
  }
  h3.blog-title {
   font-size:1.1em;
  }

This is silly. I'm using indentation, instead of #ids, and document comments to delineate sections.
/*--------------- [ BLOG STYLES } ----------------- */
body#blog {
color:#222222;
}

body#blog #page-title {
    font-size:1.5em;
}

body#blog .links {
    font-size:1.3em;
}

body#blog .title {
   font-size:1.1em;
}

One could argue the latter is harder to read... but one cannot disagree that its closer to how a browser will actually interpret it. Who knows where an indented class actually may be used if someone isn't paying 100% attention to the classes and ids in their markup. At least by using unique #id's as "indentation" you ensure that the ID must be present for the style to apply.

Also, indented CSS bugs me personally in the same way as a java-style CSS does... e.g.

#content
{
  color:#000000;
}

It just looks wrong... and I can't work with the code until I fix ever last line of formatting... While its a matter of taste, arguably, we should allow nothing that freaks people out to the point where they have to reformat every line, or take a cocktail of tranquilizers before they are comfortable working with it....
++++++++++++++++++++++++++++++++
"I'm not concerned about all hell breaking loose, but that a PART of hell will break loose... it'll be much harder to detect." - George Carlin
++++++++++++++++++++++++++++++++
work: http://www.onnetworks.com
blog: http://www.nickewis.org


"We are all worms. But I believe that I am a glow-worm." - Winston Churchill
work: http://www.chapterthree.com
blog: http://www.nicklewis.org

For clarification, the

Zarabadoo's picture

For clarification, the indenting is intended for pieces that specifically require relationship to another element to take effect. So unless it is specifically linked to another element, it really should not be indented.

Discussing it any further though seems fruitless at this point though. I believe there are enough points against it to successfully take it out of the pool.

As for the comfort factor, it is inherent with standards that there are going to be people that do not like it. There are a few prominent people that do not like the Drupal PHP coding standards, and thus do not use them in their own modules. They suck it up when doing core work though, and that is where it really counts. I will probably end up doing a bit of the same thing when it comes to my goofball indenting on my own contrib themes.

+1 for removing the

morbus iff's picture

+1 for removing the "justification in brackets".

As for "There should be a single empty line between rules.", I'm also +1 to this (as opposed to sun). Sun seems to be using the "no lines between rules" as an indication of a "block" of CSS-related selectors. My opinion on that is: you can accomplish the block just as well, and far more clearly, with comments detailing what the block is. So I'm +1 to a single empty line, and would challenge Sun to clearly document his "blocks" (which I certainly agree with) with explicit comments, instead of inferred no-lines.

Well, 1) just like brenda

sun's picture

Well,

1) just like brenda mentioned above, my point is that a CSS file gets unnecessarily cluttered if there are needless empty lines between styles (selectors).

2) Of course one should use comments to separate sections. However, one is able to use blank lines to visually separate sub-sections within a (commented) section, which do not need or justify additional comments.

3) We are also not cluttering PHP files with needless blank lines.

So in the end, I'm actually waiting for three counter-arguments instead of personal +1 votes on empty lines.

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

I guess my main reason is

stella's picture

I guess my main reason is that I find it easier to read CSS files if there's a blank line between rules, so for me readability is key here.

Readability?

sun's picture

Hm. I cannot see a significantly better readability between:

.foo-bar-baz .foobar .beer {
  margin: 0 0 2em 0;
}

.foo-bar-baz .foobar .beer div {
  overflow: hidden;
}

.foo-bar-baz .foobar .beer ul {
  overflow: hidden;
}

and
.foo-bar-baz .foobar .beer {
  margin: 0 0 2em 0;
}
.foo-bar-baz .foobar .beer div {
  overflow: hidden;
}
.foo-bar-baz .foobar .beer ul {
  overflow: hidden;
}

All styles are separated by an almost (}) blank line anyway. IMHO, it just takes more space.

If we cannot find a consensus on this one, we should drop this rule.

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

I personally find it much

Zarabadoo's picture

I personally find it much easier to read. Plus that extra space would be taken out with compression.

I'm surprised, I don't find

brenda003's picture

I'm surprised, I don't find it easier to read at all. But it's not something I'm adamantly set against, it just creates more scrolling.

I find it more readable

jix_'s picture

I find it more readable without the blank lines as well. Even if you put comments above certain style groups. The blank lines could cause some confusion about which styles a certain comment applies to.

/* Some group */
.foo-bar-baz .foobar .beer {
  margin: 0 0 2em 0;
}

.foo-bar-baz .foobar .beer div {
  overflow: hidden;
}

.foo-bar-baz .foobar .beer ul {
  overflow: hidden;
}

/* Another group */
.foo-bar-baz .foobar .beer {
  margin: 0 0 2em 0;
}

.foo-bar-baz .foobar .beer div {
  overflow: hidden;
}

.foo-bar-baz .foobar .beer ul {
  overflow: hidden;
}

vs.

/* Some group */
.foo-bar-baz .foobar .beer {
  margin: 0 0 2em 0;
}
.foo-bar-baz .foobar .beer div {
  overflow: hidden;
}
.foo-bar-baz .foobar .beer ul {
  overflow: hidden;
}

/* Another group */
.foo-bar-baz .foobar .beer {
  margin: 0 0 2em 0;
}
.foo-bar-baz .foobar .beer div {
  overflow: hidden;
}
.foo-bar-baz .foobar .beer ul {
  overflow: hidden;
}

The example with blank lines between styles makes it seem like the comment applies only to the styles directly below the comment, instead of the whole group.

If you'd use blank lines only to seperate groups, like in the 2nd example, there'd be less chance of confusion. And, as sun pointed out earlier, it'd be consistent with the way we code PHP (and JS) in Drupal.

Quick repsonses

alanburke's picture

Great to see some instant feedback!
[Addi was right! - Put it straight in the handbook - and people will talk!]

@Morbus
While I see some justification is being able to use both multiple selectors on one line,
but use multiple lines if necessary, I'd rather be consistent.

I propose single selector per line,
space follow by brace after the last selector.

I really like the single line between rules, and blocks of related rules should be commented.

How about a comment after the last line in a group of rules to indicate the end of that group?

In general, should the PHP comment guidelines also apply to CSS?
[I spoke to Neil Drumm - he is considering improving api.drupal.org to include js and css files too, btw].

Justification in brackets removed.

Alan

There is no reason CSS could

tjholowaychuk's picture

There is no reason CSS could not use a JavaDoc-like syntax. I have not had time to work on my CSSDoc syntax specification however that was the goal..
This basic structure is what I use on all projects http://vision-media.ca/sites/all/themes/light/style.css

Laying out the stylesheet like really is quite logical and simplistic, should not be an issue for anyone to maintain. It is all about maintainability, readability, not saving space or performance.


Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

I also disagree with Nick's

tjholowaychuk's picture

I also disagree with Nick's acceptance towards single-liners. Just because it may only have one rule at the moment does not mean it will stay that way, and most likely will not. So bumping down those lines is really just a hassle.


Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

Good point. The main reason

Nick Lewis's picture

Good point. The main reason I think its important, however, is that certain rules are in fact easier to read when written like that, consider headings:

h1 {1.8em;}
h2 {1.6em;}
h3 {1.4em;}
h4 {1.3em;}
h5 {1.2em;}
h6 {1.1em:}

vs.
h1 {
  1.8em;
}

h2 {
  1.6em;
}

h3 {
1.4em;
}

h4 {
  1.3em;
}

h5 {
  1.2em;
}

h6 {
1.1em:
}

Clearly, most people will prefer the first version. I think the great writing advice, "Sooner break the rules, then say something outright barbarous" applies to CSS. There are even cases where writing rules on a single line makes sense... for example a font declaration.
body {
font-family: "lucida grande", "lucida unicode", sans-serif";
}
h1, h2, h3, h4, h5, h6 {
font-family: "helvetica", sans-serif;
}

Vs.
h1,
h2,
h3,
h4,
h5,
h6 {
font-family: "helvetica", sans-serif;
}

Having the rules on the same line, in the former version, makes sense. The idea here is not be nazis about it to the point where our rules get in the way of sensible judgement. Rather, its to have standards to help avoid lines that look like this:
.highlight-text, input#some-field, #important-content, .grid-1a #content-type-secondary-content, #page-elements {font-size:24px; background-image:url(1111tb.png); float:left;}

++++++++++++++++++++++++++++++++
"I'm not concerned about all hell breaking loose, but that a PART of hell will break loose... it'll be much harder to detect." - George Carlin
++++++++++++++++++++++++++++++++
work: http://www.onnetworks.com
blog: http://www.nicklewis.org


"We are all worms. But I believe that I am a glow-worm." - Winston Churchill
work: http://www.chapterthree.com
blog: http://www.nicklewis.org

True! I am not going to lie

tjholowaychuk's picture

True! I am not going to lie h1-h6 I do put on a single line. I suppose the primary reason for caring less about it in that manor would be that rather than highly altering h1-h6 you would maybe:

#primary h1 {

}

etc. Tough call! but like you mention it does not look to pretty taking up a line each either. I guess the big problem is when it gets into having white space within the selector(s) that the one line becomes unwieldy.


Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

We could just not specify a

stella's picture

We could just not specify a standard for this? Or maybe just have a vague one saying something like "multiple one word selectors can be all on one line but longer selectors should each be on their own line".

So anything that is one word, e.g. "h1", "h2", etc, in the above example would be on one line, but anything that requires 2 parts separated by a space would constitute a longer selector and would each have to be on a separate line.

Coder should be able to handle that, though someone would need to make sure the description for that rule in the docs is clearer!

It's just an idea, but it means we could avail of the benefits in each method.

Cheers,
Stella

Dictating Coding Patterns - Bad Idea

purrin's picture

I really feel compelled to share my opinion with this whole Drupal css "standards" issue - trying to dictate to designers and Web Standards-based web developers how to code out their CSS in their style sheets is a fool's errand. There are as many different personal preferences amongst the absolute best Designers working today in xhtml and CSS as their are good designers. For us to try to make an "official" way of doing it is just ridiculous. We should be concentrating on keeping the code as lean and bloat-free as possible in core and making the system as open to as many different preferences as possible - totally agnostic as far as coding styles go.

For example, apart from Drupal, the broader CSS design community has a number of high profile design advocates and evangelists, including Andy Clarke who is on the W3C's CSS 3 working group, who argues very much in favor of making CSS more readable and usable by keeping layout styles apart from fonts, and those two apart from colors... all in separate style sheets. I fear Drupal making itself even less accessible to those who are new to it by dictating a certain way of writing CSS.. We can not draw the top CSS design talent to the Drupal community by telling them how they must learn to type their code.

A good lesson, I think, is to be learned from Wordpress - its unimaginable success is due in large part to the very open, flexible way in which is deals with styles.... it makes no assumptions and dictates no patterns... and it does not bloat you code. I think that should be our mantra: lean, agnostic, and bloat-free.

-=- christopher

PS: this wasn't meant to be directed at anyone in particular.. I just jumped in and added my $0.02 arbitrarily..

Missing the point slightly

alanburke's picture

http://groups.drupal.org/node/14421#comment-48105
attempts to address this before someone even posted it :-)

Coding standards are important when people have to work together,
where they have to read each others code,
and hope to help each other out with code or code review.

Where people work alone, and nobody else will even look at the code, ever,
coding standards don't matter a damm.

Maybe that applies to theme developers to some degree,
but if want your code to be part of the community, its better to have some standards so that people can work together.

If there were no PHP coding standards, core code would be a mess, a real mismatch of styles.

I'm going to go through all the comments soon, and finalise the standards.
Anything controversial will probably be left out, for now at least.

Thanks for the input though - any comments on the standards anyways?

Exactly

catch's picture

I think it's pretty safe to say that if coding standards were dropped from core, then a big fraction of the development community would splinter, because it'd be nearly impossible to work out what was going on most of the time.

Standard Editor

smoothdzion's picture

I agree with purrin. The more hoops people have to jump through to say "it's Drupal certified" the less jumping there will be. I think standards are good though when they make sense. I believe that there are common things we can do to make things organized better, but I don't think the spacing after styles, if elements/selectors are listed on a single line or not, are such a big issue. There is an exception for everything. I think the idea of common sense and organizing similar elements will create a very usable standard for most. I know with the tools I use I can easily find what I'm looking for without scrolling much at all.

I know that using Textmate it creates somewhat of a standard to how I code and it would take me more time to adjust things to look the way with 2 spaces to the left of every style then it would be worth. I think if you are going to create a standard then you better create a list of acceptable tools that can be used. It's the whole Internet Explorer and Firefox argument. If all tools were created the same and by default did the same exact things then we wouldn't be having this discussion because everyone would be using the same default formatting.

I use textmate to do everything and I'm sure other editors are used that don't do what Textmate can do. Maybe we should just start specifying what editors the stylesheets were created in so that people can understand what tools are being used. Then I would think that we would get a better understanding of what tools to standardize on if it ever came to that.

Brandon Buttars
Drupal Theme Development
http://www.odindev.com

Brandon Buttars
Drupal Theming
http://brandonbuttars.com

...

sun's picture

It's not about applications and your favorite tools. Please read Alan Burke's reply above; he explains the reasons why (coding) standards are important and healthy for a project like Drupal. No one cares which editor or whatever tool someone used to create or edit some bits of code, because almost all modern editors are able to apply a pre-defined coding style pattern to the source code.

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

One property & one selector on one line

rhache's picture

I agree with Nick here. Any rule that has one property and one selector should be on one line.

Selectors and lines

Zarabadoo's picture

I like putting my selectors on multiple lines. I can see why putting shorter length selectors on a single line would be convenient, I prefer to error on the side of consistency.

For me, it comes down to readability of the stylesheets. Drupal sites can be very complex beasts and the size of them can be quite large. Then you add the fact that in this community there will be several people looking at them and editing them, we will want to remove as much confusion as possible.

"While I see some

morbus iff's picture

"While I see some justification is being able to use both multiple selectors on one line, but use multiple lines if necessary, I'd rather be consistent." - than I'd vote for "multiple selectors on multiple lines". The dangers inherent in single-line usage is just too great.

While having two very short

brenda003's picture

While having two very short selectors on two lines can be annoying, if we're going for consistency I agree with Morbus here, otherwise there will be a lot of having have a VEEEERRY long line of incredibly long selectors. We all know those modules that give us way too many divs. ;)

+1 for: One selector per

owen barton's picture

+1 for:

  • One selector per line in all cases - less error prone
  • Standardizing on CSSDoc comment syntax, which is the closest thing to a standard that we have and it very friendly for folks who code almost any regular language

I am not sure about indentation of 'sub'-blocks as a whole as suggested by Zarabadoo (obviously the rules inside the braces should be indented 2 spaces). I can see how this could improve readability in some cases, but I would like to see a very very clear definition of when this indentation applies (i.e. one specific enough that we could write a coder rule for it).

Yeah I am not much of a fan

tjholowaychuk's picture

Yeah I am not much of a fan of the whole visual nesting thing either, this is usually quite obvious from reading the selector. Plus nearly all stylesheets I have ever seen are not THAT unwieldy if structured in a relatively common sense method. A stronger CSS documentation syntax was widely accepted that would be great, IDE usage would be interesting as well. If you saw my Vision Media style sheet example above even just simple stylesheet explorers within our IDEs would be quite handy.

My CSSEdit module may be something worth looking at in the future as well, as it will implement these features, and provide real-time editing for Drupal themers.


Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

I didn't used to be much of

Zarabadoo's picture

I didn't used to be much of a fan of it either until I started to use it. I normally use it within very defined areas that require the parent/child relationships. Proper and thorough commenting will be a much better focus before going too deep into this. I just wanted to give an example of things that have helped me on a whim that people would like it.

+1 for a single blank line

stella's picture

+1 for a single blank line between rules
+1 for blocks of rules being "separated" or distinguished by comments just above them

As for multiple selectors on one line, not quite sure, which I prefer, but it's hard for Coder to support both methods :) So I'm probably leaning towards one per line,.... I dunno.... I keep changing my mind.

Morbus: can you outline some of the inherent dangers you see with single-line usage?

-1 for Zarabadoo's proposal on indenting blocks.

In the doc, you have an "Other" section in which you say "Header Comment Blocks, block level comments, filenames should follow the PHP coding standards." I would prefer if you actually specify what those are in the CSS standards doc. It would prevent users having to flip between docs. This is what I'm doing for the JS standards doc after receiving feedback from pwolanin and others today.

It might be an idea to specifically state what the indentation / spacing standard is, i.e. 2 spaces. I would like there to be a standard for one space after the colon. I think it is implied in the examples above, but I'm not sure you stated it.

Cheers,
Stella

Alphabetize, hex codes, and hyphens

stephthegeek's picture

+1 for a single blank line between rules
+1 for blocks of rules being "separated" or distinguished by comments just above them

-1 for indenting (too hard to manage, better to use comments for related chunks)
-1 for multiple selectors per line -- I have been burned by this so many times, not seeing that there was another selector on the line as opposed to just a really long one

New standards proposals:
- Alphabetizing properties. It sounds kinda hokey at first, but after seeing it pop up in a couple of "best practices" lists, we started doing this a year or so ago and it's awesome.
- Color hex codes instead of names (#ffffff instead of white) -- for better scannability and accuracy
- Always using hyphens in class names, no underscores

~~~
{ Drupal Themes by TopNotchThemes } Gorgeous Drupal 5 & 6 designs with Views/Panels 2 support, plus Ubercart themes!

+1 to alphabetizing. This is

Zarabadoo's picture

+1 to alphabetizing. This is another thing I use that is quite handy. Plus it is much easier to standardize on.
+1 to hex codes.

I am not sure with the hyphen thing. This will require cooperation with module developers also. I do prefer hyphens myself though.

+1 to hex (and full hex at

brenda003's picture

+1 to hex (and full hex at that)
+1 to hyphens

Could you explain alphabetizing properties?

+1 to hyphens on class/id

rhache's picture

+1 to hyphens on class/id

RGB

elv's picture

What about RGB codes vs. hex?

+1 alphabetize properties

rhache's picture

One huge advantage is that your properties are in the same or order as Firebug.

-1 for indenting. baaaaaaaaaaaad idea. If CSS had script-like logic, I would agree.
+1 multiple selector each on one line.

+1 for alphabetizing

morbus iff's picture

+1 for alphabetizing properties. I've been doing that already on hook_menu and Form API arrays and, yep, definitely agree - it increases readability, scannability, and eases expectations. +1 for color hex codes, but then you go and say "#ffffff" instead of "#fff" (ideally, though, this is a "soft +1" from me - if you want the most accuracy, you'd use rgb(#, #, #), and I don't consider "#ff0" more "scannable" than say, "blue" or "red"; I only agree cos I practice "hex codes only" too). +1 for hyphens too. Another one I use everyday, and consistent with Drupal core's existing practices (i.e., there's no reason to argue against it, cos core's already doing it ;)).

-1 I [valley girl

Nick Lewis's picture

-1

I [valley girl voice]totally[/voice] disagree with alphabetizing. The importance of the rules needs to dictate where they go in the CSS document.

html > body > #content > #main-content > .node > .author

Don't hide body under .author because "A" becomes before "B"! CSS doesn't follow the alphabet. HTML "H" always comes before BODY "B".

++++++++++++++++++++++++++++++++
"I'm not concerned about all hell breaking loose, but that a PART of hell will break loose... it'll be much harder to detect." - George Carlin
++++++++++++++++++++++++++++++++
work: http://www.onnetworks.com
blog: http://www.nicklewis.org


"We are all worms. But I believe that I am a glow-worm." - Winston Churchill
work: http://www.chapterthree.com
blog: http://www.nicklewis.org

Not to mention, the result

Nick Lewis's picture

Not to mention, the result might not be as good as we expect...

I think garlands style.css is worse alphabetized... attached is how it looks ( via this tool.... which does the java-style css thing by default... http://www.lonniebest.com/FormatCSS/ )

(Okay, so the thing spits out f#cked up CSS.... that isn't alphabetized, but instead follows #div > .class > element.... but really, this is a rule that makes people want to use the horrid tools... I don't want to encourage people to put code through these auto generators so it meets our standards....)

++++++++++++++++++++++++++++++++
"I'm not concerned about all hell breaking loose, but that a PART of hell will break loose... it'll be much harder to detect." - George Carlin
++++++++++++++++++++++++++++++++
work: http://www.onnetworks.com
blog: http://www.nicklewis.org


"We are all worms. But I believe that I am a glow-worm." - Winston Churchill
work: http://www.chapterthree.com
blog: http://www.nicklewis.org

I believe what was intended

Zarabadoo's picture

I believe what was intended by alphabetizing is the individual style attributes on each set.

body {
  font-weight: normal;
  background: #000;
  font-family: helvetica, sans-serif;
  color: #fff;
}

vs

body {
  background: #000;
  color: #fff;
  font-family: helvetica, sans-serif;
  font-weight: normal;
}

Yep, that's what I was

stephthegeek's picture

Yep, that's what I was referring to.

~~~
{ Drupal Themes from TopNotchThemes } Gorgeous Drupal 5 & 6 designs with Views/Panels 2 support, plus Ubercart themes!

Ah! :-D Who could argue

Nick Lewis's picture

Ah! :-D

Who could argue against that?
++++++++++++++++++++++++++++++++
"I'm not concerned about all hell breaking loose, but that a PART of hell will break loose... it'll be much harder to detect." - George Carlin
++++++++++++++++++++++++++++++++
work: http://www.onnetworks.com
blog: http://www.nicklewis.org


"We are all worms. But I believe that I am a glow-worm." - Winston Churchill
work: http://www.chapterthree.com
blog: http://www.nicklewis.org

Awesome to hear this is being added to Coder (I heart Coder)

jacine's picture

Here's my 2 cents :)

+ 1 on hex
+ 1 on single line between rules
+ 1 for blocks of rules being "separated" or distinguished by comments just above them
+ 1 for Always using hyphens in class names, no underscores
+ 1 for alphabetizing properties. Does sound a little hokey, but I'll go with it :)
+ 1 for each link ending in a semicolon

- 1 on indentation
- 1 on multiple selectors on 1 line, for consistency sake, while it will surely be annoying in certain cases

+ 2 more...

1 - always use double quotes in HTML attributes, ie. <div class="class-name"> as opposed to <div class='class-name'> (though this is more regular coding standards, I had to say it)

2 - 1 space between property: & value, border: none; as opposed to border:none;

What about comments? What do you guys do? I've tried a few different ways in the past, but I find this works the best for me:

/*------------------------------------------------------------------------
 * MODULE SUPPORT
 *-----------------------------------------------------------------------*/

/* administration menu */
#admin-menu {
  font-size: .95em;
}

/* update status */
.versions table,
.versions td,
.versions th {
  border: none;
  overflow: auto;
}

I love comments delineating

Nick Lewis's picture
  1. I love comments delineating sections! I do this obsessively.
  2. Space between property and value seems to be a matter of taste. I personally always make a point of having no space... I see no compelling arguments either for or against the rule, so I think it should be left alone.
    ++++++++++++++++++++++++++++++++
    "I'm not concerned about all hell breaking loose, but that a PART of hell will break loose... it'll be much harder to detect." - George Carlin
    ++++++++++++++++++++++++++++++++
    work: http://www.onnetworks.com
    blog: http://www.nicklewis.org

"We are all worms. But I believe that I am a glow-worm." - Winston Churchill
work: http://www.chapterthree.com
blog: http://www.nicklewis.org

Comments should always use

sun's picture

Comments should always use CSSDoc syntax, just like we're using PHPDoc syntax for PHP. See http://cssdoc.net/wiki/DocBlock for an example. Personally, I hate those custom

/*** --- #### ##### Styles that apply to drunken output from here #### --- **/

that do not follow any reasonable syntax at all. Even more horrible:
/---------------- DO NOT CHANGE ANYTHING BELOW!!! ---------------*/

We all know them...

Aside from that, +1 for adding "Use double-quotes for HTML tag attributes." to our PHP coding standards.

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

Very true. I find columns

tjholowaychuk's picture

Very true. I find columns are more readable, but like you mentioned there really is no point to start a new syntax in that regards. When the time comes along that CSSDoc is a proper specification and IDEs embrace it then it wont matter if it is readable in plain text


Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

DocBlock

jacine's picture

It's great for PHP, CSS not so much... Section and Single Line Comments back to back are a real eye sore for me. If you could do single line comments, like you can in PHP, //, it wouldn't bother me. Just my opinion.

Inline comments

sun's picture

Of course nothing should prevent you from using

/* Short inline comments. */

in addition to
/**
* Long CSSDoc block comments.
*
* Including additional information.
*
* @see andPointers
*/

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

My point was that this is more geared toward PHP

jacine's picture

I hate this.... It looks like a jumbled up mess IMO.

/**
* Section
*/

/* Subsection */

Though when using it in PHP it doesn't bother me, but we are talking about CSS here so we can't use // (I'm well aware of this)

/**
* Section
*/

// Subsection

Anyway, whatever you guys decide is cool with me... I just wanted to express my hate for it publicly. I can still and will code my own way on non-contrib projects :)

I'm pretty sure // comments

stella's picture

I'm pretty sure // comments aren't valid in CSS, and so you would have to use the /* .... */ method as described by sun.

Scroll-friendly comments delineating

donquixote's picture

Yes, I like that!
Makes scrolling much easier.

Usually I use

/* ======================= SECTION ========= /

/
-------------------------- SUBSECTION ---------- */

I don't use ++++, that kills the "horizontal" look.

Definitely agree on your two

brenda003's picture

Definitely agree on your two later points. As for commenting, I use the more PHP way personally but I'm usually more of a developer these days. Those big comment lines always bugged me.

/**
* Section comment here
/

/
Minor comment here  */

Why standards?

alanburke's picture

Good to see that consensus of some kind is emerging from the healthy discussion.
I've learned so many things, CSSdoc syntax, alphabetizing comments, indentation, etc.

It nice to step back briefly and ponder the reason for standards.
If the CSS works, then why should we care?

As I see it, coding standards are to allow developers to work closer together.
Having a common langauge means we can all contribute to the same project,
without the code becoming an ugly variant of styles.

I believe it's important that all code added to drupal.org, both core and contrib should follow the standards.
I realise that some people will heartily disagree with some of the standards, and try to rail against it in their own projects.

Please think again.

Every project can benefit from other people submitting code reviews, both manual and automatic, and from patches.
Where your code is outside the standards, you present a barrier, however small, to such co-operation.

As well as that, it may be that Coder module ratings could be used as a metric to rate projects [I certainly hope it will],
and your project would suffer.

Onwards with the discussion.

Alan

CSSDoc is not really much of

tjholowaychuk's picture

CSSDoc is not really much of a standard yet, I wish though :) eventually !


Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

Updates.

alanburke's picture

I have made the following updates.

Selectors are now one to a line.
Alphabetizing rule added.
Added rule specifying full hex values for colors.

I have addded a note on writing valid css, which needs review.
I have added a note on CSS comments, which needs review.

Indendation looks unlikey to get added, unless someone can come up with a very clear rule to define its use.

Some more questions.
Uppercase or lowercase?
I certainly prefer uppercase for hex values, but lowercase elsewhere.

Where should our HTML standards go?
Who wants to take that one on?
This is good start
http://groups.drupal.org/node/6355
but it needs to migrate to the handbook, and become a standard.

Alan

Lowercase

Zarabadoo's picture

I prefer lowercase all around. A lot of that is because I don't like pressing the shift key if I don't have to and i really don't see having all caps on hex values of a lot of benefit since they are pretty identifiable on their own.

Lowercase yep. Still

morbus iff's picture

Lowercase yep. Still wondering why you're enforcing full hex colors (I note that you "responded" to it by invalidating my criticism, but not actually defining /WHY/ one should repeat #ffffff when #fff is just fine).

Hi Morbus, I'm enforcing

alanburke's picture

Hi Morbus,
I'm enforcing nothing untill Stella gets her hands on whatever standard is final:-)

Again, I feel this just comes down to consistency.
Using the full 6 characters just seems consistent with other hex codes defined.

PS.
As someone who's entire code contribution conists of a Hex code [seriously],
I'm just trying to lead the way is agreeing a standard.

The documentation page is very much in flux right now.

Alan: I (now) agree that

morbus iff's picture

Alan:

  • I (now) agree that hex codes should be capitalized.
  • But, for the same reason, I again promote the 3-letter variants.

What changed one opinion but reinforced the other? The example in the CSS 2.1 standard, which uses "F00" (capital F, three-letter variant of FF0000). [EDIT: Actually, scratch that. They're not consistent themselves, as seen here.]

Ignore me, I think. I'll +1

morbus iff's picture

Ignore me, I think. I'll +1 capitalizing hex codes and +1 six-character only. Why the change of heart? Every graphic application I can find on my box always does capitals and six-characters, and I've just been changing them to my "house style" (3 and lowercase). So, I'll save myself an extra step in the future ;)

Keep your style, please. :)

sun's picture

Like Zarabadoo, I don't like to press SHIFT so often. Also I find #E2A1C0 less readable than #e2a10, erm, sorry, misread that, #e2a1c0. That's because lowercase letters can be better distinguished from digits visually.
However, this is getting too nit-picky IMHO.

Re Hex: The purpose of coding standards is definitely not to limit features. #eee is a CSS feature, and I do not see a reason why we should not use it.

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

The more I think about it,

Zarabadoo's picture

The more I think about it, the less I like forcing hex. I don't really use the word colors unless I want a quick color so i can see the position of a block, but there is not real goo reason to eliminate them. RGB and HSL colors are becoming more useful also. Especially with Firefox3 adding support for RGBA and HSLA, I can see them being used much more as other browsers catch up.

So I recommend not restricting color declarations.

Must be something in the air..

Mostly agree

laura s's picture
  • 1 on hex ... but I strongly believe short hex should be acceptable
  • 1 for blocks of rules being "separated" or distinguished by comments just above them
  • 1 for Always using hyphens in class names, no underscores
  • 1 for alphabetizing properties. Does sound a little hokey, but I'll go with it :)
  • 1 for each link ending in a semicolon
    +1 for always use double quotes in HTML attributes
    +1 for space between property: & value, border: none; as opposed to border:none;

0/0 (meh) on single line between rules. Okay, if that's a strong preference. It goes against the whole concept of chunking data for readability, but I can cope.

-1 on indentation (sorry Al -- works in xhtml, but confusing in css)
-1 on multiple selectors on 1 line, for consistency sake, while it will surely be annoying in certain cases
-1 on HEX CAPS. I say play it as it lays. If you're pasting from Colorzilla, it will have caps. If you're typing, it might not. I don't see a gain from imposing a rule on this

I would also add a fuzzier rule: Avoid adding specific IDs/classes from the detail end up. For example, a page ID and class can save a lot in specific IDs and classes on individual fields, lists, etc. Basic example:

<div id="happy" class="mood">
<p>When I am happy....</p>
<ul>
<li>I smile.</li>
<li>I have more energy.</li>
</ul>
</div>

is cleaner and preferable to:

<div class="container">
<p class="mood" id="happy">When I am happy....</p>
<ul>
<li class="mood">I smile.</li>
<li class="mood">I have more energy.</li>
</ul>
</div>

There's nothing like having to create a 5,000-line css to accommodate all the individual items that have been given overly detailed mark-up, especially when a simple, identified div container class and/or id are missing.

/rant


Laura
pingVision, LLC (we're hiring)

Laura Scott
PINGV | Strategy • Design • Drupal Development

Totally agree with Laura's

ckng's picture

Totally agree with Laura's points.
Colorzilla you can set it return lowercase hex (right click for option)

CK Ng | myFineJob.com


consultation • web design & development • content development • site domain, hosting & maintenance • software design & development

CK Ng | myFineJob.com

CSS

alanburke's picture

Hi Laura,
The document is only concerned with the CSS coding standards,
but of course the HTML standards are closely linked.

The HTML markup and style guide mentioned above should evolve into some sort of a standard too,
but I'm going to wait till we're all done here before I tackle that.

Alan

As far as ordering of the

tjholowaychuk's picture

As far as ordering of the rules I would somewhat agree with alphabetizing it, however I prefer to start with rules that are unlikely to change, then following with those that will. Also I prefer to utilize the box model as my ordering, so display, position, dimensions etc. example:

display: block;
position: absolute;
top: 0;
left: 0
margin: 0 0 0 0;
padding: 0 0 0 0;
width: 100px;
height: 100px;
font-size: 12px;
color: #fff;

Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

makes me think

mike stewart's picture

I thought alphabetizing was a good idea when I first read it. but after seeing @Tj Holowaychuk comment, i forgot how important grouping can be. I'm still undecided on this one.

--
mike stewart { twitter: @MediaDoneRight | IRC nick: mike stewart }

Does the box model offer any

stevebayerin's picture

Does the box model offer any performance (browser rendering) advantage?

I barely notice when properties are alphabetized and when they aren't.

Good question. I am sure

tjholowaychuk's picture

Good question. I am sure implementations very but I would imagine the stylesheets are compiled before any rendering occurs so I would doubt this as any impact.


Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

Alphabetize properties is a huge -1

johnalbin's picture

I always use the box model method of ordering properties because it makes it easier to understand what the rule is actually doing. Many properties are interdependent and putting them next to each other makes that clear.

For example, let's put TJ's example in alphabetical order:

color: #fff;
display: block;
font-size: 12px;
height: 100px;
left: 0;
margin: 0;
padding: 0;
position: absolute;
top: 0;
width: 100px;

To me that is much harder to read. And if, for example, you start modifying the position, you are likely to forget to remove the top and left (or right and bottom which may or may not be in there too.) So alphabetical order makes it more likely you'll screw up the CSS when modifying it.

  - John (JohnAlbin)

  - John (JohnAlbin)

box model

sun's picture

I'm using this order, too. However, there is one caveat: All other properties that are not box-model-related can be added in arbitrary order. Also, there are certain properties like z-index that usually belong to another property, such as position: absolute, but do not belong to the box model. Additionally, rather important properties like display: block, display: inline, or overflow: hidden are not really tied to the box model - but should be noted at the beginning, because they have an great impact on the output. So, in the end we would have a rather complex standard on CSS property ordering - whereas an alphabetical order would be very simple, clean and easy to understand by everyone.

Please don't get me wrong, I'm also used to the "order by importance" as mentioned before, but I do not think we can get to a consensus about importance of CSS properties - besides the fact that the required work for defining and agreeing on the importance wouldn't be justified for CSS coding standards at all.

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

I agree, its a tough call.

tjholowaychuk's picture

I agree, its a tough call. Personally I find the 'boxmodel-ish' approach to be generally more logical as it is simply the order of which I would think when styling an element. First I need a block, then margins, padding, dimensions etc, I rarely would choose a font family before I specify the box. But again like you mentioned specific properties are not necessarily related


Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

Block vs. inline?

Crell's picture

What about just grouping that way? "Put all block-level rules first (margin, padding, position, etc.), followed by inline rules (color, font, letter spacing, etc.)" Would something at that level be a useful standard?

I think it makes pretty good

tjholowaychuk's picture

I think it makes pretty good sense, personally I find when we start with block level declarations and then go on with fonts, backgrounds etc as long as they are grouped theres nothing really tough about reading it regardless of how its laid out. I just really hate when its fonts or colors first, I think about dimensions before any of that, and it sucks when its font-family, background, font-size, color, font-weight, all scrambled around.


Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

No grouping of properties

rhache's picture

-1 on that

Properties should be by alphabetized only (otherwise nothing). To start, it's not true that padding and margins are block level properties only. They apply to inline elements as well (but not for top/bottom). Second, very few properties are block or inline "only" really, and very few CSS coders really know the distinction.

completely disagree

rhache's picture

Not hard to read for me.

Initially I was very resistant to doing it alphabetically, but after forcing myself to try it for a short while it made more sense -- plus your code is in the same order as Firebug displays it. That to me is a massive advantage. Plus, I started seeing the code has a whole.

Second, alphabetical becomes automatic and you don't even have to think where a property goes. That can't be said of grouping properties, as I could come up with many scenarios where the grouping isn't obvious.

Lastly, Tj's example is a worst case scenario. Not completely uncommon, but exactly an every rule scenario.

http://cleancss.com/

smoothdzion's picture

What if we everyone got into the common practice of using a site like http://cleancss.com/ to clean up all their css once it was all together. Would this make some people sleep better?

Brandon Buttars
Drupal Theme Development
http://www.odindev.com

Brandon Buttars
Drupal Theming
http://brandonbuttars.com

CSSdoc is an alpha-level spec

johnalbin's picture

The CSSDoc spec needs a lot of work. And I don't think we should be adopting it prematurely.

I was asked to review it by the authors a few months back, but it was quickly apparent it wasn't worth my time. I have 2 major objections to it. One is there are no tools that actually build useful CSSDoc documentation. They list PHPDocumenter, etc, but those won't generate useful CSS documentation for the following reasons because they don't recognize the @rules that CSSdoc has created that differ from PHPdoc.

In other words, the proposed section comments (which don't even use the proper CSSdoc @section syntax) won't be recognized by the automated tools, or worse would apply the comment only to the first ruleset in the section.

And the proposal doesn't follow the full CSSdoc spec. The full CSSdoc spec would require full PHPdoc-block-like syntax for every comment you want to make. i.e. you would need one per css ruleset or per property. That's super code bloat. Inline comments are much better and they are way more compact.

The idea of @css-for, @bugfix, and @section are good, but requiring a full docblock for the @css-for and @bugfix is flawed given the amount of one-off property fixes needed for various browsers. (That's my second objection to CSSdoc.)

If we're not going to follow the full CSSdoc (and I don't think we should), there's no reason to do a half-way CSSdoc standard. That would lead to confusion (follow this part of the spec, but not that.) Now, an @file docblock does make sense, but that is identical to PHPDoc.

So what are we gaining by saying we are using CSSdoc syntax? Confusion over what parts apply to Drupal's standards and a shifting, alpha-level specification. :-\

  - John (JohnAlbin)

  - John (JohnAlbin)

Great Feedback.

alanburke's picture

Hi John,

Good to hear that input.
One of the reasons I proposed a standard is that the Zen styles vary from core a bit, and for some reason it kind of bugged me :-)

Given that the spec for CSSdoc is in flux, then perhaps we should avoid it for now?
What CSS documentation standard would you suggest?

As regards something to process the documentation, Neil Drumm is hoping to rewrite the code for api.drupal.org, and were appropriate coding standards in place, he could make use of that.

Regards
Alan

[I'm a bit tied up at the moment and am struggling to finish off the standards doc in any case]

We need something. Even

tjholowaychuk's picture

We need something. Even simply outlining the document at the top of the file like an index, and then separating logical sections (forms, comments, tables, etc) is highly needed, and lower tolerance for one-liners. Also like you mentioned no IDEs even recognize or implement any form of CSSDoc syntax that I am aware of.


Tj Holowaychuk

Vision Media - Victoria BC Web Design
Victoria British Columbia Web Design School

Overall I like the standards

rhache's picture

Overall I like the standards and I'm sorry to not have participated in the discussion sooner. I have two major annoyances:

1- Not being able to leave CSS rules with one selector AND one property on one line
2- Having a blank line between between each and every rule

I could eventually learn to live with #2, but with much chagrin. #1, not giving it up.

Hopefully this will be revisited soon.

What is being achieved by all of this?

xtfer's picture

I think its highly unlikely that I will suddenly change my CSS coding practices because of the Drupal coding standards. In contrast, I am more than happy to meet the PHP coding standards, as they actually make the PHP usable.

I always place CSS rules on a single line; ordered alphabetically; in ID, Class, then Element order. This helps me find items quickly, keeps the length short (less scrolling) and knocks out inheritance issues.

But aside from all that, formatting CSS is not important for readability in the same way it is in scripting languages. There are a lot of disagreements in this thread about what is readable and what isn't, which illustrates an important point with CSS: different strokes for different folks.

This problem stems from the fact that CSS can be organised in lots and lots of different ways, whereas a scripting language, for the most part, can not. Its procedural elements MUST be in order. Because of Specificity, CSS can theoretically be in ANY order.

Perhaps we should be focussing on usage standards, not formatting... things like

  • lowercase selectors
  • hex standards
  • use of comments
  • splitting layout & design

I agree with this. I HAVE

theresaanna's picture

I agree with this.

I HAVE altered my methods in working on professional and contrib themes, but to me readable is single line declarations, attributes in order of importance (which is totally subjective to the coder).

I can definitely get on board with some guidelines, but as xtfer said, much of what we are discussing here is preference. What is readable to me, what makes sense to me might not be ideal for you. This is not to say that I don't think the community shouldn't adopt guidelines and specifications as they are completed and accepted by the developer community as a whole. I just don't personally feel that it will be particularly advantageous for the Drupal community to iron out each detail.

Has a situation arisen that has led us to seek a standard as a solution? My view is, at this stage, the priority is getting more high quality, standards compliant themes into Drupal. If this would fix an ongoing issue that is a different matter.

Topic is not custom code

sun's picture

The topic and focus is not custom code. We are talking about coding standards for Drupal core and thereby also contributed modules. Setting up a CSS coding standard will make the code readable for all, and at the same time, answer all of the current questions that arise for contributors these days:

In which way should I write CSS? Just follow what's there in the same file or should I follow CSSDoc standards? And why are selectors written differently over here? ... but user XYZ told me, I should write it this way!!

Not having clear instructions on how to write CSS code hinders development.

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

Don't give me none of that SASS

derekwebb1's picture

Actually SASS looks pretty cool: http://sass-lang.com/ ... Anyone used it? I am thinking of giving it a go sometime. How would SASS fit in with this discussion? SASS is an alternative method for coding CSS so that you can have neat things like variables and (actual) nesting of selectors.

I am thinking that SASS would really fly in the face of CSSDoc though (from what I can tell). I suppose it really depends on what the SASS interpreter's output looks like though. As I recall it looks fairly different from vanilla CSS in terms of indentation, spacing, etc... It may alphabetize. But a tool like this could change the way we code CSS as well.

Derek

Is this initiative still

jix_'s picture

Is this initiative still alive? The handbook page still says it's "only a proposal", but most comments here were posted in 2008/2009.

My 2.5 cents

shderuiter's picture

+1 for multiple selectors being on a single line each, with the exception of top level elements (h1, p, etc.). I usually put them together on a single line and if that line were to grow too big you're doing something wrong anyway. ;-)

+1 properties being on a single line.

-1 indentation of properties with spaces. I hate spaces for indentation and I have yet to hear a convincing argument to using spaces in favor of tabs. Use tabs and set your editor to your own preferred tab width. I know 2 spaces is the de facto standard in Drupal php files but we hates it and we wants our tabs, yes, precious...

+1 for a space after property name.

+1 for the semi-colon.

-1 for alphabetizing properties. I prefer to logically group my properties (pretty arbitrary, but it's a habit I picked up when I first learned css back in the olden days):

  • width/height
  • margin/padding
  • background/foreground colors (including images)
  • font/text related
  • positioning
  • etc.

-1 on using uppercase with hex colors; I agree with sun on that one.

I haven't decided what to think about leaving out the empty lines between styles; they do make grouping with short comments easier but it looks like a bit of a jumble to me with larger stylesheets.

Hey, bumping this as well.

yoroy's picture

Hey, bumping this as well. Would like to be able to point contributors to this spec without the disclaimer on top.

Reading this through I see some changes to do:

  • Change to lowercase hex colors. It's easier to parse visually.
  • Don't alphabetize properties. Logical grouping encouraged, suggested ordering:
  1. floats, displays, and other high-impact properties
  2. block level
  3. inline

(Alphabetical sort equals random sort here. Promote a 'first things first' approach.)

I'm personally -1 on a blank line between every rule, it takes away a handy tool for grouping stuff. Discuss :-)

Progress

sun's picture

The order of properties was already discussed lengthy, perhaps somewhere else. Ordering properties alphabetically is the only standard, everyone, including non-experts in CSS, can follow. Additionally, a logical/boundary/box grouping and ordering did not reach consensus and due to the variety of properties, there most probably cannot be a consensus. When defining standards, we need to make them work for everyone, at any skill level, and everywhere.
Effectively, I've changed my mind regarding the order of properties, and support the alphabetical ordering for its simplicity.

Most concrete changes for coding standards for CSS are being discussed in http://drupal.org/project/issues/search/drupal?issue_tags=coding+standards

The proposal to use lowercase hex colors does not seem to be tackled though.

Daniel F. Kudwien
netzstrategen

Thoughts....

chroid's picture

Im putting together some internal CSS guidelines for the developers here at our studio and have always duelled with CSS standards and best practices.

With some pretty large Drupal developments under our belts, it's become increasingly difficult to work on varying styles of code layouts that have passed through a number of devs.

Of particular note is the length of some of our CSS files. We've made it common practice to instantly split away from any major additions to the themes default stylesheet (due to it's size), and create a new stylesheet for our custom layouts. This brings the scroll length down significantly but is not to say that the new one won't get just as big.

I guess the problem here is that everyone has their set ways of doing things which can create a bit of bias.

In an effort to reduce length and IMO improve (from a developers point of view) the readability and ease of property vs layout recognition I've opted for single line rules with good commenting.

/* Some section specific layout */
.foo-bar-baz .foobar .beer { float:left;width:100px;height:200px;margin:0 0 2em 0;padding:0; }
.foo-bar-baz .foobar .beer div { overflow:hidden;float:left; }
.foo-bar-baz .foobar .beer ul { display:block;float:left;position:absolute:overflow: hidden; }
.foo-bar-baz .foobar .beer ul li { float:left;width:100px;height:200px;margin:0 0 2em 0;padding:0;font-size:11px;color:#deface; }
/* end */

It creates grouped layout based blocks within the CSS. Agreed, that some rules have the potential to be loooong, but I offset that by simply breaking to a new line:

/* Some section specific layout */
.foo-bar-baz .foobar .beer ul li {
  float:left;width:100px;height:200px;margin:0 0 2em 0;padding:0;
  font-size:11px;color:#deface;background:#fff;
}
/* end */

You may note I also try to push all the layout based properties first. Most times when coming into a CSS file for the first time the more difficult part of edits are around layouts. Colouring for example is a piece of cake. So,

float:left;width:100px;height:200px;margin:0 0 2em 0;padding:0;

Is the general rule I'll follow, floated, width then height (just like how WxH works with image dimensions) then margining and padding. I get the want for having things alphabetically, but it gives a pretty heavy separation between, for example, width and height, which are, in terms of layout, closely related.

Just interested to see what people think here.

C

one line declarations

jessebeach's picture

I used to prefer one line declarations, but there were times when a declaration would wrap and I didn't like multiple indenting to get the second line over to left-align with the first.

Also, viewing a one-line declaration style CSS sheet in source is terrible if any of the lines are wrapped and indented. What do you do with long lines?

For properties, my preference is to stick with the Drupal standard alphabetized list. Take your example, for example. To me, the properties are declared according to the box model...almost. I would have written it like this.

float:left;width:100px;height:200px;padding:0;margin:0 0 2em 0;

Even within my own documents, I've found myself varying the order of properties within a declaration before I adopted the Drupal standard. Sure, the standard ignores the semantics of the properties, but it's simple and void of interpretation.

One liners

chroid's picture

Hi Jesse,
Thanks for the reply.

Also, viewing a one-line declaration style CSS sheet in source is terrible if any of the lines are wrapped and indented. What do you do with long lines?

The source view (from FF at least) renders the indentation and breaks just as they would appear in the editor. But I'd question the importance of code layout when it comes to source view ? Or by source do you mean the code layout in the editor ? As opposed to 'view source' from a browser.

To me, the properties are declared according to the box model...almost

Correct, in my example provided they follow the box model, however, margin does come first in that scenario (http://www.w3.org/TR/CSS2/box.html)

The ordering ones a hard one, I can see the merits in a standard for alphabeticised properties, but it just seems (from a developers point of view) difficult to instantly recognise layout/structure properties and values.

...

sun's picture

The "source view", regardless of debugging extension and regardless of web browser, doesn't count at all for Drupal's coding standards. Coding standards are shaping a common denominator that can be followed by anyone, without any preprocessor or postprocessor (Firebug and any other code prettifier falls into one of these categories).

For the same reason, we're also not basing the order on the box model or any other CSS internals - regular PHP developers, who may not be experts in CSS, can still write styles in a way that comply with Drupal's coding standards.

Since Drupal's development and evolution is driven by patches, it's highly preferred to have separate styles also on separate lines. A large amount of rules can be grouped via CSSDoc statements instead, but can also be put into different files.

Daniel F. Kudwien
netzstrategen

driven by patches

As If's picture

sun>> Since Drupal's development and evolution is driven by patches,
sun>> it's highly preferred to have separate styles also on separate lines

End of discussion on that fine point, right there. Highly anal, yes, but it makes perfect sense for anything publicly patchable.

Game, set, match. Patches

james.elliott's picture

Game, set, match. Patches that only change 1 attribute, should only change 1 attribute. Having to parse through a long string to identify the attribute that has changed is counter productive.

I am bombed with emails from

donquixote's picture

I am bombed with emails from this thread subscription, so now I can't resist to add my own thoughts.

Forces:

  • patches and diff are more pleasant if every rule is in a separate line.
  • moving individual rules from one block into another. Again, this is easier with every rule on a separate line. And, it's vital to have all of them in the same indentation, so you don't have to fix indentation after moving a rule.
  • scrolling / long-range visual scan: it is important to quickly find a specific section within a long list of style definitions. This is easier if the content has a structured visual signature, and not every definition looks the same. It is vital to have some visual artifacts that stick out while scrolling. The thing that does this best is a horizontal line comment (---- or ===). Between those, we can play with different numbers of blank lines, to group some definitions together and separate others.
  • slow / short-range visual scan: same as above, but slower, because now you are looking for individual rules. It helps a lot if every rule is in a separate line, with a homogeneous indentation, because now your scan can follow one vertical line, instead of having to jump around left and right.
  • Sometimes we want to compare "parallel" style definitions, such as with ltr / rtl, or different headline tags. This can be a lot easier with all-rules-in-one-row styles, if it allows to align subsequent style definitions like a table, but obviously there is a tradeoff with the other forces.
  • We want as many rules as possible in the viewable monitor area, at any given moment. This means, we don't want to have more empty lines than necesary. Again, there is a tradeoff, and this force is not the most important one.

So, how do my stylesheets look like?
(more or less)

/
* The horizontal line is a visual anchor for easier scrolling.
* Much better than just a comment.
*/

/
------------------------------------------------- section A ---------- /

/

* "Parallel" ruled can be aligned like table cells,
* which helps to compare.
* This should really be the exception in an average style sheet!
/
h1 {color:green; font-size:20;}
h2 {color:red;    font-size:15;}
h3 {color:black; font-size:12;}

/

* Similar for ltr / rtl stuff.
/
.someclass {
  /
what is common for ltr and rtl? /
  margin:4px 20px 6px;
}
/
how are ltr and rtl different? /
.ltr .someclass {float:left; margin-right:120px;}
.rtl .someclass {float:right; margin-left:120px;}


/
----------------------------------------- section B --------------- /

/

* In general I prefer one line each rule.
*/
.another-class {
  padding:5px 0;
  white-space:nowrap;
}

The horizontal lines really help a lot, I can absolutely recommend it.
The "parallel" idea is ok and useful for custom css, but maybe it does not really fit in a "coding standard". Or does it?

...

sun's picture

At the very least, proper CSS comments are required to make this example snippet remotely work ;) Perhaps one of g.d.o's filters killed some stars?

Daniel F. Kudwien
netzstrategen

Weird shit. Seems we found a

donquixote's picture

Weird shit. Seems we found a bug in the filters.
Try: "slash star space star slash space slash star space star slash", and this is what you get:

/* / / */
(with "code" tag)

/* / / */
(without the "code" tag)

/* / / /
/ / / */
(code followed by non-code. seems the bug goes beyond the tag)

EDIT:
Ok, there is also at least one typo in my css snippet above.

EDIT II:
Must be the markdown syntax! I totally missed that.

/* */ /* */ no markdown this

donquixote's picture

/* */ /* */
no markdown this time.

Alphabetizing properties is a bad idea

bleen's picture

...and I dont think it has been settled on at all. Take this example from Bartik (note: I added shadow to make my point):

#navigation ul.links li a {
  color: #333;
  background: #ccc;
  background: rgba(255, 255, 255, 0.7);
  text-shadow: 0 1px #eee;
  -khtml-border-radius-topleft: 8px;
  -moz-border-radius-topleft: 8px;
  -webkit-border-top-left-radius: 8px;
  border-top-left-radius: 8px;
  -khtml-border-radius-topright: 8px;
  -moz-border-radius-topright: 8px;
  -webkit-border-top-right-radius: 8px;
  border-top-right-radius: 8px;
  -moz-box-shadow: 0 0 15px rgba(50, 0, 0, 0.5);
  -webkit-box-shadow: 0 0 15px rgba(50, 0, 0, 0.5);
  -khtml-box-shadow: 0 0 15px rgba(50, 0, 0, 0.5);
  box-shadow: 0 0 15px rgba(50, 0, 0, 0.5);
}

If this were alphabetized it would be:

#navigation ul.links li a {
  -khtml-border-radius-topleft: 8px;
  -khtml-border-radius-topright: 8px;
  -khtml-box-shadow: 0 0 15px rgba(50, 0, 0, 0.5);
  -moz-border-radius-topleft: 8px;
  -moz-border-radius-topright: 8px;
  -moz-box-shadow: 0 0 15px rgba(50, 0, 0, 0.5);
  -webkit-border-top-left-radius: 8px;
  -webkit-border-top-right-radius: 8px;
  -webkit-box-shadow: 0 0 15px rgba(50, 0, 0, 0.5); 
  background: #ccc;
  background: rgba(255, 255, 255, 0.7);
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
  box-shadow: 0 0 15px rgba(50, 0, 0, 0.5);
  color: #333;
  text-shadow: 0 1px #eee;
}

It's terrible that the box shadows and border-radii are not all together. Look how easy it would be to forget to change "box shadow".

I agree that alphabetizing is the only easily enforced, newbie-friendly standard here but that does not mean we should adopt it. It would be infuriating to me to submit a patch and have it be set to needs work because the properties weren't alphabetized. But why?! I would ask and the only answer that I could get back is "its the only thing we could think of" ... not good enough.

I finish with a question ... how does this finally get settled? This has been discussed for just about two years now, when does a decision get made? And who makes it?

OK

Jeff Burnz's picture

Thus far I have pretty much refused to be drawn on this topic, but really its time to start putting this to bed and nail it down - atm we're writing CSS for core (and have been for the past two years) with very little guidance - that said there has certainly been efforts to follow the proposed standards less the notable exceptions of alphabetization and comment styles (the latter of which is being followed most of the time - Bartik was a notable exception that used proprietary comment styles based on the maintainers preference - never a good thing for core).

Some things that just makes sense and appear to be developing practices in core:

  1. Grouped selectors each on a new line.
  2. Each property on a new line.
  3. Group declaration blocks (no separating lines), separate groups of declarations with a line and introductory comment.
  4. Indent properties with 2 spaces (not a tab or no indentation).

Alphabetized selectors separated by a line is a no-brainer - with one very notable exception - the classic reset which gets pretty absurd if broken out onto a new line for each selector (see the Corolla themes reset, as it was when proposed for core), its much more common and sensible to be like...

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
  ...
}

Alphabetized properties - good but needs sensible exceptions - such as:

  1. Browser specific extension such as -moz, -webkit etc fall back to the actual property, thus -moz-border-image is alphabetized under border-image.
  2. Browser specific extensions always precede the actual property.
  3. Browser specific extensions are alphabetized.

The above example from Bartik starts looking like this:

#navigation ul.links li a {
  background: #ccc;
  background: rgba(255, 255, 255, 0.7);
  color: #333;
  -khtml-border-radius-topleft: 8px;
  -moz-border-radius-topleft: 8px;
  -webkit-border-top-left-radius: 8px;
  border-top-left-radius: 8px;
  -khtml-border-radius-topright: 8px;
  -moz-border-radius-topright: 8px;
  -webkit-border-top-right-radius: 8px;
  border-top-right-radius: 8px;
  -khtml-box-shadow: 0 0 15px rgba(50, 0, 0, 0.5);
  -moz-box-shadow: 0 0 15px rgba(50, 0, 0, 0.5);
  -webkit-box-shadow: 0 0 15px rgba(50, 0, 0, 0.5);
  box-shadow: 0 0 15px rgba(50, 0, 0, 0.5);
  text-shadow: 0 1px #eee;
}

As for comments - well... @file docblock makes sense to me, other than that single line comments for short single line comments and docblocks for multiple line comments. Inline type comments are OK but to be avoided and only used for very specific types of comments - such as RTL / LTR comments.

For example:

/**
* @file style.css
* Styles for my kick'n'ass theme
*/

/* HTML Elements */
h1 {
  font-size: 2em;
  font-weight: normal;
  margin: 0 1em 1em 2em; /* LTR */
}
h2 {
  background: #ccc;
  color: #000;
}

/* Forms */
input.form-text,
textarea  {
  padding: 5px;
}

/**
* This is just an example long comment to show that we
* should use docblocks for longer comments.
*/
#someid  {
  ...
}

Note - the code filter does weird shit with docblock indentation and is not really accurate.

As you may well infer I am more for a very strict set of rules - I think anything less allows for arbitrary interpretations and will just be messy and impossible to enforce.

I can't see any room here for personal readability preferences - this is a standard for all where readability is one part, a standardized, easily applied set of rules with very few exceptions is the other. On that note exceptions are OK. Attempting to craft a set of hard rules with no exceptions I think will be extremely difficult - in regards to satisfying both readability and sensibility.

So a few parting comments:

  1. Alphabetized with a few exceptions is the ONLY reasonable proposal, in two years nothing has come up that betters it.
  2. Lets get this done for D8 and lock it in.

We pretty much settled on a

sun's picture

We pretty much settled on a couple of CSS coding standards already.

  • http://drupal.org/node/302199 contains the most current, from which I think all are set in stone already.

  • All remaining questions on any details are actively discussed in the following issues: http://drupal.org/project/issues/search/drupal?status[]=Open&issue_tags=coding+standards

It is a good idea to read all of those issues first before adding and doing any proposals and arguing for or against something. Some of those detailed questions depend on and interrelate with other issues, and a few of them even turned out to have technical constraints.

See you over there. Thanks. :)

Daniel F. Kudwien
netzstrategen

Theme development

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week