Live data from Hacker News

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

news.ycombinator.com

51–60 of 122 posts

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

#51
I tend to build all my DOM in object oriented javascript, and always give the root element of a UI component the same class name as the name of the JavaScript class. See eg https://github.com/marcuswestin/Focus/blob/master/js/ui/pane.... The CSS selectors then mirror the structure of thr JavaScript files as well as the hierarchy of the resulting UI.

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

#52
post #10

Earlier quoted context omitted.

I prefer the latter. It's more readable IMO. If file size is an issue, I just use a CSS compressor. Also, it's not too hard in finding selectors, most IDE's or text editors have a "Find" command to locate a selector. That's my 2 cents.

This isn't about file size at all, it is about being able to see as much CSS as possible at one time. My screen is wide, my editor is wide, my code is fairly wide, but the standard format for CSS is really narrow. Every time I see the vertical format in my editor, easily 80% of the window space is completely empty, and otherwise useless, and 80% of the CSS is not on the screen and I have to page up and down, or searc…

I do it too and for the same reasons. Just easier to keep your head around if you can see and scan more of the file at once. I tend to list properties with the most crucial to layout first so it's easy to find major problems (e.g., float, etc).

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

#54
post #16

i saw one guy's css once it it looks like: .wrapper { ... } .sidebar { ... } .content { ... } the indentation was well done.

We had a GUI 'designer' who did the exact same thing to our whole site. Spent ages trying to get stuff to work... :)

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

#55
post #33

Suprised no-one has mentioned Nicole Sullivan's (amongst others) Object Oriented CSS (OOCSS) project: https://github.com/stubbornella/oocss/wiki She used this approach at Facebook (and Yahoo, I think?) to successfully tidy up a huge code base of thousands of CSS files down to a more manageable few. I attended her workshop at Webstock and since then had a chance to put it into practice on a 'get it up quick' green fie…

I'm really interested in the OOCSS approach, it's efficient, maintainable and modular, and although it's not perfectly semantic the trade-off is minimal. I first discovered it from the Velocity 2009 conference, and Nicole Sullivan's talk from Web Directions North - http://goo.gl/yLkby

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

#56
I can't recommend Compass/Sass highly enough. A CSS pre-processor will completely free you to organize your CSS however you want, and more importantly, it lets you build up a library of common components (forms, buttons, etc.) that you can easily reuse and adapt for new projects.

Generally, these library files will simply contain mixins (reusable chunks of code), so they don't output anything directly into your CSS, but allow you to include the mixins in certain styles. Very useful for adding effects, rounded corners, etc. on different elements.

Keep in mind however, that mixins can be overused, adding bloat to your code. CSS does cascade, after all. You should always look to see if you can separate reusable css rules to include in markup (commonly seen with grid systems). How you balance out the convenience of keeping your css flexible vs. not littering your markup with lots of styles really depends on the project, but it's something to think about.

I have developed one very useful trick while using Sass for managing colors. Instead of assigning colors directly to an element (very hard to track down later if a change is necessary), I create color variables named after the element and attribute in question. Then in my colors.scss file, I build up my color palette, and after, list all the color variables I created in my stylesheets, setting their values to the appropriate color from the palette (with tweaks, if necessary).

  // in colors.scss ---

  // color palette 
  $red: #ff0000;
  
  // assign colors to elements
  $body-background-color: $red;

  // in layout.css, for example ---

  body {
    background-color: $body-background-color;
  }
Since Sass lends itself to lots of files, keeping all my colors and the elements they are assigned to together in one file makes them much easier to manage down the road.

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

#57
ySlow recommends at most 3 stylesheets, and with good reason: the HTML headers and request times can drastically slow a site down.

By simply organizing the structure of the file well, you can have a easy to navigate css file. Of course there may be a few extra style sheets; ie.css, section specific.css, print.css and mobile.css.

Here's my CSS file structure:

-reset

-html tags h1,p,q etc

-header

-navigation

-content

-sidebar

-misc (popups, forms, buttons)

-footer

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

#58
I also use Sass and Compass. I generally organize the directory roughly like this for projects without a separate designer:

    stylesheets
      - ie.sass
      - master.sass
      patials
        - _base.sass
        - _forms.sass
      utilities
        - _custom_mixins.sass
      vendor
        - _facebox.scss
        
The files with leading underscores are partials which are included in the master.sass. Note that facebox is an scss file. I just dump the CSS in it and include the partial in master.

The nice thing about sass is that it you can change output format, so you can just include all the external CSS files as partials and automatically output a single compressed files with your entire CSS.

When I work with a designer who uses CSS I use a structure like this:

    stylesheets
      - master.sass
      partials
        - _style.scss
      vendor
        - _facebox.sass
In this case, the designer's CSS is dumped in the _style.scss partial, then included in master.sass. Then in master.sass I include my modifications to the designer stylesheets. That way I can leave the designer-provided CSS largely untouched and still have all the flexibility and power of Sass.

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

#59
post #57

ySlow recommends at most 3 stylesheets, and with good reason: the HTML headers and request times can drastically slow a site down. By simply organizing the structure of the file well, you can have a easy to navigate css file. Of course there may be a few extra style sheets; ie.css, section specific.css, print.css and mobile.css. Here's my CSS file structure: -reset -html tags h1,p,q etc -header -navigation -content -…

It's generally a good idea to concatenate your stylesheets for production - doesn't mean you have to write them as one big file, though.

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

#60
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.

Another problem with IDs is that they make the "point system" in selectors much more complicated than if you only use classes and elements.
Post reply on HN