Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I organize the contents of my CSS file(s)?

Tags:

css

This question is about organizing the actual CSS directives themselves within a .css file. When developing a new page or set of pages, I usually just add directives by hand to the .css file, trying to refactor when I can. After some time, I have hundreds (or thousands) of lines and it can get difficult to find what I need when tweaking the layout.

Does anyone have advice for how to organize the directives?

  • Should I try to organize top-down, mimicking the DOM?
  • Should I organize functionally, putting directives for elements that support the same parts of the UI together?
  • Should I just sort everything alphabetically by selector?
  • Some combination of these approaches?

Also, is there a limit to how much CSS I should keep in one file before it might be a good idea to break it off into separate files? Say, 1000 lines? Or is it always a good idea to keep the whole thing in one place?

Related Question: What's the best way to organize CSS rules?

like image 499
Adam Bellaire Avatar asked Sep 28 '08 15:09

Adam Bellaire


1 Answers

Have a look at these three slideshare presentations to start:

  • Beautiful Maintainable CSS
  • Maintainable CSS
  • Efficient, maintainable, modular CSS

Firstly, and most importantly, document your CSS. Whatever method you use to organize your CSS, be consistent and document it. Describe at the top of each file what is in that file, perhaps providing a table of contents, perhaps referencing easy to search for unique tags so you jump to those sections easily in your editor.

If you want to split up your CSS into multiple files, by all means do so. Oli already mentioned that the extra HTTP requests can be expensive, but you can have the best of both worlds. Use a build script of some sort to publish your well-documented, modular CSS to a compressed, single CSS file. The YUI Compressor can help with the compression.

In contrast with what others have said so far, I prefer to write each property on a separate line, and use indentation to group related rules. E.g. following Oli's example:

#content {     /* css */ }     #content div {         /* css */     }     #content span {         /* css */     }     #content etc {         /* css */     }  #header {     /* css */ }     #header etc {         /* css */     } 

That makes it easy to follow the file structure, especially with enough whitespace and clearly marked comments between groups, (though not as easy to skim through quickly) and easy to edit (since you don't have to wade through single long lines of CSS for each rule).

Understand and use the cascade and specificity (so sorting your selectors alphabetically is right out).

Whether I split up my CSS into multiple files, and in what files depends on the size and complexity of the site and the CSS. I always at least have a reset.css. That tends to be accompanied by layout.css for general page layout, nav.css if the site navigation menus get a little complicated and forms.css if I've got plenty of CSS to style my forms. Other than that I'm still figuring it out myself too. I might have colors.css and type.css/fonts.css to split off the colors/graphics and typography, base.css to provide a complete base style for all HTML tags...

like image 68
mercator Avatar answered Sep 24 '22 00:09

mercator