Live data from Hacker News

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

news.ycombinator.com

31–40 of 122 posts

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

#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 fields project (http://chchneeds.org.nz). I must say I was really pleasantly surprised at the way this approach just avoids a lot of the pain points, as a web developer/coder (ie not an html/css guru) I so often face when just getting the simplest things to 'work'.

I guess the hardest thing for me to get my head around to was that to make things more modular you had to let go (a tiny bit) of being so religious about 'semantic html' as a requirement for the HTML, but I think its worth it to get your CSS a whole lot more modular and 'pluggable' together.

Still a bit more work to do, IMHO, but I'm definitely going to be monitoring this project closely.

Nicole Sullivan's site: http://www.stubbornella.org/content/

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

#34
post #29

Earlier quoted context omitted.

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.

Exactly, as far as CSS is concerned an id is effectively equivalent to a class name except when it breaks things.

Ids should be used only for scripts, and most of the time should be auto generated anyway.

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

#35
post #9
post #5

To be honest. With firebug's ability to tell me what line and css files the style on an element is coming from I don't find spending a lot of time on organization to have a ton of benefit.

While I love Firebug, I'd have to counter that organization with CSS is actually quite important (and does have its benefits!) This is especially true when you're working either in a team environment or on a large site. The very nature of CSS (and how things cascade down) can become a nightmare when organization isn't taken into account from the start.

I agree completely, especially on a web app that is constantly growing. 2 on-the-job things that help me the most are:

1. Using the 960 grid system (or your preferred grid flavor) 2. Grouping by the following:

Body Typography Forms (Other global stuff like links and buttons) /* Layout Element 1 / ID1 {} ID2 {} class1 {} class2 {} / Layout Element 2 */ ID3 {} ID4 {} class3 {} class4 {}

I also write my styles on one line, but that's just personal preference. I find it easier to scan down for the selector, then across for the property I want. I definitely don't do it for aesthetics or file size ;)

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

#36
I once tried breaking my css into files according to function (typography.css, color.css, layout.css). It ended up being a royal pain because when I added a new HTML element and I went to style it, I ended up having to duplicate the css selector for that element in three files. I also was constantly switching among the three files to work with one element's styles. So whatever you do, don't organize by function. :)

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

#37
Ever since using Drupal's Zen theme I have preferred their basic setup for grouping CSS files. Here is a rough example:

html-reset.css layout-fixed.css page-backgrounds.css messages.css pages.css blocks.css navigation.css comments.css forms.css fields.css print.css ie.css ie6.css

Drupal's CSS optimization, once turned on, will roll all of these up for you automatically, so if you aren't using Drupal you will need some sort of build system to combine and minimize everything. But I think this is a good start.

On a side note, I really like the method the Zen theme uses for CSS columns. Worth checking that out as well.

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

#38
post #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 u…

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

Isn't that a lot to download? That's like 2 seconds on average to download that file in the U.S., and probably in the 10 seconds or higher range for a lot of folks. I guess if the download is delayed to after some kind of engagement (sign up, etc) it's not a big deal?

Is 320K of CSS really necessary? (legitimately asking)

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

#39
Ideally, I use Sass with Compass (http://compass-style.org/). This alone is a great start to keeping things organized. One of the things I love about Sass is that it allows partials and variable declarations, which both help immensely with code organization.

Then, my code is usually organized as follows. Each section can be extracted to a partial if it gets long enough or if more separation is desired.

  Framework includes and resets
  Variable declarations — colors, possibly specific widths  
  Universal method declarations
  Standard global tags — headings, p, a, ul, li, etc... 
  Generic form styling, usually in a dedicated partial
  Layout — container, header, content, footer
  Then to specific section styling
Since Sass uses indentation, I indent everything in each section within a top level tag. This really helps visually distinguish each section and gives a much less uniform look to the stylesheet which makes it a lot more scannable. If a section uses methods exclusively I'll declare them just above the top-level section tag.

I'm pretty happy with this approach. It was formed fairly organically but become more defined as I use it for more projects. It also seems to work well for both large and small projects, since partials can be used when the code starts getting long.

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

#40
post #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 u…

My current project has 30 directories with 73 partial Sass files, which compile down to one 320kb file. Isn't that a lot to download? That's like 2 seconds on average to download that file in the U.S., and probably in the 10 seconds or higher range for a lot of folks. I guess if the download is delayed to after some kind of engagement (sign up, etc) it's not a big deal? Is 320K of CSS really necessary? (legitimately…

320K is a lot, no question. This project hasn't been released to public yet, and there are areas to optimize.

Keep in mind that this only 42k gzipped.

Post reply on HN