views:

48

answers:

4

Hi,

I am new to jquery. I have this in my code:

$("tbody tr:odd ").addClass("alt");

with css:

tbody tr.alt td {
   background-color: #e6EEEE;
}

I have a cell in table with

<td class="coloron">

Right now, the every other row command is over riding my class="coloron".

How can I maintain my cell unique colour while having every other row colouring?

+2  A: 

Define the styles so that your unique color is defined later in the stylesheet, like this:

tbody tr.alt td {
  background-color: #e6EEEE;
}
tbody tr td.coloron {
  background-color: #FFFFFF;
}

If a row has multiple classes, given the same level of specificity in the style rule, the one defined last in the CSS wins. You can see it working here.

Nick Craver
A good answer, +1
ILMV
This works great!, many thanks!
Gordon
@Gordon - welcome :) be sure to accept an answer if it resolves your problem!
Nick Craver
A: 

Your tbody tr.alt td is more specific than .coloron and will override it, instead do something like this:

tbody tr.alt td.coloron {
   // your CSS
}    

Or perhaps this:

tbody tr td.coloron {
   // your CSS
}
ILMV
A: 

use css !important:

td.coloron {
    background: #ccc !important;
}
aularon
No! This is the wrong way to solve CSS specificity problems, just proves ones lack of understanding of the initial problem.
ILMV
I agree with you : )
aularon
A: 

try adding this in your CSS:

.coloron, .coloron.alt { background:red }

Moin Zaman
One is a `<td>`, one is a `<tr>` :)
Nick Craver
Can you even combine classes like that?
ILMV
@ILMV - Yep, but since they're not on the same element, it doesn't apply here.
Nick Craver
Cheers Nick, was questioning their use in general, you're right the classes belong to two separate elements.
ILMV