Live data from Hacker News

Ask HN: Anyone have a really smart way to organize css?

news.ycombinator.com

21–30 of 122 posts

Re: Ask HN: Anyone have a really smart way to organize css?

#21
After many years doing CSS my best workflow has come to this:

    reset.css //for resetting browsers
    grid.css //if I'm using a css framework
    global.css //styles that are shared across the site
    section.css //styles that pertain a specific section. The name of the file varies, i.e. "about.css".
You need a good code editor that allows you to open files without tabbing or reaching for the mouse, I use Textmate's Command T to switch fast among my files.

reset.css There's a bunch around, I use the one from htmlboilerplate.com, but there's many good ones available. (Eric Meyer's). You will almost never touch this file.

grid.css I only use this occasionally, when I'm working on sites where the grid is very clear and I take out all the stuff I'm not going to use. I usually go for a three col version of 960.gs and trim it to about 12 lines of css. Never touch this.

global.css Here you put your nav, your footer, you body styles, etc. I think that separating by colors and typography doesn't make sense, because you usually change a widget's appearance.

section.css I count on the body tag having a classname, so I can have body class="about" and then do...

    .about section.photo {...}
This way you never override your styles accidentally.

Miscellaneous I avoid the one declaration per line convention when I have similar styles and I want to be able to read them in a table format, i.e.

    .available {background-color: #0f0;}
    .taken     {background-color: #00f;}
    .deleted   {background-color: #f00;}
I usually start from the most generic to the more specific, but I don't worry too much about code order because in the end I just do a search and reach it in no time.

Re: Ask HN: Anyone have a really smart way to organize css?

#22
post #19

Natalie Downe, my co-founder, has a very neat technique for organising CSS which she calls a CSS System. She described it in this talk: http://lanyrd.com/2008/barcamp-london-5/sg/ She splits everything in to general styles (basic HTML elements), helper styles (things like forms, notifications, icons), page structure (header, footer, layout columns etc), page components (reusable composable classes for components that…

ID's are useful for unique element styling which will always be a case in a website. There's certain elements that are absolutely unique.

Goes hand-in-hand with javascript to ease selector targeting.

I never scope ID's either, that's the wrong way to do it. An ID is always unique so it is top-level.

Re: Ask HN: Anyone have a really smart way to organize css?

#23
A good example of some super organized CSS is in the HTML5 Boilerplate by Paul Irish. Found here: https://github.com/paulirish/html5-boilerplate/blob/master/c... I sometimes use multiple instances of the same selector, especially if a project starts getting very big. I like to separate each function of the site into it's own block after the footer declarations. It lets me stay organized during development and ensures that I can keep track of what I'm working on while I'm working on it. Here's the order I'm currently using, which was inspired by the above:

    /* HTML STRUCTURE STYLES */
    body { foo: bar; }
    h1 { foo: bar; }
    a { foo: bar; }
    /* Image Replacement & Hacks */
    .ir { foo: bar; }
    /* Container Styles */
    #header { foo: bar; }
    #main { foo: bar; }
    #footer { foo: bar; }
    .column { foo: bar; }
    .sidebar { foo: bar; }
    .img-cotainer { foo: bar; }
    /* Everything inside of #header */
    .navigation { foo: bar; }
    #header li.nav { foo: bar; }
    /* Everything inside of #main */
    .content { foo: bar; }
    /* Everything inside of #footer */
    .social { foo: bar; }
    #footer li.nav { foo: bar; }
    /* Everything in specific view inside of a container from above */
    .profile { foo: bar; }
    .comments { foo: bar; }
    /* Media Queries */
    @media all and (orientation:portrait {
      * { foo: bar; }
    }
    /* Print Styles using Media Query */
    @media print {
      * { foo: bar; }
    }

Re: Ask HN: Anyone have a really smart way to organize css?

#24
For me the biggest goal is fewer lines to organize in the first place rather than any particular scheme for grouping. What I do is:

1) Use as few separate stylesheets site-wide as possible so any contradicting or repetitive rules will be obvious.

2) Use one line per rule so I can scan the selectors quickly, then scroll horizontally if necessary

3) I prefer complex comma-separated selectors which set one or two properties to simple selectors which set many properties. Grouping in this way gets me closer to having constants, e.g. a specific hex color won't be rewritten again and again, it will just follow a complex selector list.

4) Once a selector works, I try to make it more general, e.g. "div.info { font-size: 12px }" can probably just be ".info { font-size: 12px }". More general rules will apply in more cases, so fewer overall rules will be necessary.

Re: Ask HN: Anyone have a really smart way to organize css?

#25
On an MVC site, I organize by controller. There's one general file for generic cross-site stuff (header/footer, etc.), supplemented by another CSS file that shares the markup for all controller-specific pages.

This gives a max of two css files per page. For the landing page I compress the generic file along with the controller-specific css file for the default page into a single file. This speeds up the loading process while preventing too much bloat. Since the other pages are usually hit after the landing page they require only a single extra download. Any reset code can be included in there as well, although maintained as a separate file.

I doubt this approach is ideal when it comes to organizing things, but it's fairly fast when it comes to development. I design by including css in style="" and then refactoring the markup into classes once the design is complete. Saves a lot of back and forth while getting things lined up.

Re: Ask HN: Anyone have a really smart way to organize css?

#26
I've really enjoyed SASS/SCSS optionally with Compass. It lets you compile the stylesheets while still keeping the source files separate and organized. I've also seen the approach with one big file for the application with commented sections, however, the drawback to that approach is using a version control system with multiple editors on a large file (merge conflicts, etc).

Re: Ask HN: Anyone have a really smart way to organize css?

#28
post #24

For me the biggest goal is fewer lines to organize in the first place rather than any particular scheme for grouping. What I do is: 1) Use as few separate stylesheets site-wide as possible so any contradicting or repetitive rules will be obvious. 2) Use one line per rule so I can scan the selectors quickly, then scroll horizontally if necessary 3) I prefer complex comma-separated selectors which set one or two proper…

2) Use one line per rule so I can scan the selectors quickly, then scroll horizontally if necessary

I'm going to finally start turning off text wrap. You've opened my eyes to this. Thanks for sharing.

Re: Ask HN: Anyone have a really smart way to organize css?

#29
post #19

Natalie Downe, my co-founder, has a very neat technique for organising CSS which she calls a CSS System. She described it in this talk: http://lanyrd.com/2008/barcamp-london-5/sg/ She splits everything in to general styles (basic HTML elements), helper styles (things like forms, notifications, icons), page structure (header, footer, layout columns etc), page components (reusable composable classes for components that…

ID's are useful for unique element styling which will always be a case in a website. There's certain elements that are absolutely unique. Goes hand-in-hand with javascript to ease selector targeting. I never scope ID's either, that's the wrong way to do it. An ID is always unique so it is top-level.

The problem with absolutely unique elements is that, in a complex enough website (that evolves over time), they often end up not being unique after all.

I agree about IDs for JavaScript though.

Re: Ask HN: Anyone have a really smart way to organize css?

#30
At some point, the amount of CSS complexity becomes best handled by a preprocessor.

Stylus is great: http://learnboost.github.com/stylus/

I made a post about it, and personally prefer it to Sass and Less: http://nylira.com/stylus-the-revolutionary-successor-to-css/ The hardcore abbreviation mixins in my post seem to offend some coders. When you work in CSS and HTML hours every day though, every character saved adds up to a huge productivity boost.

My current project has 30 directories with 73 partial Sass files, which compile down to one 320kb file.

As to your question, I organize my partials based on controller and url structure.

Post reply on HN