views:

60

answers:

6

I have a css class rule:

.test{ text-align:center; font-family:Verdana; }

And i want to create another id rule (I hope It is right calling by "id rule" ):

#divNew1{ color: Red; }
#spanNew2{ color: Green; }
#pNew3{ color: Yellow; }

I have a lot of div elements. I want to pass .test class properties to other elements with only changing css file. That's why i don't want to add class attribute to div elements. The html code below:

<div id="divNew1">Ta ta taaaaa</div>
<span id="spanNew2">Ta ta taaaaa</span>
<p id="pNew3">Ta ta taaaaa</p>

But i want to add .test class properties to #divNew class by using inheritance and i don't want to add class attribute to the div like as above. Is there any way to do this?

A: 
<div id="divNew" class="test">Ta ta taaaaa</div>
jeerose
But you didn't use inheritance to create id rule?
uzay95
Sorry... right you are. Nick is right. In CSS the last style to be declared will be inherited.
jeerose
+6  A: 

Just include the ID class on the upper declartion, the last declaration for any property wins. E.g. if the first rule had a color: Green;, .test would be green, #divNew would still be red.

.test, #divNew{ text-align:center; font-family:Verdana; }
#divNew{ color: Red; }
Nick Craver
beat me to it :(
Chris
You know object oriented inheritence like that: "class NewClass : OldClass" or in java "class NewClass extends OldClass" . Is therey any way to do it like that?
uzay95
You can't, you can make use of nested classes, for example <div class="test1 test2"></div> this will merge both style declarations to the div.
F.Aquino
This the correct answer. Simply put, when you declare a new style, you can separate the list of items (classes or ID's) with a comma. If you want to override those attributes for just one of those items (in this case "#divNew"), you just need to re-declare another set of attributes for that item AFTER the initial declaration, exactly as per Nick's example. This supported even in legacy versions of MISE.
Iain Collins
@uzay95 CSS just doesn't work that way, this is as close as it gets :)
Nick Craver
The CSS paradigm is actually more flexible than J2EE style inheritance paradigm allows for, especially as you can can use selectors to change a style depending on parent elements, and element types, even based on parent element types+classes+ID's (at least, as long as the browser is better than MSIE 6).
Iain Collins
A: 

Not sure to understand you, but:

.test{ text-align:center; font-family:Verdana; }
#divNew.test{ color: Red; }
rsilva
+1  A: 

I believe the question is, can my "#divNew" CSS rule inherit the properties of the existing ".test" rule so that:

[Psuedo Code]

.test { color: red; }

#divNew : .test { border: 1px solid Black }

... results in an element with an id of #divNew getting both red text and a black border.

And the answer is no - there is no syntax for declaring the inheritance of one rule by another rule - but you can apply multiple CSS rules to one element.

In this example, the element would take the rules for "#divNew" and ".test" and ".another". It would override any conflicting properties with the last rule in your CSS.

<div id="#divNew" class="test another">...
Sohnee
+1  A: 

LESS/dotLess allow you to perform additional processing within a CSS file on the server side, using a CSS style syntax. LESS. I'd link to dotLess, but I can't find a functioning link at present (http://www.dotlesscss.com/ is coming up empty for me)

edit

Or T4CSS from Phil Haack

Damien_The_Unbeliever
A: 

What do you mean by inheritance? If in your HTML #divNew is a child of .test, then CSS properties of .test are already inherited by it (unless you override them by setting specific #divNew properties).

Syntax for adding properties directly to #divNew which is also .test:

#divNew.test {/*properties*/}

Syntax for adding properties to #divNew which is a child of .test:

.test #divNew {/*properties*/}
Andrey Vlasov