Freshly brewed posts on digital design, development, the web and commentary.

9rules
3rd
Feb

When you first start writing css there is a temptation as you write it to just add more and more without creating any order to your file. Pretty quickly this becomes a shooting yourself in the foot technique whilst you search through numerous lines to find just one style. There are various for and against arguments to using multiple style sheets but lets look at how you can for now in one file get some basic order and structure.

Group and conquer

One of the simplest things you can do order your css is group by type. Most sites can be divided into some basic type groups:

  • Layout / Structure
  • Typography
  • Navigation
  • Images
  • Forms

To get this basic structure into your CSS when you first create your document it’s a good idea to put something like the following in CSS comments:

/*Layout*/

/*Typography*/

/*Navigation*/

/*Images*/

/*Forms*/

/*Miscellaneous*/

I use layout rather than structure but the naming doesn’t really matter this is the ‘bones’ of your site. I always add a miscellaneous as a sort of section overflow - this often becomes a dumping ground I sort into a section or create new one if it’s required. You can of course drill down even further and I’d always suggest if it’s a component that takes several styles you group that with a new comment. For instance, you may have a top navigation block that uses ul, li and some other link styling. I would in this case have the following:

/*navigation*/
/*top navigation*/
style elements go here

One visual thing you can do to split each section further is even after a section add:

/* end of typography */
or
/* ********************* */

Big things come first

In ordering my typographical elements with regards to headers I always make sure I list them in order so I end up with h1, h2, h3 and so on going down the typographical elements section. I find this makes it far easier to see at a glance particularly when I come back to a style sheet I’ve not been working on for a while. A great way to deal with the same types within these widget groups is to just like the similar elements use further grouping. For instance, all paragraph styles would be together.

What’s the point in being organised?

As I’ve said the main reason is readability, beyond that it means less time spent hunting for the CSS need in a haystack. The semantic naming of your code should extend to clean code that is well organised. Using simple methods like this you can easily spot and understand the CSS even if you are coming to it a long time after it was written. It’s also a great way to work if you are in groups and more than one person is editing the CSS. In the case of groups I like to have a ‘dump’ or scratch section where changes can be put - this works great if you are in control of the CSS but other developers may need to apply styles. I still would always recommend a single person has sole control over the CSS as that helps with consistency - that or have a very stringent organisation that all working on it understand and adhere to.

It may sound a bit like CSS by police, but there is nothing worse than a lines upon lines of chaos dumped CSS where there is no meaning and to find anything you’re trawling through lines of text. There are far more advanced methods of course of organising CSS, this is more the bare minimum you should do. When working in larger groups I think time stamping in comments and even commenting changes can be beneficial. It may on the surface seem a bit of a chore to do this but once you do it will quickly become part of your CSS writing routine and you’ll find it also helps you visualise the styles laying it out.

12 Responses

4th
Feb

Justin Lilly

Hey, I was curious if you might be willing to do something similar to this for folder structure. Specifically folder structure when designing. I currently have something akin to:

Code/
-Documents/
– holds contracts, briefs, etc
-Dev/
– hold source images and such
-Http/
– holds current working copy
-Live/
– holds code approved for production

Do you have any thoughts on this matter? Do you have a better suggestion? Do your readers?

5th
Feb
5th
Feb

It’s so much easier to comprehend different websites’ code when it’s been organised in a standard way.

What about keeping all colour instructions in a separate section at the end? I don’t usually do that but it might make life easier.

5th
Feb

It’s fairly straightforward common sense stuff, but really useful, particularly when maintenance of a site gets handed on to someone who didn’t write the code.

5th
Feb

Clare Warburton

Jina Bolton wrote a similar article: http://www.thinkvitamin.com/features/design/creating-sexy-stylesheets

5th
Feb

I use a similar approach, although I use different categories:

/* 01. BASIC PAGE LAYOUT */
/* 02. PAGE GRIDS */
/* 03. NAVIGATION */
/* 04. TITLES */
/* 05. LINKS */
/* 06. CONTENT - GENERAL */
/* 07. CONTENT - COMPONENTS */
/* 08. FORMS */
/* 09. TABLES */

I usually set a table of contents at the top, sadly there’s no option for anchor links in css :)

5th
Feb

Thank you for the article.

For years I used to take the sectional comments approach when dividing my style sheets. I used to apply an almost JavaDoc-style of commenting to help clarify things for me:

/**
* NAVIGATION
*
* Some comments, notes, etc.
*/
#navigation { …css here }

I then stumbled upon a project called CSSDoc:
http://cssdoc.net/

While it is still in the nascent development stages, this is how I have started formatting my CSS now. Obviously it adds weight to your CSS files, but if you use a script to strip out the comments for a “compressed” version of the file(s) this is a great way to keep your CSS organized.

Cheers!

5th
Feb

Nic Steenhout

I like to do this myself, most of the time. But then, I always struggle with the extra weight of a well laid out CSS file, compared to a file that has been optimised and reduced.

5th
Feb

michelangelo

I like Mike Stenhouse’s approach, which he calls modular CSS. It creates some duplicate selectors (since you may need to target the same element both for its color and, say, its layout) but it works. I am hoping that tools like Stylizer will make all of this moot very soon.

8th
Feb

karmatosed

@Justin: I have a sort of similar process with my files and directories. I use a dump folder for my desktop and separate projects into Design and Websites. I like your ordering and I’ll have to document mine at some point.

@Jason: I agree there is nothing worse than going into a project and working with someone’s perverted logic of sense to code - perhaps a further bolstering for the need for a standard approach to organisation and definitely an argument for commenting in code. I don’t keep colour separate myself but it’s certainly one approach - I prefer to deal element by element myself.

@Richard: Yes I agree it is common sense but you’d probably not be surprised by how many seem to loose their sense when writing CSS. It really baffles me.

@Clare: Thanks for bringing that to my attention and indeed a great article. Jina goes into far more detail than I have as of course I’m purely talking about a single CSS organisation and the method I use but a great article none the less.

@Niels: I agree some form of anchor / documentation linking in CSS would be great but I’m not sure it will ever happen. Your structure looks nice and solid and the key really is if it works for you and is understandable by others any variation works.

@Billee: That is an interesting project - not sure how much for small projects it would be over kill but one to consider for more large scale work I think and I will be reading more on it. Thank you for the link.

@Nic: Yes there is extra weight but it again depends on your project. There are far different methods for large optimised sites. The principle of organisation should stay the same though.

@michelangelo: Interesting not read that and will read a bit more about it. I don’t know I’ve tried so many ‘tools’ to make my code job easier but when it comes down to it I always return to hand coding in Textmate. Whilst interesting I don’t think Stylizer is for me for the same reason I stopped using other tools once tried them. Maybe I just like making my job harder :)

4th
Aug

? ??? ????????…

21st
Aug

?????????!

Name (required)

Mail (will not be published) (required)

Website