views:

195

answers:

3

I'm trying to use the > CSS child selector in IE7, and it doesn't seem to work. I have nested tables. My outer table has a class name "mytable", and I want the td's of the outer table to show borders. I don't want the inner table td's to have borders.

I think I should be able to have CSS that looks like this:

.mytable { border-style: solid }
.mytable>tr>td { border-style: solid }

But the second line seems to have no effect. If I change the second line to make it less specific, it applies to all the td's - I see too many borders.

td { border-style: solid }

So I think it really is just an issue with the selectors. Pages like this suggest that IE7 should be able to do what I want. Am I doing something silly?

Here's the whole HTML file:

<html>
  <head>
    <style type="text/css">
      .mytable { border-style: solid; border-collapse: collapse;}
      td { border-style: solid; }
    </style>
  </head>

  <body>
    <table class="mytable">
      <tr>
        <td>Outer top-left</td>
        <td>Outer top-right</td>
      </tr>
      <tr>
        <td>Outer bottom-left</td>
        <td>
          <table>
            <tr>
              <td>Inner top-left</td>
              <td>Inner top-right</td>
            </tr>
            <tr>
              <td>Inner bottom-left</td>
              <td>Inner bottom-right</td>
            </tr>
          <table>
        </td>
      </tr>
    <table>
  </body>
</html>
+1  A: 

if by: '.mytable>tr>td' you want to say "the td that is a child of a tr that is a child of .mytable" then you don't need the > at all.

Have you tried it without?

.mytable tr td {}

should do what you're looking for (if I understand your question correctly)

j-man86
Thanks j-man86, but using spaces lets the td be any descendant (child, grandchild, great-grandchild etc), rather than exactly a child. So your suggestion would apply to *all* td's that are anywhere underneath a .mytable.
John
+1  A: 

According to multiple sources, the child selector (>) should work in IE7. You can by pass the problem by using the descendant selector (space) and reverting the style for all of the more deeply-nested elements:

.mytable { border-style: solid; border-collapse: collapse;}
.mytable tr td { border-style: solid; }
.mytable tr td td{ border-style: none; }
Steven Paligo
Thanks Steven. I should have added in my original question that I was aware of that possibility. The gory detail is: I may actually have multiple tables, only some of which are .mytable. I want the .mytable td's to have border-style solid, and all the others to have border-style none. So while your answer does solve the problem for my small example (thanks again!), it's not quite what I'm looking for.
John
Microsoft's documentation (http://msdn.microsoft.com/en-us/library/aa358819%28v=VS.85%29.aspx) says that the browser has to be running in standards-compliant mode for the child combinator to work. Which document mode is you page running in?
Steven Paligo
FYI: In IE7, you can call "alert(document.compatMode);" to display the document mode. If it displays "CSS1Compat", you're in standards-compliant mode. "BackCompat" is displayed for quirks mode.
Steven Paligo
Thanks Steve, I think the compatibility mode was the problem. When I put the <!DOCTYPE...> bit in, it changed into CSS1Compat, and "TR > TD" started working. So the child selector is working, but ".mytable > TR > TD" still doesn't work. I'm not really sure that I should be able to use a class name as a parent in that way, and I guess this says that IE7 won't do it. Thanks again.
John
+1  A: 

Besides using standards-compliant mode you have to write

.mytable>tbody>tr>td 

because - even if it's not written explicitely - there is a tbody element in the DOM between the table and the tr.

bbuser