Live data from Hacker News

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

news.ycombinator.com

91–100 of 122 posts

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

#91

I have four core files: reset.css - I use eric meyer's elements.css - Global defaults for things like body, h1, h2, h3, p, a, input, strong. All or almost all selectors in here are just tag names. layout.css - Just sets up the global layout with containers: e.g. header, footer, left column, right column. blocks.css - Reusable chunks. In addition to that, I use a separate file for each "page type". For example, 'artic…

What would you put in chunks.css? Could you maybe provide a brief example?

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

#92

Earlier quoted context omitted.

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

What about an ID for main page elements and things that never will change like #header or #footer or #sidebar?

Ideally for those you would use the html5 header, footer, and aside tags, respectively. You can use the html5 Javascript shim to enable support for those tags in IE.

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

#93
post #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,…

I do this, but with a slight tweak: instead of naming the colors things like $red, I try to give them semantic names like $shadow, $highlight, etc. in case I end up having to pick a completely different color scheme.

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

#94
post #29

Earlier quoted context omitted.

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.

It is not equivalent in any sense. Let's not forget specificity.

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

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

ID is unique on the page. Website can have many pages. If there is a need to style some element of the page differently scoped IDs can be handy.

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

#96

I have four core files: reset.css - I use eric meyer's elements.css - Global defaults for things like body, h1, h2, h3, p, a, input, strong. All or almost all selectors in here are just tag names. layout.css - Just sets up the global layout with containers: e.g. header, footer, left column, right column. blocks.css - Reusable chunks. In addition to that, I use a separate file for each "page type". For example, 'artic…

That makes a lot of CSS files. Google chrome network optimization would tell you to reduce the amount of includes to speed up your site. That said that is debatable since it would be quickly cached.

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

#97
post #96

I have four core files: reset.css - I use eric meyer's elements.css - Global defaults for things like body, h1, h2, h3, p, a, input, strong. All or almost all selectors in here are just tag names. layout.css - Just sets up the global layout with containers: e.g. header, footer, left column, right column. blocks.css - Reusable chunks. In addition to that, I use a separate file for each "page type". For example, 'artic…

That makes a lot of CSS files. Google chrome network optimization would tell you to reduce the amount of includes to speed up your site. That said that is debatable since it would be quickly cached.

As he says at the end, one would obviously not just stick a big stack of `link`s in the page. Some kind of build system/asset packager would be necessary and desired (so you aren't forced to write a huge monolithic css file that no one but your past-self can navigate :)).

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

#99

Earlier quoted context omitted.

"I agree about IDs for JavaScript though." Would you mind elaborating on the JavaScript point? Is it for convenience with document.getElementById or something? Using jQuery, I tend to prefer class selectors the same as in my CSS.

Jquery class selector is the slowest selector, avoid them as much as possible. ID selector is the fastest. So, if your application is jquery intense, better avoid css selectors.

in modern querySelector implementations (any real browser), class look-ups are just as fast as IDs.

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

#100
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…

I checked the lanyrd.com stylesheet. I'm sorry to say but if your stylesheet is this big you are doing it wrong. 100K for a compressed stylesheet! So I'm not so sure her technique is a winner.
Post reply on HN