Ask HN: Anyone have a really smart way to organize css?
31–40 of 122 posts
Re: Ask HN: Anyone have a really smart way to organize css?
#32Re: Ask HN: Anyone have a really smart way to organize css?
#33She 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?
#34Earlier 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.
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?
#35To 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.
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?
#36Re: Ask HN: Anyone have a really smart way to organize css?
#37html-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?
#38At 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…
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?
#39Then, 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?
#40At 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…
Keep in mind that this only 42k gzipped.