Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Cascade just one level

Say i have a <table> with a css class defined (<table class="x">) and i want to change the 'color' property of just the first level of <td>, is this possible using css without setting classes on the relevant <td>?

I.e.

<table class="x">
    <tr>
        <td>
            xxxx
            <table><tr><td>yyy</td><tr></table>
        </td>
    </tr>
<table>

I only want the xxxx to change color - so .x td{color:red;} will currently apply to xxxx and yyyy when i only want xxxx targeted, but would rather not give the xxxx td a class name

like image 722
maxp Avatar asked Apr 06 '26 19:04

maxp


2 Answers

Try this:

table.x > tr > td { ... }

The > is the immediate child selector.

like image 101
Andrew Hare Avatar answered Apr 10 '26 07:04

Andrew Hare


One option would be to override the cascade effect by setting the second level to what you want, allowing you to cancel out the effects on the top level.

Something like this:

table.x tr td { ...}

table.x tr td table tr td { ... }

The first statement will apply to all td's under the .x table, but then the second statement will override that for the inner nesting levels (and anything below them). Since it comes after the first statement, it will override anything set in statements above.

This also will work in IE because it uses the more basic construction of ancestors/descendants rather than children, which are not as well supported.

like image 43
cdeszaq Avatar answered Apr 10 '26 08:04

cdeszaq