Ask HN: Anyone have a really smart way to organize css?
51–60 of 122 posts
Re: Ask HN: Anyone have a really smart way to organize css?
#52Earlier 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…
Re: Ask HN: Anyone have a really smart way to organize css?
#53i saw one guy's css once it it looks like: .wrapper { ... } .sidebar { ... } .content { ... } the indentation was well done.
Re: Ask HN: Anyone have a really smart way to organize css?
#54i saw one guy's css once it it looks like: .wrapper { ... } .sidebar { ... } .content { ... } the indentation was well done.
Re: Ask HN: Anyone have a really smart way to organize css?
#55Suprised 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…
Re: Ask HN: Anyone have a really smart way to organize css?
#56Generally, 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?
#57By 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 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?
#59ySlow 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 -…
Re: Ask HN: Anyone have a really smart way to organize css?
#60Natalie 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.