On the #drupal IRC channel, Excallibur points out that there are no coding standards for CSS. I'd like to propose some straightforward ones.
Note that I am not intentionally omitting indentation within rules, but am having trouble with my code filter.
This java-style of CSS does not make the code easier to read:
#rule1
{
margin:0px;
}It just adds an unnecessary space. Below is the correct way, which is consistent with core CSS.
#rule1 {
margin:0px;
padding:1em;
}While the following code is more compact, it's also more difficult to scan.
#rule1, #rule2 {
margin:0px;
padding:1em;
}The right way is separate lines.
#rule1,
#rule2 {
margin:0px;
padding:1em;
}This small rule can make a big difference in complex themes (ever tried to debug some old civicspace themes?).
Single line rules are acceptable when there is only one selector and one property.
a {text-decoration:none;}
pre {font-size:1em;}Can you think of any others? Disagree?
Comments
totally agree
I've been writing my CSS this way for years, And it totally bugs me when CSS is not in this format. As far as a quick fix for formating a style sheet that is in non-compliance, Topstyle has a great CSS formatting utility. For Mac users all i've found is CSSEdit's re-indent Style Sheet command. It works but it kinda barfs on CSS that is heavily commented or has hacks. There's also online CSSTidy formating tools, pick your poison.
VI rules
Any suggestion for creating rules in vi or Zend Studio to help out with this? Like 'set stoptab=2'. What are some others? (yeah I guess emacs should be covered as well).
Agreed
I agree with your suggestions and also that the indent should be two spaces. This makes it consistent with writing things like if statements in php (personally my fingers can't help but to put the brackets this way anyway.) I noticed in a recent version of zen theme that the brackets are on their own lines in the css and nothing is indented and it makes me have to fight some OCD urges to want to change them all to look like what I think is correct.
Wanna talk about OCD, I saw
Wanna talk about OCD, I saw those brackets and threw up my hands, and declared: "I CAN'T WORK WITH THIS". I don't defend my reaction, however... these sort of things bring me to the brink of insanity.
single line rule and spacing
I disagree about the single line rule. I would go for consistency (every property on its own line), which is better for visual parsing and maintainability.
Another thing that could be recommended is the spacing around the colon. I prefer "foo: bar;" instead of "foo:bar;".
Both of these things are common in the already existing css files from drupal core.
Well, the first examples I
Well, the first examples I learned CSS from did it without a space. The spaces don't seem necessary like they do with arrays, e.g. array('foo'=>'bar'), vs array('foo' => 'bar'), or $var='foo', vs $var = 'foo';
haha, I ended the sentences with a semi-colon -- it seems correct to me.
Nick, definitely a space
Nick, definitely a space after the colon: please;
I personally like the look of indenting (maybe because it looks more like drupal code). But also queezing everything to the left makes it blend in with the selectors and hence harder to visually parse.
well, i tried to indent --
well, i tried to indent -- but the code filter turned my two spaces into nothing for some reason. Goes to show: copy and paste code from your editor, don't write it in blogposts.
With the space after the colon -- I don't know whether it really matters. It certainly doesn't disrupt my ability to read CSS files for errors like java style CSS, or rules notated as such
a:href, div#example, span.unnecessary, div.shouldofusedaparagraphtag, div.shouldhaveusedaheader {
color:black;
}
white-space-collapse
CSS Level 3 is partly supported as far as I know. "white-space-collapse:preserve" in your code tag might fix this problem in some browsers.
Mother of mercy, I specified
Mother of mercy, I specified two spaces... damn drupal 4.7, and the weird input filters this blog inhereted from drupal 4.5! I'd fix the issue, but I'm lazy.
Hi Good post. For me
Hi
Good post. For me personally I also prefere to use an indent. I think it makes it more clear of what is selector and what is property.
I have to agree AND disagree
I have to agree AND disagree with most of your points, if that's possible!
The only strong recommendation I have is that single line rules must have a space before the brackets: ie
{ font-size: 10px; }NOT{font-size: 10px;}. Much easier to read.The reason I disagree is that as somebody who spends all day in CSS files, I prefer less lines as much as possible, as long as it doesn't hamper readability. A couple of your suggestions would add probably a third or more lines of additional code, which I would be loathe to do.
However, the one strong point with your suggestions is that they leaver no room for interpretation, which is good in a framework environment -- unlike my personal habits which require some judgment calls sometimes.
Thanks,
Rene
My 2 cents
I don't think a strict CSS coding standard for Drupal is as important as that in the PHP.
I say this mostly because I have a highly unusual style of writing CSS.
Here is a snippet:
#header-region {
font-size:12px;
background:url(core/header.gif) 50% 0px no-repeat;}
#header-region a {
color:#999;
padding:4px;}
#header-region a.active {
font-weight:bold;
color:#164;}
#header-region a:hover,
#header-region a.active:hover {
color:#fff;
background:#7a8;}
#header-region .limiter {
position:relative;
height:60px;
background:url(core/header.gif) 50% 0px no-repeat;}
While I would have no problem switching to putting the closing bracket of rules on their own lines, I have serious misgivings about non-indented CSS structure.
It is to me as troublesome as failing to indent if, foreach and other such structures in PHP. My main reason for following cascading / structured formatting of CSS is it documents and implicitly represents how the language actually behaves -- if you apply a rule to a parent element like #header-region, it will cascade to its children. This makes tracking redundant rules, rule overrides, and the such much simpler and straightforward, even without the help of DOM inspectors like Firebug.
I of course understand that I am one of 2 people I have ever met who approach CSS this way. So I don't suggest that this be a standard. I just hope a conflicting standard doesn't get imposed on me : )
I agree with points 1 and 2,
I agree with points 1 and 2, but not 3. The reason I don't like single line rule-sets (even if it's one property) is because when someone comes along to add an extra property, they're required to reformat the whole rule-set.
Also, I'm strongly opposed to separating each brace onto its own line, it wastes a ridiculous amount of space and makes it harder to tell where each ruleset starts and finishes (as if the page becomes flat, or something).
fieldset{
..border: 1px solid #000;
..background: #eee;
}
fieldset.important
{
..border: 1px solid #f00;
..background: #fee;
}
Compared to (visible 'chunks' of code):
fieldset {..border: 1px solid #000;
..background: #eee;
}
fieldset.important {
..border: 1px solid #f00;
..background: #fee;
}
Another thing I dislike (but not that much) is blank lines between rule-sets, a line with a single brace '}' is pretty much a blank line in itself.
Disagreement going in the other direction
I don't find multi-line selectors any harder to parse, so long as they make sense in the first place. I code with the openig brace on the top line, because that is how I learned and it is how I code functions. I don't see any need or benefit in puttijng the opening brace on its own line.
I am not doing this out of a mania for compactness. To the contrary, my style.css files tend to be complex, so I like to separate color, typography and position into their ow3n sections, so there are often multiple selector calls. I have been doing elastic themes, and I find that this is a big help when the client wants type resized relative to the containers.
#primary, #blah {background-color: #4B1703;
}
Actually I like the missing indentation!
I agree with all of the suggestions but I'd like to add that omitting the indentation actually looks better to me. I've been doing that for a long time now and it feels just right. The reason I started doing that was because I was using the edit CSS feature of the Firefox Web Developer extension and I had limited space and didn't want to waste it on whitespace. (And for a long CSS file it also reduces the file size as well, but that's not the main point.)
Quite agree.
That's pretty much my coding style too, although I add some spaces in single line rules, not this:
a {text-decoration:none;}but this:
a { text-decoration: none; }I think the latter increases readability.
I also add a space after the colon separating the property from it's value. Again, I think that increases readability.
Now that we have the CSS parser/compressor in Drupal I've become a big fan of adding plenty of comments too, big multiline ones if necessary. Often one comes back to a theme after many months, or is handing it off to someone else. Well commented CSS is just as good a thing as commented PHP - it was a pain when it was all sent to every browser.
The software I currently use for CSS* also has 'groups' where a comment is used to group styles together, like so:
/* @group Main Layout */selector1 {
margin: 0px;
padding: 1em;
}
selector2 {
margin: 0px;
padding: 3em;
}
/* @end */
It's useful for grouping styles together, and has the added bonus of allowing quick navigation in my editor. Not sure how useful that is in general, but something to consider.
@Jeff
Putting all your colour rules in one place is not something that should be part of Drupal's CSS code style suggestions.
@Kevin Montgomery
The properties are indented from the selector anyway, I don't think it increases readability to put the opening bracket on a new line. Instead it just means more scrolling ;)
* MacRabbit's CSSEdit2
no brainer
Nick,
This is a no brainer to add to the Coder module. It was even discussed at Doug's Coder module session at Drupalcon Boston. Thanks for taking the lead on this and not letting it fall through the cracks.
-mike
Indentation
Yes, I agree that indentation is also important. But I don't whether to use spaces or tabulator. I prefer tabulator. I would also put a space between values and properties.
css rules
I like to put all css color declarations at the top of the file, separate from the other rules, where possible. This provides a convenient place to alter the color scheme of a site, without having to search through the whole file. It makes it easier to see which elements share the same colors as well.
Dont you mention about
Dont you mention about indent?
Indentation?
I would suggest indenting the rules -- such as:
#rule1 {
margin:0px;
padding:1em;
}
Spacing and comments
I know you're having trouble with indentation, but it's worth spelling out. One tab = two spaces, as per PHP. I'd add a space after the colon.
Also, comment blocks may as well keep to the Drupal convention for PHP.
I disagree with some points...
I used to write all my css stylesheets exactly as you are proposing; however I switch to make all lines in this format:
#style{
foo: bar;
}
and
#style,#style2
{
foo: bar;
foo2: bar2;
}
The reason is that I feel like it makes one's css faster to visually parse (it is a lot harder to see the element being styled if it's position relative to it's style keeps changing. Additionally, the "unnecessary" line break does aid in parsing).
If you are really concerned about the amount of bites and extra line break takes up, you should be condensing your css on the live site anyways, making it a moot point.
Symbian / Whitesmiths rules
With the additional 2 spaces indents within the braces, this is a typical Symbian / Whitesmiths formatting, and the one I always recommend too in languages where it is applicable.