Live data from Hacker News

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

news.ycombinator.com

81–90 of 122 posts

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

#81

Use tables for layout instead of CSS. Then your CSS will be more concise (essentially reduced to a theme) and less confusing (because CSS layout properties like float and position tend to cause the most confusion). Edit: Rephrased to try and better express the idea.

joke?

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

#82

Don't overcomplicate things: - Have only one CSS-file, unless your project is huge . - Never use IDs - Avoid reset CSS (espescially if you can ignore IE6/7 - If you think you need variables (like in SASS), you are probably thinking about it the wrong way - Give classes functional names, not presentational - Linebreaks after declaration. (Makes your CSS-file easier to navigate) - Remember that you can use you media qu…

Never use IDs? Why? i'm open to this if you have a good argument but i use IDs for unique elements only.

The problem is that as a site grows, it's common for things that were once unique to become multiple.

Also, if you just resolve to always use classes instead of ids, then you can eliminate the cognitive overhead that comes with every new selector of, "Should this be a class or an id?".

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

#83

Use tables for layout instead of CSS. Then your CSS will be more concise (essentially reduced to a theme) and less confusing (because CSS layout properties like float and position tend to cause the most confusion). Edit: Rephrased to try and better express the idea.

joke?

Not a joke, though I realize it's an unpopular view.

I also had trouble phrasing the idea in a way that didn't sound stupid.

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

#84
I lay my styles out in 1 file and I don't often comment them. Stylesheets cascade to much to pin them to a specific area of use - it's easier to debug them by clever naming still - sigh. IE specific files loaded after and only contain 1 or two tricky IE tweaks. Javascript /Jquery plugins sometimes come with their own stylesheets, and in those cases I'll leave them where I found them. For some of the bigger sites with very different looks and feels, I'll often have a boilerplate (reset to skeleton level) and each page or section will load in their own stylesheet. Just depends how big the site is.

Order is: * Reset (only for early IE )-http://developer.yahoo.com/yui/reset/ I find myself using it less and less

* Global Styles - HTML tags - global font faces

* Clearfix - http://www.webtoolkit.info/css-clearfix.html

* Skeleton/Structural Styles - grids/divs/columns

* Headers/footers/persistent styles

* Page specific styles and IDs

* Conditionally load IE Styles if they're required

* load javascript plugin css with the plugin's preferred dir if possible

As for pipelining them, I usually just use a gzipped connection and still serve them individually, since internal image paths for backgrounds rely on the CSS path. CSS caches very nicely, so take advantage of it on the HTTP side.

example: http://code.google.com/p/streeme/source/browse/trunk/web/css...

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

#85

Earlier quoted context omitted.

Never use IDs? Why? i'm open to this if you have a good argument but i use IDs for unique elements only.

The problem is that as a site grows, it's common for things that were once unique to become multiple. Also, if you just resolve to always use classes instead of ids, then you can eliminate the cognitive overhead that comes with every new selector of, "Should this be a class or an id?".

How many of your pages have two footers?

I do agree on the cognitive load though.

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

#86

Don't overcomplicate things: - Have only one CSS-file, unless your project is huge . - Never use IDs - Avoid reset CSS (espescially if you can ignore IE6/7 - If you think you need variables (like in SASS), you are probably thinking about it the wrong way - Give classes functional names, not presentational - Linebreaks after declaration. (Makes your CSS-file easier to navigate) - Remember that you can use you media qu…

Never use IDs? Why? i'm open to this if you have a good argument but i use IDs for unique elements only.

The main reason is that including IDs makes “selector points” much more complex.

A selected with one ID will outweigh a selector with two classes for instance. This can quickly lead to complexity because you need to use a ID many places in order to override a selector that used ID.

A class only used once serves the same purpose as ID (CSS-wise), expect that you avoid a lot of problems.

I might use IDs for anchor purposes (site.com/foo#section), but I never reference it in my CSS.

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

#87
The system I came up with is A table of contents that makes it easy to jump to specific styles.

First the global styles, then different things like Header, Navigation, Main Content, Right-hand-side-doodad, Widgets, Footer, etc.

Then create a Table of contents at the top and give each section a unique identifier, FEDCBA-Head, FEDCBA-Nav, etc. Go down to each group and put in a comment saying "Start of Navigation CSS ".

Now you can look in the table of contents and do a quick file search on the unique identifier, and be taken right to the CSS you're looking for.

I generally make the table of contents a lot more detailed, and it has an upkeep cost, but it's well worth it when the CSS file gets big.

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

#88

Don't overcomplicate things: - Have only one CSS-file, unless your project is huge . - Never use IDs - Avoid reset CSS (espescially if you can ignore IE6/7 - If you think you need variables (like in SASS), you are probably thinking about it the wrong way - Give classes functional names, not presentational - Linebreaks after declaration. (Makes your CSS-file easier to navigate) - Remember that you can use you media qu…

> - If you think you need variables (like in SASS), you are probably thinking about it the wrong way

I've yet to see CSS code for page layout without the usual awkwardness where margin-left of some element MUST equal some other element's width minus some bar's left offset, yadayada. It's for stuff like this that variables make sense. I wholeheartedly agree that if you need a variable to colour two bars and a heading orange, you're probably doing it wrong.

The blog you refer to only has an example with colours, which is just too easy.

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

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

"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.

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

#90

Don't overcomplicate things: - Have only one CSS-file, unless your project is huge . - Never use IDs - Avoid reset CSS (espescially if you can ignore IE6/7 - If you think you need variables (like in SASS), you are probably thinking about it the wrong way - Give classes functional names, not presentational - Linebreaks after declaration. (Makes your CSS-file easier to navigate) - Remember that you can use you media qu…

I think you should definitely use multiple files. By all means concatenate them and minify them for production, but I find it very helpful to have CSS files for layout, forms, and specific sections of the site or app.

CSS complexity is increasing at a huge rate and the volume of CSS for even a simple site is running at hundreds of lines. Splitting it into separate files keeps it readable and reusable.

Post reply on HN