Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selection of first column of first row of a table (excluding nested tables)

Tags:

css

I'm having difficulty with CSS selectors and I hope someone can perhaps help me out here.

I have some HTML that looks like so:

<table class=myTable>    <tr>       <td>this is the td of interest</td>    </tr>    <tr>       <td>          <table>             <tr><td>I don't want this td!</td></tr>          </table>       </td>    </tr> </table> 

I am trying to apply a background image to the FIRST td of the FIRST tr. So I tried the following:

table.myTable tr:first-child td:first-child {     background-image: url(someUrl.gif); } 

But this is also finding the first td of the first tr of the nested table. I've tried different combinations with > and +, but no luck. Anyone have any ideas?

Note: I'm aiming for a solution that is compatible with IE7+.

like image 859
Roly Avatar asked Aug 29 '11 16:08

Roly


People also ask

Which selector is at first row of the table?

The ::first-line selector is used to add a style to the first line of the specified selector. Note: The following properties can be used with ::first-line: font properties.

How do you select the first TR in a table?

You could also have table > thead > tr and then table > tbody > tr. So you might need to specify whether you want the first row from the thead or the tbody. find('tr:first') will select the tr in the thead. – Jeff S.

How do I freeze the first column in a table?

To freeze the row/column we can use a simple HTML table and CSS. HTML: In HTML we can define the header row by <th> tag or we can use <td> tag also. Below example is using the <th> tag. We also put the table in DIV element to see the horizontal and vertical scrollbar by setting the overflow property of the DIV element.


2 Answers

The selector you need is

table.myTable > tbody > tr:first-child > td:first-child 

There is an implicit TBODY element in there, even though you don't have it in the code.

like image 77
ThatMatthew Avatar answered Nov 05 '22 14:11

ThatMatthew


table.myTable > tr:first-child > td:first-child 

The > means the tr that is a direct child of table

like image 34
George Kastrinis Avatar answered Nov 05 '22 14:11

George Kastrinis