Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Classes & SubClasses

Tags:

css

Is it possible, other than what I'm doing because it doesn't seem to work, to do this? I want to be able to have subclasses that are under a class to use the CSS specifically for that class.subclass.

CSS

.area1 {     border:1px solid black; } .area1.item {     color:red; } .area2 {     border:1px solid blue; } .area2.item {     color:blue; } 

HTML

<div class="area1">     <table>         <tr>             <td class="item">Text Text Text</td>             <td class="item">Text Text Text</td>         </tr>     </table> </div> <div class="area2">      <table>         <tr>             <td class="item">Text Text Text</td>             <td class="item">Text Text Text</td>         </tr>     </table> </div> 

So that I can just use class="item" for the elements under the parent css class "area1","area2". I know I can use class="area1 item" to get this to work, but I don't understand why it has to be so verbose about it. Shouldn't the css subclass look at what parent class it is under in order to define it?

Note: this works in IE (using 7 right now), but in FF it does not, so I'm assuming this isn't a CSS standard way of doing something.

like image 871
Ryan Abbott Avatar asked Feb 17 '09 21:02

Ryan Abbott


People also ask

How many types of CSS classes are there?

We can divide CSS selectors into five categories: Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state)

How do you write a class in CSS?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)


2 Answers

Just need to add a space:

.area2 .item {     ... } 
like image 110
Chad Birch Avatar answered Sep 25 '22 22:09

Chad Birch


FYI, when you define a rule like you did above, with two selectors chained together:

.area1.item {     color:red; } 

It means:

Apply this style to any element that has both the class "area1" and "item".

Such as:

<div class="area1 item"> 

Sadly it doesn't work in IE6, but that's what it means.

like image 33
Matt Howell Avatar answered Sep 24 '22 22:09

Matt Howell