views:

2228

answers:

5

I want to apply styles only to the table inside the DIV with a particular class:

Note: I'd rather use a css-selector for children elements.

Why does the #1 works and #2 doesnt?

1:

div.test th, div.test td, div.test caption {padding:40px 100px 40px 50px;}

2:

div.test th, td, caption {padding:40px 100px 40px 50px;}

HTML:

<html>
    <head>
        <style>
            div.test > th, td, caption {padding:40px 100px 40px 50px;}
        </style>
    </head>
    <body>
        <div>
            <table border="2">
                <tr><td>some</td></tr>
                <tr><td>data</td></tr>
                <tr><td>here</td></tr>
            </table>
        </div>
        <div class="test">
            <table  border="2">
                <tr><td>some</td></tr>
                <tr><td>data</td></tr>
                <tr><td>here</td></tr>
            </table>
        </div>
    </body>
</html>

What am I doing wrong?

+13  A: 

This code "div.test th, td, caption {padding:40px 100px 40px 50px;}" applies a rule to all th elements which are contained by a div element with a class named test, in addition to all td elements and all caption elements.

It is not the same as "all td, th and caption elements which are contained by a div element with a class of test". To accomplish that you need to change your selectors:

'>' isn't fully supported by some older browsers (I'm looking at you, Internet Explorer).

div.test th,
div.test td,
div.test caption {
    padding: 40px 100px 40px 50px;
}
Ken Browning
is there any way around writing div.test 3 times over?
roman m
@rm Nope. There's no nesting of rules or 'with' type grouping
sblundy
thanx for clarification
roman m
A: 

As far as I know this:

div[class=yourclass] table {  your style here; }

or in your case even this:

div.yourclass table { your style here; }

(but this will work for elements with yourclass that might not be divs) will affect only tables inside yourclass. And, as Ken says, the > is not supported everywhere (and div[class=yourclass] too, so use the point notation for classes).

tunnuz
+6  A: 

The > selector matches direct children only, not descendants.

You want

div.test th, td, caption {}

or more likely

div.test th, div.test td, div.test caption {}

Edit:

The first one says

  div.test th, /* any <th> underneath a <div class="test"> */
  td,          /* or any <td> anywhere at all */
  caption      /* or any <caption> */

Whereas the second says

  div.test th,     /* any <th> underneath a <div class="test"> */
  div.test td,     /* or any <td> underneath a <div class="test"> */
  div.test caption /* or any <caption> underneath a <div class="test">  */

In your original the div.test > th says any <th> which is a **direct** child of <div class="test">, which means it will match <div class="test"><th>this</th></div> but won't match <div class="test"><table><th>this</th></table></div>

Greg
waaaaay morelikely #2 methinks
annakata
I think you should expand this answer to explain why #1 probably isn't what he intends.
SpoonMeiser
yes, please explain why #1 doesn't work
roman m
@Ken: beat you, thank you for the answer
roman m
fwiw, because the td and caption in that selector are "dumb" - they'll match any given th/caption without regard for div.test. That kind of blind targetting is rarely what you want in CSS for anything but the most general styles.
annakata
@annakata: that's a part of css framework, i'm trying to apply it locally
roman m
+1  A: 

Don't forget that the >, + and [ ] selectors are unavailable for IE6 and under.

Erick
+1  A: 
div.test td, div.test caption, div.test th

works for me.

The child selector > does not work in IE6.

Traingamer
And (while I'm looking that up) you got lots of the same answer :-)
Traingamer