tags:

views:

120

answers:

8

Hi,

If have a style like this

.Style1
{
   background-color:white;
}

And style 2 like this.

.Style2
{
   border: black solid 1px;
}

How do I get Style2 to have Style1 as a base style???

Malcolm

A: 
.style2 .style1
{
...
}
Lloyd
not correct. This does something completely different
Philippe Leybaert
Really? Can elaborate?
Lloyd
This defines a style for every element that has class "style1" and is a descendant element of anything that has class "style2".
Philippe Leybaert
You'd only need a comma to fix this, though, right? .style2, .style1 {}?
butterchicken
@butterchicken: no :)
Philippe Leybaert
A: 
<... class="style2 style1" ..>
DanDan
A: 

the thing here is to simply use it as (example)

class="style1 style2"

this means the element will use the style1 then style2 (overriding style1 with style2 if any overlapping occurs e.g. if you use the same property in both the last one will be used).

b0x0rz
A: 

As far as I know, there isn't really a way to do this.

You could of course make whatever element you wish to apply those style to include both class names <div class="style1 style2">

And the lack of this functionality is the reason there's been a rise in OOCSS (Object Oriented CSS). And frameworks such as Less.

peirix
A: 
<div class="Style1">
   <div class="Style2">bla bla bla</div>
</div>
Alexander Corotchi
+1  A: 

There's no inheritance in CSS. The closest you can get to inheritance is by specifying 2 classes in your HTML elements:

<div class="Style1 Style2">..</div>

Or you can simply use the same style name:

.Style1 { background-color:white; }
.Style1 { border: black solid 1px; }

Now Style1 will have both properties

Philippe Leybaert
This is right - you can specify multiple styles in the "class" attribute for an element.
Liao
Yeah, I totally agree with this answer.... +1
Kim Andersen
This is twirks that don't directly simulate the inheritance behaviour. The answer from 10goto10 will work quite good as inheritance behaviour on the css class.
awe
A: 

You could use

<span class="style1 style2">

or define

span { /* define base styles for all spans here */ }

span.style1 { /*inherits styles from span, and adds specific styles to that */ }

Using with

<p>Normal p text and <span>style</span>, <span class="style1">style 1 type text</span>.</p>
David Thomas
+5  A: 
.Style1, .Style2 {
     background-color:white;
}

.Style2 {
     border: black solid 1px;
}

This way Style1 and Style2 will both have the background set to white, and only Style2 will also have a black border.

fantastic! i never knew this
Liao