views:

154

answers:

4

Starting a new rails project and we have a well-thought-out color palette, and want to capture that in one place. I have usually kept colors in the CSS, but I find I end up with all the same color in lots of different selectors, as it shows up as a background color, color, border color, etc. I also will occassionally need access to colors in the Javascript. It would be great to just define each color ONCE.

So I'd just like to define my color palette in a way that's re-usable in the CSS and Javascript, but I don't want to go all the way to SASS, abandoning CSS syntax completely.

Is there a rails plugin already made that allows this? I could patch together an ERB type solution, but I don't want to do it if someone else has something readily available.

+3  A: 

There is a new project out called {less} that sounds like what you are looking for: http://lesscss.org/

81bronco
+2  A: 

LESS seems to have a rails plugin, and a more css like syntax.

alif
+1  A: 

There are several server side parsers like LESS and SASS, but if you want to use the palett mentality in straight CSS you have to revers your thinking. Define basic styles like colors, fonts etc. and apply multiple classes at the tag level.

[style]

.color1{color:red}

.color2{color:blue}

.color3{color:green}

.bcolor1{color:red}

.bcolor2{color:blue}

.bcolor3{color:green}

[/style]

[tag class="color1 bcolor2"]

This has worked very well for us.

Bernesto
This, of course, eliminates the key benefit of CSS by removing the indirection between your document and your styles.
Doug McClean
I'm against this for the reason Doug gave (you might as well just use inline styles if you're gonna directly associate the class names with the styles they represent). However, there is a related technique that allows you to use both meaningful selectors / class names *and* reduce redundant styles - see: http://stackoverflow.com/questions/47487/create-a-variable-in-css-file-for-use-within-that-css-file/47508#47508 (but note that neither of these techniques do anything for ndp's desire to share color definitions between disparate styles).
Shog9
This is nothing like inline styles you simply are defining that a color is assigned, not what that color is. You are effectively creating a unique color class with from combination of classes. Think of it as light's and dark's.
Bernesto
Re up voted as this post wasn't wrong or unhelpful. This is subjective, and although I don't agree with the post, its not wrong.
Owen
A: 

Another (pure CSS) way may be to define each color once, and have the several selectors associated with that oe color definition, for example:

body,
p,
#foo,
.bar {color: #802369 }
ChrisW