tags:

views:

171

answers:

8

I want to include a class within a class - sort of like variablizing a class - so that I can define something once and use it throughout the sheet. If I change the color, it changes everything.

.myFontColor{color:blue;}
.myTitle{font-size:large; myFontColor}
.myHeader{font-weight:bold; myFontColor}

The only way I see to do this is. To include every class name in the definition, but if I have 20 or 30 items in the sheet referring to that color, it's going to get ugly really fast.

.myFontColor, .myTitle, .myHeader{color:blue;}
.myTitle{font-size:large;}
.myHeader{font-weight:bold;}

or to list a each class when I create an element, which gets a little messy too.

<div class="myFontColor myTitle myHeader">

Thanks for your help.

A: 

Variables (as you need them) are not possible in CSS at this time. You would have to use the Cascade, as you have done, and include all of the classes in the definition.

CLaRGe
+1  A: 

You can write several classes when defining an element:

<div class="blue w210pt">
</div>
<p class="green w210pt">
</p>

<style>
.green{color:green;}
.blue{color:blue;}
.w210pt{width:210px;}
</style>
chosta
You answer is valid, but tends to get messy after a while too. This is pretty much what I do now and was hoping for a better way. I edited my question regarding this. Thanks for the response.
Praesagus
this is a poor example of CSS. the classes should describe the element, not the style. you might as well hard code it if you're going to do this.
Ben Scheirman
I agree its not the best and most beautiful solution but I've been using this method with success for small web-sites.It increased my speed and productivity, kept my clients happy and my bank account fat.
chosta
A: 

You may want to take a look at Sass, something from the Rails community that makes you use a slightly different syntax but allows for some smarts like this. If you're not doing the Ruby/Rails thing, you can still use Sass as a command-line preprocessor to translate your Sass files in to CSS.

Jim Puls
+1  A: 

As you suspect, you can't do what you describe in a direct way -- you need to use the cascading aspect of CSS to get what you want. However, I think you may be misusing CSS to some degree. You should be defining semantics and then connecting your presentation to those semantics. For example, seeing a class like .myFontColor would be a big hint that you're not really using classes to represent semantics (although I recognize this is probably simplified for the example).

John Feminella
yes my class names were for illustration only. :)
Praesagus
+2  A: 

The only way I can think of would be using javascript to dynamically assign the attributes. For example, make a list of all the attributes that you want to configure (such as color, font-size, font-weight) and a list of all the classes you want to be configurable (such as .myHeader, myTitle, etc). Then do something like this:

for (x=0; x< cssClasses.length; x++)
{
  addAttribs(cssClasses[x]);
}

function addAttribs(class)
{
   $("." + class).css("color","#444");
   $("." + class).css("font-weight","bold");
   //And so on..
}

This may be the closest you can get, and it will have the advantage of all the attributes stored in one function, so if you change it there it will be updated everywhere else.

Edit: By the way, I'm using Jquery in the addAttribs() function to easily access and set the css attributes.

Click Upvote
I did think of this but was desperately hoping for something built in to the style model... :| The example is great though and I may use it.
Praesagus
I understand, unfortunately i think this may be the closest you can get. You can improve by putting the function in its own javascript file such as 'styles.js'
Click Upvote
OK, this works the best. I can create a list of vars in JS and have the page replace certain class attributes in the CSS page onload.Using the below code methods works well, but as noted, formatting is changed for the entire page and organization is more difficult.
Praesagus
A: 

The most simple solution to not repeat declarations is to combine a few classes, as you know.

Maybe are you looking for css variables?

eKek0
+1  A: 

You can also use a server side solution like php (or any other you have access to).

Using php you would save the style-sheet as a php file which would get "executed" every time it is requested. You could put all your variables on top and echo them where ever you need them.

The browser sees a normal style-sheet, but the server would have some extra work to do. And it does not really improve the readability of the style-sheet...

Not a very elegant solution, but it works.

jeroen
It depends on the application but a slightly tidier method would be to use the PHP stylesheet to generate a set of static CSS files which are then use on the production site. It's then easy to make new versions by changing variables and saving the output CSS, and the server doesn't take a PHP hit.
Alistair Knock
True, but that would only be possible if the style-sheet of the production site is fixed. Although the OP does not say so, my initial thought that came to mind after reading the question was theming (is that a word?).
jeroen
Actually this might be very elegant and flexible. I'll test it out and see if it works better than the standard sheets.
Praesagus
I guess it depends on the editor you are using as well, it can mess up the colour coding (if your editor has that...).
jeroen
A: 

One solution is to use a PHP stylesheet:

<?
header('Content-type: text/css');

$color1 = '#123456';

echo <<<EOS

.myTitle {color: $color1}

EOS;
?>
endtime